forked from sourcegraph/src-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeowners_create.go
More file actions
118 lines (98 loc) · 2.93 KB
/
codeowners_create.go
File metadata and controls
118 lines (98 loc) · 2.93 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
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"context"
"flag"
"fmt"
"io"
"strings"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/cmderrors"
)
func init() {
usage := `
Examples:
Create a codeowners file for the repository "github.com/sourcegraph/sourcegraph":
$ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f CODEOWNERS
Create a codeowners file for the repository "github.com/sourcegraph/sourcegraph" from stdin:
$ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f -
`
flagSet := flag.NewFlagSet("create", 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")
fileFlag = flagSet.String("file", "", "File path to read ownership information from (- for stdin)")
fileShortFlag = flagSet.String("f", "", "File path to read ownership information from (- for stdin). Alias for -file")
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")
}
if *fileFlag == "" && *fileShortFlag == "" {
return errors.New("provide a file using -file")
}
if *fileFlag != "" && *fileShortFlag != "" {
return errors.New("have to provide either -file or -f")
}
if *fileShortFlag != "" {
*fileFlag = *fileShortFlag
}
file, err := readFile(*fileFlag)
if err != nil {
return err
}
content, err := io.ReadAll(file)
if err != nil {
return err
}
client := cfg.apiClient(apiFlags, flagSet.Output())
query := `mutation CreateCodeownersFile(
$repoName: String!,
$content: String!
) {
addCodeownersFile(input: {
repoName: $repoName,
fileContents: $content,
}
) {
...CodeownersFileFields
}
}
` + codeownersFragment
var result struct {
AddCodeownersFile CodeownersIngestedFile
}
if ok, err := client.NewRequest(query, map[string]interface{}{
"repoName": *repoFlag,
"content": string(content),
}).Do(context.Background(), &result); err != nil || !ok {
var gqlErr api.GraphQlErrors
if errors.As(err, &gqlErr) {
for _, e := range gqlErr {
if strings.Contains(e.Error(), "repo not found:") {
return cmderrors.ExitCode(2, errors.Newf("repository %q not found", *repoFlag))
}
if strings.Contains(e.Error(), "codeowners file has already been ingested for this repository") {
return cmderrors.ExitCode(2, errors.New("codeowners file has already been ingested for this repository"))
}
}
}
return err
}
return nil
}
// Register the command.
codeownersCommands = append(codeownersCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}