-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathbatch_repositories.go
More file actions
184 lines (153 loc) · 4.46 KB
/
batch_repositories.go
File metadata and controls
184 lines (153 loc) · 4.46 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"context"
"flag"
"fmt"
cliLog "log"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"
"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/batches"
"github.com/sourcegraph/src-cli/internal/batches/graphql"
"github.com/sourcegraph/src-cli/internal/batches/service"
"github.com/sourcegraph/src-cli/internal/batches/ui"
)
func init() {
usage := `
'src batch repositories' works out the repositories that a batch spec would
apply to.
Usage:
src batch repositories [-f] FILE
Examples:
$ src batch repositories batch.spec.yaml
$ src batch repositories -f batch.spec.yaml
`
flagSet := flag.NewFlagSet("repositories", flag.ExitOnError)
var (
fileFlag = flagSet.String("f", "", "The batch spec file to read, or - to read from standard input.")
apiFlags = api.NewFlags(flagSet)
)
var (
allowUnsupported bool
allowIgnored bool
skipErrors bool
)
flagSet.BoolVar(
&allowUnsupported, "allow-unsupported", false,
"Allow unsupported code hosts.",
)
flagSet.BoolVar(
&allowIgnored, "force-override-ignore", false,
"Do not ignore repositories that have a .batchignore file.",
)
flagSet.BoolVar(
&skipErrors, "skip-errors", false,
"If true, errors encountered won't stop the program, but only log them.",
)
handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
file, err := getBatchSpecFile(flagSet, fileFlag)
if err != nil {
return err
}
ctx := context.Background()
client := cfg.apiClient(apiFlags, flagSet.Output())
svc := service.New(&service.Opts{
Client: client,
})
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx, skipErrors)
if err != nil {
return err
}
if err := validateSourcegraphVersionConstraint(ffs); err != nil {
if !skipErrors {
return err
} else {
cliLog.Printf("WARNING: %s", err)
}
}
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})
spec, _, _, err := parseBatchSpec(ctx, file, svc)
if err != nil {
ui := &ui.TUI{Out: out}
ui.ParsingBatchSpecFailure(err)
return err
}
queryTmpl, err := parseTemplate(batchRepositoriesTemplate)
if err != nil {
return err
}
totalTmpl, err := parseTemplate(batchRepositoriesTotalTemplate)
if err != nil {
return err
}
_, repos, err := svc.ResolveWorkspacesForBatchSpec(ctx, spec, allowUnsupported, allowIgnored)
if err != nil {
if _, ok := err.(batches.UnsupportedRepoSet); ok {
// This is fine, we just ignore those in the output.
} else if _, ok := err.(batches.IgnoredRepoSet); ok {
// This is fine, we just ignore those in the output.
} else {
return errors.Wrap(err, "resolving repositories")
}
}
repoCount := 0
max := 0
for _, repo := range repos {
if len(repo.Name) > max {
max = len(repo.Name)
}
repoCount++
}
if err := execTemplate(queryTmpl, batchRepositoryTemplateInput{
Max: max,
RepoCount: len(repos),
Repos: repos,
SourcegraphEndpoint: cfg.endpointURL.String(),
}); err != nil {
return err
}
return execTemplate(totalTmpl, batchRepositoryTemplateInput{
RepoCount: repoCount,
})
}
batchCommands = append(batchCommands, &command{
flagSet: flagSet,
aliases: []string{"repos"},
handler: handler,
usageFunc: func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src batch %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
},
})
}
const batchRepositoriesTemplate = `
{{- range .Repos -}}
{{- " "}}{{ color "success" }}{{ padRight .Name $.Max " " }}{{ color "nc" -}}
{{- if ne (len .Branch.Name) 0 -}}{{ " " }}{{- color "search-branch" -}}{{- .Branch.Name -}}{{ color "nc" -}}{{- end -}}
{{- color "search-border"}}{{" ("}}{{color "nc" -}}
{{- color "search-repository"}}{{$.SourcegraphEndpoint}}{{.URL}}{{color "nc" -}}
{{- color "search-border"}}{{")\n"}}{{color "nc" -}}
{{- end -}}
`
const batchRepositoriesTotalTemplate = `
{{- color "logo" -}}✱{{- color "nc" -}}
{{- " " -}}
{{- if eq .RepoCount 0 -}}
{{- color "warning" -}}
{{- else -}}
{{- color "success" -}}
{{- end -}}
{{- .RepoCount }} workspace{{ if ne .RepoCount 1 }}s{{ end }} total
{{- color "nc" -}}
`
type batchRepositoryTemplateInput struct {
Max int
Query string
RepoCount int
Repos []*graphql.Repository
SourcegraphEndpoint string
}