Skip to content

Commit e1a9e41

Browse files
authored
chore/cli: migrate orgs to urfave/cli (#1314)
* migrate orgs to urfave/cli * remove legacy orgs command registrations
1 parent b1f2001 commit e1a9e41

10 files changed

Lines changed: 227 additions & 267 deletions

cmd/src/doc.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ Examples:
5757
}
5858

5959
commanders := map[string]*commander{
60-
"": &commands,
61-
"batch": &batchCommands,
62-
"config": &configCommands,
63-
"extsvc": &extsvcCommands,
64-
"code-intel": &codeintelCommands,
65-
"orgs": &orgsCommands,
66-
"orgs members": &orgsMembersCommands,
67-
"repos": &reposCommands,
68-
"users": &usersCommands,
60+
"": &commands,
61+
"batch": &batchCommands,
62+
"config": &configCommands,
63+
"extsvc": &extsvcCommands,
64+
"code-intel": &codeintelCommands,
65+
"repos": &reposCommands,
66+
"users": &usersCommands,
6967
}
7068

7169
pending := out.Pending(output.Line("", output.StylePending, "Rendering Markdown..."))

cmd/src/orgs.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package main
22

33
import (
4-
"flag"
5-
"fmt"
4+
"github.com/sourcegraph/src-cli/internal/clicompat"
5+
"github.com/urfave/cli/v3"
66
)
77

8-
var orgsCommands commander
9-
10-
func init() {
11-
usage := `'src orgs' is a tool that manages organizations on a Sourcegraph instance.
8+
var orgsCommand = clicompat.Wrap(&cli.Command{
9+
Name: "orgs",
10+
Aliases: []string{"org"},
11+
Usage: "manages organizations",
12+
UsageText: "src orgs [command options]",
13+
Description: orgsExamples,
14+
HideVersion: true,
15+
Commands: []*cli.Command{
16+
orgsListCommand,
17+
orgsGetCommand,
18+
orgsCreateCommand,
19+
orgsDeleteCommand,
20+
orgsMembersCommand,
21+
},
22+
})
23+
24+
const orgsExamples = `'src orgs' is a tool that manages organizations on a Sourcegraph instance.
1225
1326
Usage:
1427
@@ -25,23 +38,6 @@ The commands are:
2538
Use "src orgs [command] -h" for more information about a command.
2639
`
2740

28-
flagSet := flag.NewFlagSet("orgs", flag.ExitOnError)
29-
handler := func(args []string) error {
30-
orgsCommands.run(flagSet, "src orgs", usage, args)
31-
return nil
32-
}
33-
34-
// Register the command.
35-
commands = append(commands, &command{
36-
flagSet: flagSet,
37-
aliases: []string{"org"},
38-
handler: handler,
39-
usageFunc: func() {
40-
fmt.Println(usage)
41-
},
42-
})
43-
}
44-
4541
const orgFragment = `
4642
fragment OrgFields on Org {
4743
id

cmd/src/orgs_create.go

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
76

8-
"github.com/sourcegraph/src-cli/internal/api"
7+
"github.com/sourcegraph/src-cli/internal/clicompat"
8+
"github.com/urfave/cli/v3"
99
)
1010

11-
func init() {
12-
usage := `
11+
const orgsCreateExamples = `
1312
Examples:
1413
1514
Create an organization:
@@ -18,24 +17,27 @@ Examples:
1817
1918
`
2019

21-
flagSet := flag.NewFlagSet("create", flag.ExitOnError)
22-
usageFunc := func() {
23-
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())
24-
flagSet.PrintDefaults()
25-
fmt.Println(usage)
26-
}
27-
var (
28-
nameFlag = flagSet.String("name", "", `The new organization's name. (required)`)
29-
displayNameFlag = flagSet.String("display-name", "", `The new organization's display name. Defaults to organization name if unspecified.`)
30-
apiFlags = api.NewFlags(flagSet)
31-
)
20+
var orgsCreateCommand = clicompat.Wrap(&cli.Command{
21+
Name: "create",
22+
Usage: "creates an organization",
23+
UsageText: "src orgs create [options]",
24+
Description: orgsCreateExamples,
25+
HideVersion: true,
26+
Flags: clicompat.WithAPIFlags(
27+
&cli.StringFlag{
28+
Name: "name",
29+
Usage: "The new organization's name. (required)",
30+
},
31+
&cli.StringFlag{
32+
Name: "display-name",
33+
Usage: "The new organization's display name. Defaults to organization name if unspecified.",
34+
},
35+
),
36+
Action: func(ctx context.Context, cmd *cli.Command) error {
37+
name := cmd.String("name")
38+
displayName := cmd.String("display-name")
3239

33-
handler := func(args []string) error {
34-
if err := flagSet.Parse(args); err != nil {
35-
return err
36-
}
37-
38-
client := cfg.apiClient(apiFlags, flagSet.Output())
40+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
3941

4042
query := `mutation CreateOrg(
4143
$name: String!,
@@ -53,20 +55,13 @@ Examples:
5355
CreateOrg Org
5456
}
5557
if ok, err := client.NewRequest(query, map[string]any{
56-
"name": *nameFlag,
57-
"displayName": *displayNameFlag,
58-
}).Do(context.Background(), &result); err != nil || !ok {
58+
"name": name,
59+
"displayName": displayName,
60+
}).Do(ctx, &result); err != nil || !ok {
5961
return err
6062
}
6163

62-
fmt.Printf("Organization %q created.\n", *nameFlag)
63-
return nil
64-
}
65-
66-
// Register the command.
67-
orgsCommands = append(orgsCommands, &command{
68-
flagSet: flagSet,
69-
handler: handler,
70-
usageFunc: usageFunc,
71-
})
72-
}
64+
_, err := fmt.Fprintf(cmd.Writer, "Organization %q created.\n", name)
65+
return err
66+
},
67+
})

cmd/src/orgs_delete.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
76

8-
"github.com/sourcegraph/src-cli/internal/api"
7+
"github.com/sourcegraph/src-cli/internal/clicompat"
8+
"github.com/urfave/cli/v3"
99
)
1010

11-
func init() {
12-
usage := `
11+
const orgsDeleteExamples = `
1312
Examples:
1413
1514
Delete an organization by ID:
@@ -26,23 +25,21 @@ Examples:
2625
2726
`
2827

29-
flagSet := flag.NewFlagSet("delete", flag.ExitOnError)
30-
usageFunc := func() {
31-
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())
32-
flagSet.PrintDefaults()
33-
fmt.Println(usage)
34-
}
35-
var (
36-
orgIDFlag = flagSet.String("id", "", `The ID of the organization to delete.`)
37-
apiFlags = api.NewFlags(flagSet)
38-
)
39-
40-
handler := func(args []string) error {
41-
if err := flagSet.Parse(args); err != nil {
42-
return err
43-
}
44-
45-
client := cfg.apiClient(apiFlags, flagSet.Output())
28+
var orgsDeleteCommand = clicompat.Wrap(&cli.Command{
29+
Name: "delete",
30+
Usage: "deletes an organization",
31+
UsageText: "src orgs delete [options]",
32+
Description: orgsDeleteExamples,
33+
HideVersion: true,
34+
Flags: clicompat.WithAPIFlags(
35+
&cli.StringFlag{
36+
Name: "id",
37+
Usage: "The ID of the organization to delete.",
38+
},
39+
),
40+
Action: func(ctx context.Context, cmd *cli.Command) error {
41+
orgID := cmd.String("id")
42+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
4643

4744
query := `mutation DeleteOrganization(
4845
$organization: ID!
@@ -58,19 +55,12 @@ Examples:
5855
DeleteOrganization struct{}
5956
}
6057
if ok, err := client.NewRequest(query, map[string]any{
61-
"organization": *orgIDFlag,
62-
}).Do(context.Background(), &result); err != nil || !ok {
58+
"organization": orgID,
59+
}).Do(ctx, &result); err != nil || !ok {
6360
return err
6461
}
6562

66-
fmt.Printf("Organization with ID %q deleted.\n", *orgIDFlag)
67-
return nil
68-
}
69-
70-
// Register the command.
71-
orgsCommands = append(orgsCommands, &command{
72-
flagSet: flagSet,
73-
handler: handler,
74-
usageFunc: usageFunc,
75-
})
76-
}
63+
_, err := fmt.Fprintf(cmd.Writer, "Organization with ID %q deleted.\n", orgID)
64+
return err
65+
},
66+
})

cmd/src/orgs_get.go

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package main
22

33
import (
44
"context"
5-
"flag"
6-
"fmt"
75

8-
"github.com/sourcegraph/src-cli/internal/api"
6+
"github.com/sourcegraph/src-cli/internal/clicompat"
7+
"github.com/urfave/cli/v3"
98
)
109

11-
func init() {
12-
usage := `
10+
const orgsGetExamples = `
1311
Examples:
1412
1513
Get organization named abc-org:
@@ -22,26 +20,30 @@ Examples:
2220
2321
`
2422

25-
flagSet := flag.NewFlagSet("get", flag.ExitOnError)
26-
usageFunc := func() {
27-
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())
28-
flagSet.PrintDefaults()
29-
fmt.Println(usage)
30-
}
31-
var (
32-
nameFlag = flagSet.String("name", "", `Look up organization by name. (e.g. "abc-org")`)
33-
formatFlag = flagSet.String("f", "{{.|json}}", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}} ({{.DisplayName}})")`)
34-
apiFlags = api.NewFlags(flagSet)
35-
)
36-
37-
handler := func(args []string) error {
38-
if err := flagSet.Parse(args); err != nil {
39-
return err
40-
}
41-
42-
client := cfg.apiClient(apiFlags, flagSet.Output())
43-
44-
tmpl, err := parseTemplate(*formatFlag)
23+
var orgsGetCommand = clicompat.Wrap(&cli.Command{
24+
Name: "get",
25+
Usage: "gets an organization",
26+
UsageText: "src orgs get [options]",
27+
Description: orgsGetExamples,
28+
HideVersion: true,
29+
Flags: clicompat.WithAPIFlags(
30+
&cli.StringFlag{
31+
Name: "name",
32+
Usage: `Look up organization by name. (e.g. "abc-org")`,
33+
},
34+
&cli.StringFlag{
35+
Name: "f",
36+
Value: "{{.|json}}",
37+
Usage: `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}} ({{.DisplayName}})")`,
38+
},
39+
),
40+
Action: func(ctx context.Context, cmd *cli.Command) error {
41+
name := cmd.String("name")
42+
format := cmd.String("f")
43+
44+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
45+
46+
tmpl, err := parseTemplate(format)
4547
if err != nil {
4648
return err
4749
}
@@ -60,18 +62,11 @@ Examples:
6062
Organization *Org
6163
}
6264
if ok, err := client.NewRequest(query, map[string]any{
63-
"name": *nameFlag,
64-
}).Do(context.Background(), &result); err != nil || !ok {
65+
"name": name,
66+
}).Do(ctx, &result); err != nil || !ok {
6567
return err
6668
}
6769

6870
return execTemplate(tmpl, result.Organization)
69-
}
70-
71-
// Register the command.
72-
orgsCommands = append(orgsCommands, &command{
73-
flagSet: flagSet,
74-
handler: handler,
75-
usageFunc: usageFunc,
76-
})
77-
}
71+
},
72+
})

0 commit comments

Comments
 (0)