@@ -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 = `
1314Examples:
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 ())
0 commit comments