-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathextsvc_list.go
More file actions
105 lines (88 loc) · 2.29 KB
/
extsvc_list.go
File metadata and controls
105 lines (88 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"flag"
"fmt"
"github.com/sourcegraph/src-cli/internal/api"
)
func init() {
usage := `
Examples:
List external service configurations on the Sourcegraph instance:
$ src extsvc list
List external service configurations and choose output format:
$ src extsvc list -f '{{.ID}}'
`
flagSet := flag.NewFlagSet("list", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src extsvc %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
firstFlag = flagSet.Int("first", 1000, "Return only the first n external services.")
formatFlag = flagSet.String("f", "", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.|json}}")`)
apiFlags = api.NewFlags(flagSet)
)
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
first := *firstFlag
var formatStr string
if *formatFlag != "" {
formatStr = *formatFlag
} else {
// Set default here instead of in flagSet.String because it is very long and makes the usage message ugly.
formatStr = `{{range .Nodes}}ID: {{.id}} | {{padRight .kind 15 " "}} | {{.displayName}}{{"\n"}}{{end}}`
}
tmpl, err := parseTemplate(formatStr)
if err != nil {
return err
}
ctx := context.Background()
client := cfg.apiClient(apiFlags, flagSet.Output())
queryVars := map[string]any{
"first": first,
}
var result externalServicesListResult
if ok, err := client.NewRequest(externalServicesListQuery, queryVars).Do(ctx, &result); err != nil || !ok {
return err
}
return execTemplate(tmpl, result.ExternalServices)
}
// Register the command.
extsvcCommands = append(extsvcCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
const externalServicesListQuery = `
query ($first: Int!) {
externalServices(first: $first) {
nodes {
id
kind
displayName
config
createdAt
updatedAt
}
totalCount
pageInfo {
hasNextPage
}
}
}
`
// Typing here is primarily for ordering of results as we format them (maps are unordered).
type externalServicesListResult struct {
ExternalServices struct {
Nodes []map[string]any
TotalCount int
PageInfo struct {
HasNextPage bool
}
}
}