Skip to content

Commit 24ef747

Browse files
committed
migrate orgs to urfave/cli
1 parent 82de268 commit 24ef747

9 files changed

Lines changed: 388 additions & 16 deletions

cmd/src/orgs.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,30 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
7+
"github.com/sourcegraph/src-cli/internal/clicompat"
8+
"github.com/urfave/cli/v3"
69
)
710

811
var orgsCommands commander
912

10-
func init() {
11-
usage := `'src orgs' is a tool that manages organizations on a Sourcegraph instance.
13+
var orgsCommand = clicompat.Wrap(&cli.Command{
14+
Name: "orgs",
15+
Aliases: []string{"org"},
16+
Usage: "manages organizations",
17+
UsageText: "src orgs [command options]",
18+
Description: orgsExamples,
19+
HideVersion: true,
20+
Commands: []*cli.Command{
21+
orgsListCommand,
22+
orgsGetCommand,
23+
orgsCreateCommand,
24+
orgsDeleteCommand,
25+
orgsMembersCommand,
26+
},
27+
})
28+
29+
const orgsExamples = `'src orgs' is a tool that manages organizations on a Sourcegraph instance.
1230
1331
Usage:
1432
@@ -25,6 +43,9 @@ The commands are:
2543
Use "src orgs [command] -h" for more information about a command.
2644
`
2745

46+
func init() {
47+
usage := orgsExamples
48+
2849
flagSet := flag.NewFlagSet("orgs", flag.ExitOnError)
2950
handler := func(args []string) error {
3051
orgsCommands.run(flagSet, "src orgs", usage, args)

cmd/src/orgs_create.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"fmt"
77

88
"github.com/sourcegraph/src-cli/internal/api"
9+
"github.com/sourcegraph/src-cli/internal/clicompat"
10+
"github.com/urfave/cli/v3"
911
)
1012

11-
func init() {
12-
usage := `
13+
const orgsCreateExamples = `
1314
Examples:
1415
1516
Create an organization:
@@ -18,6 +19,58 @@ Examples:
1819
1920
`
2021

22+
var orgsCreateCommand = clicompat.Wrap(&cli.Command{
23+
Name: "create",
24+
Usage: "creates an organization",
25+
UsageText: "src orgs create [options]",
26+
Description: orgsCreateExamples,
27+
HideVersion: true,
28+
Flags: clicompat.WithAPIFlags(
29+
&cli.StringFlag{
30+
Name: "name",
31+
Usage: "The new organization's name. (required)",
32+
},
33+
&cli.StringFlag{
34+
Name: "display-name",
35+
Usage: "The new organization's display name. Defaults to organization name if unspecified.",
36+
},
37+
),
38+
Action: func(ctx context.Context, cmd *cli.Command) error {
39+
name := cmd.String("name")
40+
displayName := cmd.String("display-name")
41+
42+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
43+
44+
query := `mutation CreateOrg(
45+
$name: String!,
46+
$displayName: String!,
47+
) {
48+
createOrganization(
49+
name: $name,
50+
displayName: $displayName,
51+
) {
52+
id
53+
}
54+
}`
55+
56+
var result struct {
57+
CreateOrg Org
58+
}
59+
if ok, err := client.NewRequest(query, map[string]any{
60+
"name": name,
61+
"displayName": displayName,
62+
}).Do(ctx, &result); err != nil || !ok {
63+
return err
64+
}
65+
66+
_, err := fmt.Fprintf(cmd.Writer, "Organization %q created.\n", name)
67+
return err
68+
},
69+
})
70+
71+
func init() {
72+
usage := orgsCreateExamples
73+
2174
flagSet := flag.NewFlagSet("create", flag.ExitOnError)
2275
usageFunc := func() {
2376
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())

cmd/src/orgs_delete.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"fmt"
77

88
"github.com/sourcegraph/src-cli/internal/api"
9+
"github.com/sourcegraph/src-cli/internal/clicompat"
10+
"github.com/urfave/cli/v3"
911
)
1012

11-
func init() {
12-
usage := `
13+
const orgsDeleteExamples = `
1314
Examples:
1415
1516
Delete an organization by ID:
@@ -26,6 +27,49 @@ Examples:
2627
2728
`
2829

30+
var orgsDeleteCommand = clicompat.Wrap(&cli.Command{
31+
Name: "delete",
32+
Usage: "deletes an organization",
33+
UsageText: "src orgs delete [options]",
34+
Description: orgsDeleteExamples,
35+
HideVersion: true,
36+
Flags: clicompat.WithAPIFlags(
37+
&cli.StringFlag{
38+
Name: "id",
39+
Usage: "The ID of the organization to delete.",
40+
},
41+
),
42+
Action: func(ctx context.Context, cmd *cli.Command) error {
43+
orgID := cmd.String("id")
44+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
45+
46+
query := `mutation DeleteOrganization(
47+
$organization: ID!
48+
) {
49+
deleteOrganization(
50+
organization: $organization
51+
) {
52+
alwaysNil
53+
}
54+
}`
55+
56+
var result struct {
57+
DeleteOrganization struct{}
58+
}
59+
if ok, err := client.NewRequest(query, map[string]any{
60+
"organization": orgID,
61+
}).Do(ctx, &result); err != nil || !ok {
62+
return err
63+
}
64+
65+
_, err := fmt.Fprintf(cmd.Writer, "Organization with ID %q deleted.\n", orgID)
66+
return err
67+
},
68+
})
69+
70+
func init() {
71+
usage := orgsDeleteExamples
72+
2973
flagSet := flag.NewFlagSet("delete", flag.ExitOnError)
3074
usageFunc := func() {
3175
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())

cmd/src/orgs_get.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"fmt"
77

88
"github.com/sourcegraph/src-cli/internal/api"
9+
"github.com/sourcegraph/src-cli/internal/clicompat"
10+
"github.com/urfave/cli/v3"
911
)
1012

11-
func init() {
12-
usage := `
13+
const orgsGetExamples = `
1314
Examples:
1415
1516
Get organization named abc-org:
@@ -22,6 +23,60 @@ Examples:
2223
2324
`
2425

26+
var orgsGetCommand = clicompat.Wrap(&cli.Command{
27+
Name: "get",
28+
Usage: "gets an organization",
29+
UsageText: "src orgs get [options]",
30+
Description: orgsGetExamples,
31+
HideVersion: true,
32+
Flags: clicompat.WithAPIFlags(
33+
&cli.StringFlag{
34+
Name: "name",
35+
Usage: `Look up organization by name. (e.g. "abc-org")`,
36+
},
37+
&cli.StringFlag{
38+
Name: "f",
39+
Value: "{{.|json}}",
40+
Usage: `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}} ({{.DisplayName}})")`,
41+
},
42+
),
43+
Action: func(ctx context.Context, cmd *cli.Command) error {
44+
name := cmd.String("name")
45+
format := cmd.String("f")
46+
47+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
48+
49+
tmpl, err := parseTemplate(format)
50+
if err != nil {
51+
return err
52+
}
53+
54+
query := `query Organization(
55+
$name: String!,
56+
) {
57+
organization(
58+
name: $name
59+
) {
60+
...OrgFields
61+
}
62+
}` + orgFragment
63+
64+
var result struct {
65+
Organization *Org
66+
}
67+
if ok, err := client.NewRequest(query, map[string]any{
68+
"name": name,
69+
}).Do(ctx, &result); err != nil || !ok {
70+
return err
71+
}
72+
73+
return execTemplate(tmpl, result.Organization)
74+
},
75+
})
76+
77+
func init() {
78+
usage := orgsGetExamples
79+
2580
flagSet := flag.NewFlagSet("get", flag.ExitOnError)
2681
usageFunc := func() {
2782
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())

cmd/src/orgs_list.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"fmt"
77

88
"github.com/sourcegraph/src-cli/internal/api"
9+
"github.com/sourcegraph/src-cli/internal/clicompat"
10+
"github.com/urfave/cli/v3"
911
)
1012

11-
func init() {
12-
usage := `
13+
const orgsListExamples = `
1314
Examples:
1415
1516
List organizations:
@@ -22,6 +23,78 @@ Examples:
2223
2324
`
2425

26+
var orgsListCommand = clicompat.Wrap(&cli.Command{
27+
Name: "list",
28+
Usage: "lists organizations",
29+
UsageText: "src orgs list [options]",
30+
Description: orgsListExamples,
31+
HideVersion: true,
32+
Flags: clicompat.WithAPIFlags(
33+
&cli.IntFlag{
34+
Name: "first",
35+
Value: 1000,
36+
Usage: "Returns the first n organizations from the list.",
37+
},
38+
&cli.StringFlag{
39+
Name: "query",
40+
Usage: `Returns organizations whose names match the query. (e.g. "alice")`,
41+
},
42+
&cli.StringFlag{
43+
Name: "f",
44+
Value: "{{.Name}}",
45+
Usage: `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}} ({{.DisplayName}})" or "{{.|json}}")`,
46+
},
47+
),
48+
Action: func(ctx context.Context, cmd *cli.Command) error {
49+
first := cmd.Int("first")
50+
queryValue := cmd.String("query")
51+
format := cmd.String("f")
52+
53+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
54+
55+
tmpl, err := parseTemplate(format)
56+
if err != nil {
57+
return err
58+
}
59+
60+
query := `query Organizations(
61+
$first: Int,
62+
$query: String,
63+
) {
64+
organizations(
65+
first: $first,
66+
query: $query,
67+
) {
68+
nodes {
69+
...OrgFields
70+
}
71+
}
72+
}` + orgFragment
73+
74+
var result struct {
75+
Organizations struct {
76+
Nodes []Org
77+
}
78+
}
79+
if ok, err := client.NewRequest(query, map[string]any{
80+
"first": api.NullInt(first),
81+
"query": api.NullString(queryValue),
82+
}).Do(ctx, &result); err != nil || !ok {
83+
return err
84+
}
85+
86+
for _, org := range result.Organizations.Nodes {
87+
if err := execTemplate(tmpl, org); err != nil {
88+
return err
89+
}
90+
}
91+
return nil
92+
},
93+
})
94+
95+
func init() {
96+
usage := orgsListExamples
97+
2598
flagSet := flag.NewFlagSet("list", flag.ExitOnError)
2699
usageFunc := func() {
27100
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())

cmd/src/orgs_members.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
7+
"github.com/sourcegraph/src-cli/internal/clicompat"
8+
"github.com/urfave/cli/v3"
69
)
710

811
var orgsMembersCommands commander
912

10-
func init() {
11-
usage := `'src orgs members' is a tool that manages organization members on a Sourcegraph instance.
13+
var orgsMembersCommand = clicompat.Wrap(&cli.Command{
14+
Name: "members",
15+
Aliases: []string{"member"},
16+
Usage: "manages organization members",
17+
UsageText: "src orgs members [command options]",
18+
Description: orgsMembersExamples,
19+
HideVersion: true,
20+
Commands: []*cli.Command{
21+
orgsMembersAddCommand,
22+
orgsMembersRemoveCommand,
23+
},
24+
})
25+
26+
const orgsMembersExamples = `'src orgs members' is a tool that manages organization members on a Sourcegraph instance.
1227
1328
Usage:
1429
@@ -22,6 +37,9 @@ The commands are:
2237
Use "src orgs members [command] -h" for more information about a command.
2338
`
2439

40+
func init() {
41+
usage := orgsMembersExamples
42+
2543
flagSet := flag.NewFlagSet("members", flag.ExitOnError)
2644
handler := func(args []string) error {
2745
orgsMembersCommands.run(flagSet, "src orgs members", usage, args)

0 commit comments

Comments
 (0)