-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcodeowners_get.go
More file actions
87 lines (70 loc) · 1.92 KB
/
codeowners_get.go
File metadata and controls
87 lines (70 loc) · 1.92 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
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/cmderrors"
)
func init() {
usage := `
Examples:
Read the current codeowners file for the repository "github.com/sourcegraph/sourcegraph":
$ src codeowners get -repo='github.com/sourcegraph/sourcegraph'
`
flagSet := flag.NewFlagSet("get", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src codeowners %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
repoFlag = flagSet.String("repo", "", "The repository to attach the data to")
apiFlags = api.NewFlags(flagSet)
)
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
if *repoFlag == "" {
return errors.New("provide a repo name using -repo")
}
client := cfg.apiClient(apiFlags, flagSet.Output())
query := `query GetCodeownersFile(
$repoName: String!
) {
repository(name: $repoName) {
ingestedCodeowners {
...CodeownersFileFields
}
}
}
` + codeownersFragment
var result struct {
Repository *struct {
IngestedCodeowners *CodeownersIngestedFile
}
}
if ok, err := client.NewRequest(query, map[string]any{
"repoName": *repoFlag,
}).Do(context.Background(), &result); err != nil || !ok {
return err
}
if result.Repository == nil {
return cmderrors.ExitCode(2, errors.Newf("repository %q not found", *repoFlag))
}
if result.Repository.IngestedCodeowners == nil {
return cmderrors.ExitCode(2, errors.Newf("no codeowners data found for %q", *repoFlag))
}
fmt.Fprintf(os.Stdout, "%s", result.Repository.IngestedCodeowners.Contents)
return nil
}
// Register the command.
codeownersCommands = append(codeownersCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}