-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add orgs members add/remove commands
- Loading branch information
Showing
4 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var orgsMembersCommands commander | ||
|
||
func init() { | ||
usage := `'src orgs members' is a tool that manages organization members on a Sourcegraph instance. | ||
Usage: | ||
src orgs members command [command options] | ||
The commands are: | ||
add adds a user as a member to an organization | ||
remove removes a user as a member from an organization | ||
Use "src orgs members [command] -h" for more information about a command. | ||
` | ||
|
||
flagSet := flag.NewFlagSet("members", flag.ExitOnError) | ||
handler := func(args []string) error { | ||
orgsMembersCommands.run(flagSet, "src orgs members", usage, args) | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
orgsCommands = append(orgsCommands, &command{ | ||
flagSet: flagSet, | ||
aliases: []string{"member"}, | ||
handler: handler, | ||
usageFunc: func() { | ||
fmt.Println(usage) | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
func init() { | ||
usage := ` | ||
Examples: | ||
Add a member (alice) to an organization (abc-org): | ||
$ src orgs members add -org-id=$(src org get -f '{{.ID}}' -name=abc-org) -username=alice | ||
` | ||
|
||
flagSet := flag.NewFlagSet("add", flag.ExitOnError) | ||
usageFunc := func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs members %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Println(usage) | ||
} | ||
var ( | ||
orgIDFlag = flagSet.String("org-id", "", "ID of organization to which to add member. (required)") | ||
usernameFlag = flagSet.String("username", "", "Username of user to add as member. (required)") | ||
apiFlags = newAPIFlags(flagSet) | ||
) | ||
|
||
handler := func(args []string) error { | ||
flagSet.Parse(args) | ||
|
||
query := `mutation AddUserToOrganization( | ||
$organization: ID!, | ||
$usernameOrEmail: String!, | ||
) { | ||
addUserToOrganization( | ||
organization: $organization, | ||
usernameOrEmail: $usernameOrEmail, | ||
) { | ||
alwaysNil | ||
} | ||
}` | ||
|
||
var result struct { | ||
AddUserToOrganization struct{} | ||
} | ||
return (&apiRequest{ | ||
query: query, | ||
vars: map[string]interface{}{ | ||
"organization": *orgIDFlag, | ||
"usernameOrEmail": *usernameFlag, | ||
}, | ||
result: &result, | ||
done: func() error { | ||
fmt.Printf("User %q added as member to organization with ID %q.\n", *usernameFlag, *orgIDFlag) | ||
return nil | ||
}, | ||
flags: apiFlags, | ||
}).do() | ||
} | ||
|
||
// Register the command. | ||
orgsMembersCommands = append(orgsMembersCommands, &command{ | ||
flagSet: flagSet, | ||
handler: handler, | ||
usageFunc: usageFunc, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
func init() { | ||
usage := ` | ||
Examples: | ||
Remove a member (alice) from an organization (abc-org): | ||
$ src orgs members remove -org-id=$(src org get -f '{{.ID}}' -name=abc-org) -user-id=$(src users get -f '{{.ID}}' -username=alice) | ||
` | ||
|
||
flagSet := flag.NewFlagSet("remove", flag.ExitOnError) | ||
usageFunc := func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs members %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Println(usage) | ||
} | ||
var ( | ||
orgIDFlag = flagSet.String("org-id", "", "ID of organization from which to remove member. (required)") | ||
userIDFlag = flagSet.String("user-id", "", "ID of user to remove as member. (required)") | ||
apiFlags = newAPIFlags(flagSet) | ||
) | ||
|
||
handler := func(args []string) error { | ||
flagSet.Parse(args) | ||
|
||
query := `mutation RemoveUserFromOrg( | ||
$orgID: ID!, | ||
$userID: ID!, | ||
) { | ||
removeUserFromOrg( | ||
orgID: $orgID, | ||
userID: $userID, | ||
) { | ||
alwaysNil | ||
} | ||
}` | ||
|
||
var result struct { | ||
RemoveUserFromOrg struct{} | ||
} | ||
return (&apiRequest{ | ||
query: query, | ||
vars: map[string]interface{}{ | ||
"orgID": *orgIDFlag, | ||
"userID": *userIDFlag, | ||
}, | ||
result: &result, | ||
done: func() error { | ||
fmt.Printf("User %q removed as member from organization with ID %q.\n", *userIDFlag, *orgIDFlag) | ||
return nil | ||
}, | ||
flags: apiFlags, | ||
}).do() | ||
} | ||
|
||
// Register the command. | ||
orgsMembersCommands = append(orgsMembersCommands, &command{ | ||
flagSet: flagSet, | ||
handler: handler, | ||
usageFunc: usageFunc, | ||
}) | ||
} |