11package main
22
33import (
4+ "context"
45 "flag"
56 "fmt"
6- "os"
7- "os/exec"
8- "strings"
97
10- "text/template"
11-
12- "github.com/pkg/errors"
13- "github.com/sourcegraph/src-cli/internal/batches"
8+ "github.com/sourcegraph/src-cli/internal/api"
9+ "github.com/sourcegraph/src-cli/internal/batches/service"
10+ "github.com/sourcegraph/src-cli/internal/cmderrors"
1411)
1512
1613func init () {
@@ -30,51 +27,33 @@ Examples:
3027`
3128
3229 flagSet := flag .NewFlagSet ("new" , flag .ExitOnError )
30+ apiFlags := api .NewFlags (flagSet )
3331
3432 var (
3533 fileFlag = flagSet .String ("f" , "batch.yaml" , "The name of the batch spec file to create." )
3634 )
3735
3836 handler := func (args []string ) error {
37+ ctx := context .Background ()
38+
3939 if err := flagSet .Parse (args ); err != nil {
4040 return err
4141 }
4242
43- f , err := os .OpenFile (* fileFlag , os .O_WRONLY | os .O_CREATE | os .O_EXCL , 0644 )
44- if err != nil {
45- if os .IsExist (err ) {
46- return fmt .Errorf ("file %s already exists" , * fileFlag )
47- }
48- return errors .Wrapf (err , "failed to create file %s" , * fileFlag )
43+ if len (flagSet .Args ()) != 0 {
44+ return cmderrors .Usage ("additional arguments not allowed" )
4945 }
50- defer f .Close ()
5146
52- tmpl , err := template .New ("" ).Parse (batchSpecTmpl )
53- if err != nil {
54- return err
55- }
47+ svc := service .New (& service.Opts {
48+ Client : cfg .apiClient (apiFlags , flagSet .Output ()),
49+ })
5650
57- author := batches.GitCommitAuthor {
58- Name : "Sourcegraph" ,
59- 60- }
61-
62- // Try to get better default values from git, ignore any errors.
63- if err := checkExecutable ("git" , "version" ); err == nil {
64- var gitAuthorName , gitAuthorEmail string
65- var err1 , err2 error
66- gitAuthorName , err1 = getGitConfig ("user.name" )
67- gitAuthorEmail , err2 = getGitConfig ("user.email" )
68-
69- if err1 == nil && err2 == nil && gitAuthorName != "" && gitAuthorEmail != "" {
70- author .Name = gitAuthorName
71- author .Email = gitAuthorEmail
72- }
51+ if err := svc .DetermineFeatureFlags (ctx ); err != nil {
52+ return err
7353 }
7454
75- err = tmpl .Execute (f , map [string ]interface {}{"Author" : author })
76- if err != nil {
77- return errors .Wrap (err , "failed to write batch spec to file" )
55+ if err := svc .GenerateExampleSpec (ctx , * fileFlag ); err != nil {
56+ return err
7857 }
7958
8059 fmt .Printf ("%s created.\n " , * fileFlag )
@@ -92,46 +71,3 @@ Examples:
9271 },
9372 })
9473}
95-
96- func getGitConfig (attribute string ) (string , error ) {
97- cmd := exec .Command ("git" , "config" , "--get" , attribute )
98- out , err := cmd .CombinedOutput ()
99- if err != nil {
100- return "" , err
101- }
102- return strings .TrimSpace (string (out )), nil
103- }
104-
105- const batchSpecTmpl = `name: NAME-OF-YOUR-BATCH-CHANGE
106- description: DESCRIPTION-OF-YOUR-BATCH-CHANGE
107-
108- # "on" specifies on which repositories to execute the "steps".
109- on:
110- # Example: find all repositories that contain a README.md file.
111- - repositoriesMatchingQuery: file:README.md
112-
113- # "steps" are run in each repository. Each step is run in a Docker container
114- # with the repository as the working directory. Once complete, each
115- # repository's resulting diff is captured.
116- steps:
117- # Example: append "Hello World" to every README.md
118- - run: echo "Hello World" | tee -a $(find -name README.md)
119- container: alpine:3
120-
121- # "changesetTemplate" describes the changeset (e.g., GitHub pull request) that
122- # will be created for each repository.
123- changesetTemplate:
124- title: Hello World
125- body: This adds Hello World to the README
126-
127- branch: BRANCH-NAME-IN-EACH-REPOSITORY # Push the commit to this branch.
128-
129- commit:
130- author:
131- name: {{ .Author.Name }}
132- email: {{ .Author.Email }}
133- message: Append Hello World to all README.md files
134-
135- # Change published to true once you're ready to create changesets on the code host.
136- published: false
137- `
0 commit comments