@@ -2,74 +2,57 @@ package main
22
33import (
44 "context"
5- "flag"
6- "fmt"
7- "io"
85 "strings"
96
107 "github.com/sourcegraph/sourcegraph/lib/errors"
118
129 "github.com/sourcegraph/src-cli/internal/api"
10+ "github.com/sourcegraph/src-cli/internal/clicompat"
1311 "github.com/sourcegraph/src-cli/internal/cmderrors"
12+ "github.com/urfave/cli/v3"
1413)
1514
16- func init () {
17- usage := `
18- Examples:
19-
20- Create a codeowners file for the repository "github.com/sourcegraph/sourcegraph":
21-
22- $ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f CODEOWNERS
15+ const codeownersCreateExamples = `
16+ Create a codeowners file for a repository.
2317
24- Create a codeowners file for the repository "github.com/sourcegraph/sourcegraph" from stdin :
18+ Examples :
2519
26- $ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f -
20+ $ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f CODEOWNERS
21+ $ src codeowners create -repo='github.com/sourcegraph/sourcegraph' -f -
2722`
2823
29- flagSet := flag .NewFlagSet ("create" , flag .ExitOnError )
30- usageFunc := func () {
31- fmt .Fprintf (flag .CommandLine .Output (), "Usage of 'src codeowners %s':\n " , flagSet .Name ())
32- flagSet .PrintDefaults ()
33- fmt .Println (usage )
34- }
35- var (
36- repoFlag = flagSet .String ("repo" , "" , "The repository to attach the data to" )
37- fileFlag = flagSet .String ("file" , "" , "File path to read ownership information from (- for stdin)" )
38- fileShortFlag = flagSet .String ("f" , "" , "File path to read ownership information from (- for stdin). Alias for -file" )
39-
40- apiFlags = api .NewFlags (flagSet )
41- )
42-
43- handler := func (args []string ) error {
44- if err := flagSet .Parse (args ); err != nil {
45- return err
46- }
47-
48- if * repoFlag == "" {
49- return errors .New ("provide a repo name using -repo" )
50- }
51-
52- if * fileFlag == "" && * fileShortFlag == "" {
53- return errors .New ("provide a file using -file" )
54- }
55- if * fileFlag != "" && * fileShortFlag != "" {
56- return errors .New ("have to provide either -file or -f" )
57- }
58- if * fileShortFlag != "" {
59- * fileFlag = * fileShortFlag
60- }
61-
62- file , err := readFile (* fileFlag )
24+ var codeownersCreateCommand = clicompat .Wrap (& cli.Command {
25+ Name : "create" ,
26+ Usage : "create a codeowners file" ,
27+ UsageText : "src codeowners create [options]" ,
28+ Description : codeownersCreateExamples ,
29+ HideVersion : true ,
30+ Flags : clicompat .WithAPIFlags (
31+ & cli.StringFlag {
32+ Name : "repo" ,
33+ Usage : "The repository to attach the data to" ,
34+ Required : true ,
35+ Validator : requiresNotEmpty ("provide a repo name using -repo" ),
36+ },
37+ & cli.StringFlag {
38+ Name : "file" ,
39+ Aliases : []string {"f" },
40+ Usage : "File path to read ownership information from (- for stdin)" ,
41+ TakesFile : true ,
42+ Required : true ,
43+ Validator : requiresNotEmpty ("provide a file using -file" ),
44+ },
45+ ),
46+ Action : func (ctx context.Context , cmd * cli.Command ) error {
47+ repoName := cmd .String ("repo" )
48+ fileName := cmd .String ("file" )
49+
50+ content , err := readFile (fileName )
6351 if err != nil {
6452 return err
6553 }
6654
67- content , err := io .ReadAll (file )
68- if err != nil {
69- return err
70- }
71-
72- client := cfg .apiClient (apiFlags , flagSet .Output ())
55+ client := cfg .apiClient (clicompat .APIFlagsFromCmd (cmd ), cmd .Writer )
7356
7457 query := `mutation CreateCodeownersFile(
7558 $repoName: String!,
@@ -89,14 +72,14 @@ Examples:
8972 AddCodeownersFile CodeownersIngestedFile
9073 }
9174 if ok , err := client .NewRequest (query , map [string ]any {
92- "repoName" : * repoFlag ,
75+ "repoName" : repoName ,
9376 "content" : string (content ),
94- }).Do (context . Background () , & result ); err != nil || ! ok {
77+ }).Do (ctx , & result ); err != nil || ! ok {
9578 var gqlErr api.GraphQlErrors
9679 if errors .As (err , & gqlErr ) {
9780 for _ , e := range gqlErr {
9881 if strings .Contains (e .Error (), "repo not found:" ) {
99- return cmderrors .ExitCode (2 , errors .Newf ("repository %q not found" , * repoFlag ))
82+ return cmderrors .ExitCode (2 , errors .Newf ("repository %q not found" , repoName ))
10083 }
10184 if strings .Contains (e .Error (), "codeowners file has already been ingested for this repository" ) {
10285 return cmderrors .ExitCode (2 , errors .New ("codeowners file has already been ingested for this repository" ))
@@ -107,12 +90,5 @@ Examples:
10790 }
10891
10992 return nil
110- }
111-
112- // Register the command.
113- codeownersCommands = append (codeownersCommands , & command {
114- flagSet : flagSet ,
115- handler : handler ,
116- usageFunc : usageFunc ,
117- })
118- }
93+ },
94+ })
0 commit comments