-
Notifications
You must be signed in to change notification settings - Fork 56
/
repos_get.go
74 lines (60 loc) · 1.5 KB
/
repos_get.go
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
package main
import (
"context"
"flag"
"fmt"
"github.com/sourcegraph/src-cli/internal/api"
)
func init() {
usage := `
Examples:
Look up a repository by name:
$ src repos get -name=github.com/sourcegraph/src-cli
`
flagSet := flag.NewFlagSet("get", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src repos %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
nameFlag = flagSet.String("name", "", "The name of the repository. (required)")
formatFlag = flagSet.String("f", "{{.ID}}", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}}") or "{{.|json}}")`)
apiFlags = api.NewFlags(flagSet)
)
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
client := cfg.apiClient(apiFlags, flagSet.Output())
tmpl, err := parseTemplate(*formatFlag)
if err != nil {
return err
}
query := `query Repository(
$name: String!,
) {
repository(
name: $name
) {
...RepositoryFields
}
}
` + repositoryFragment
var result struct {
Repository Repository
}
if ok, err := client.NewRequest(query, map[string]interface{}{
"name": *nameFlag,
}).Do(context.Background(), &result); err != nil || !ok {
return err
}
return execTemplate(tmpl, result.Repository)
}
// Register the command.
reposCommands = append(reposCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}