Skip to content

Commit fc0462d

Browse files
authored
Add a add subcommand to extsvc (#856)
* Add a add subcommand to extsvc In the context of scaletesting, it's pretty useful to be able to add code hosts on the fly, while toying with a test instance. This PR adds such a command. * fixup * Small fixes
1 parent 1e95160 commit fc0462d

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

cmd/src/extsvc.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The commands are:
2323
2424
list lists the external services on the Sourcegraph instance
2525
edit edits external services on the Sourcegraph instance
26+
add add an external service on the Sourcegraph instance
2627
2728
Use "src extsvc [command] -h" for more information about a command.
2829
`
@@ -52,6 +53,8 @@ type externalService struct {
5253
CreatedAt, UpdatedAt string
5354
}
5455

56+
var errServiceNotFound = errors.New("no such external service")
57+
5558
func lookupExternalService(ctx context.Context, client api.Client, byID, byName string) (*externalService, error) {
5659
var result struct {
5760
ExternalServices struct {
@@ -72,5 +75,5 @@ func lookupExternalService(ctx context.Context, client api.Client, byID, byName
7275
return svc, nil
7376
}
7477
}
75-
return nil, errors.New("no such external service")
78+
return nil, errServiceNotFound
7679
}

cmd/src/extsvc_create.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"os"
9+
"strings"
10+
11+
"github.com/mattn/go-isatty"
12+
"github.com/sourcegraph/sourcegraph/lib/errors"
13+
"github.com/sourcegraph/src-cli/internal/api"
14+
)
15+
16+
func init() {
17+
usage := `
18+
Examples:
19+
20+
create an external service configuration on the Sourcegraph instance:
21+
22+
$ cat new-config.json | src extsvc create
23+
$ src extsvc create -name 'My GitHub connection' new-config.json
24+
`
25+
26+
flagSet := flag.NewFlagSet("create", flag.ExitOnError)
27+
usageFunc := func() {
28+
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src extsvc %s':\n", flagSet.Name())
29+
flagSet.PrintDefaults()
30+
fmt.Println(usage)
31+
}
32+
var (
33+
nameFlag = flagSet.String("name", "", "exact name of the external service to create")
34+
kindFlag = flagSet.String("kind", "", "kind of the external service to create")
35+
apiFlags = api.NewFlags(flagSet)
36+
)
37+
38+
handler := func(args []string) (err error) {
39+
ctx := context.Background()
40+
if err := flagSet.Parse(args); err != nil {
41+
return err
42+
}
43+
if *nameFlag == "" {
44+
return errors.New("-name must be provided")
45+
}
46+
47+
var createJSON []byte
48+
if len(flagSet.Args()) == 1 {
49+
createJSON, err = os.ReadFile(flagSet.Arg(0))
50+
if err != nil {
51+
return err
52+
}
53+
}
54+
if !isatty.IsTerminal(os.Stdin.Fd()) {
55+
// stdin is a pipe not a terminal
56+
createJSON, err = io.ReadAll(os.Stdin)
57+
if err != nil {
58+
return err
59+
}
60+
}
61+
62+
createExternalServiceInput := map[string]interface{}{
63+
"kind": strings.ToUpper(*kindFlag),
64+
"displayName": *nameFlag,
65+
"config": string(createJSON),
66+
}
67+
queryVars := map[string]interface{}{
68+
"input": createExternalServiceInput,
69+
}
70+
var result struct{} // TODO: future: allow formatting resulting external service
71+
72+
client := cfg.apiClient(apiFlags, flagSet.Output())
73+
if ok, err := client.NewRequest(externalServicesCreateMutation, queryVars).Do(ctx, &result); err != nil {
74+
return err
75+
} else if ok {
76+
fmt.Println("External service created:", *nameFlag)
77+
}
78+
return nil
79+
}
80+
81+
// Register the command.
82+
extsvcCommands = append(extsvcCommands, &command{
83+
flagSet: flagSet,
84+
handler: handler,
85+
usageFunc: usageFunc,
86+
})
87+
}
88+
89+
const externalServicesCreateMutation = `
90+
mutation AddExternalService($input: AddExternalServiceInput!) {
91+
addExternalService(input: $input) {
92+
id
93+
warning
94+
}
95+
}`

0 commit comments

Comments
 (0)