Skip to content

Commit f3a7d00

Browse files
authored
Merge pull request cli#4028 from cli/bump-cobra
Upgrade Cobra for improved shell completion support
2 parents fdad37e + 2801206 commit f3a7d00

File tree

8 files changed

+426
-108
lines changed

8 files changed

+426
-108
lines changed

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/cpuguy83/go-md2man/v2 v2.0.0
1414
github.com/creack/pty v1.1.13
1515
github.com/gabriel-vasile/mimetype v1.1.2
16-
github.com/google/go-cmp v0.5.4
16+
github.com/google/go-cmp v0.5.5
1717
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1818
github.com/hashicorp/go-version v1.2.1
1919
github.com/henvic/httpretty v0.0.6
@@ -28,14 +28,14 @@ require (
2828
github.com/rivo/uniseg v0.2.0
2929
github.com/shurcooL/githubv4 v0.0.0-20200928013246-d292edc3691b
3030
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f
31-
github.com/spf13/cobra v1.1.3
31+
github.com/spf13/cobra v1.2.1
3232
github.com/spf13/pflag v1.0.5
33-
github.com/stretchr/testify v1.6.1
33+
github.com/stretchr/objx v0.1.1 // indirect
34+
github.com/stretchr/testify v1.7.0
3435
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
35-
golang.org/x/sync v0.0.0-20190423024810-112230192c58
36+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
3637
golang.org/x/sys v0.0.0-20210601080250-7ecdf8ef093b
3738
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
38-
golang.org/x/text v0.3.4 // indirect
3939
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
4040
)
4141

go.sum

Lines changed: 354 additions & 87 deletions
Large diffs are not rendered by default.

pkg/cmd/completion/completion.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ func NewCmdCompletion(io *iostreams.IOStreams) *cobra.Command {
2929
3030
### bash
3131
32-
Add this to your %[1]s~/.bash_profile%[1]s:
32+
First, ensure that you install %[1]sbash-completion%[1]s using your package manager.
33+
34+
After, add this to your %[1]s~/.bash_profile%[1]s:
3335
3436
eval "$(gh completion -s bash)"
3537
@@ -65,7 +67,7 @@ func NewCmdCompletion(io *iostreams.IOStreams) *cobra.Command {
6567

6668
switch shellType {
6769
case "bash":
68-
return rootCmd.GenBashCompletion(w)
70+
return rootCmd.GenBashCompletionV2(w, true)
6971
case "zsh":
7072
return rootCmd.GenZshCompletion(w)
7173
case "powershell":

pkg/cmd/issue/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
8383
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
8484
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|all}")
8585
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
86-
return []string{"open", "closed", "all"}, cobra.ShellCompDirectiveNoSpace
86+
return []string{"open", "closed", "all"}, cobra.ShellCompDirectiveNoFileComp
8787
})
8888
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of issues to fetch")
8989
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")

pkg/cmd/pr/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
8585
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of items to fetch")
8686
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|merged|all}")
8787
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
88-
return []string{"open", "closed", "merged", "all"}, cobra.ShellCompDirectiveNoSpace
88+
return []string{"open", "closed", "merged", "all"}, cobra.ShellCompDirectiveNoFileComp
8989
})
9090
cmd.Flags().StringVarP(&opts.BaseBranch, "base", "B", "", "Filter by base branch")
9191
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")

pkg/cmd/repo/create/create.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,51 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
135135
cmd.Flags().BoolVarP(&opts.ConfirmSubmit, "confirm", "y", false, "Skip the confirmation prompt")
136136
cmd.Flags().StringVarP(&opts.GitIgnoreTemplate, "gitignore", "g", "", "Specify a gitignore template for the repository")
137137
cmd.Flags().StringVarP(&opts.LicenseTemplate, "license", "l", "", "Specify an Open Source License for the repository")
138+
139+
_ = cmd.RegisterFlagCompletionFunc("gitignore", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
140+
httpClient, err := opts.HttpClient()
141+
if err != nil {
142+
return nil, cobra.ShellCompDirectiveError
143+
}
144+
cfg, err := opts.Config()
145+
if err != nil {
146+
return nil, cobra.ShellCompDirectiveError
147+
}
148+
hostname, err := cfg.DefaultHost()
149+
if err != nil {
150+
return nil, cobra.ShellCompDirectiveError
151+
}
152+
results, err := listGitIgnoreTemplates(httpClient, hostname)
153+
if err != nil {
154+
return nil, cobra.ShellCompDirectiveError
155+
}
156+
return results, cobra.ShellCompDirectiveNoFileComp
157+
})
158+
159+
_ = cmd.RegisterFlagCompletionFunc("license", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
160+
httpClient, err := opts.HttpClient()
161+
if err != nil {
162+
return nil, cobra.ShellCompDirectiveError
163+
}
164+
cfg, err := opts.Config()
165+
if err != nil {
166+
return nil, cobra.ShellCompDirectiveError
167+
}
168+
hostname, err := cfg.DefaultHost()
169+
if err != nil {
170+
return nil, cobra.ShellCompDirectiveError
171+
}
172+
licenses, err := listLicenseTemplates(httpClient, hostname)
173+
if err != nil {
174+
return nil, cobra.ShellCompDirectiveError
175+
}
176+
var results []string
177+
for _, license := range licenses {
178+
results = append(results, fmt.Sprintf("%s\t%s", license.Key, license.Name))
179+
}
180+
return results, cobra.ShellCompDirectiveNoFileComp
181+
})
182+
138183
return cmd
139184
}
140185

@@ -225,14 +270,14 @@ func createRun(opts *CreateOptions) error {
225270
// is passed, or when the confirm flag is set.
226271
if opts.Template == "" && opts.IO.CanPrompt() && !opts.ConfirmSubmit {
227272
if gitIgnoreTemplate == "" {
228-
gt, err := interactiveGitIgnore(api.NewClientFromHTTP(httpClient), host)
273+
gt, err := interactiveGitIgnore(httpClient, host)
229274
if err != nil {
230275
return err
231276
}
232277
gitIgnoreTemplate = gt
233278
}
234279
if repoLicenseTemplate == "" {
235-
lt, err := interactiveLicense(api.NewClientFromHTTP(httpClient), host)
280+
lt, err := interactiveLicense(httpClient, host)
236281
if err != nil {
237282
return err
238283
}
@@ -384,7 +429,7 @@ func createRun(opts *CreateOptions) error {
384429
return nil
385430
}
386431

387-
func interactiveGitIgnore(client *api.Client, hostname string) (string, error) {
432+
func interactiveGitIgnore(client *http.Client, hostname string) (string, error) {
388433

389434
var addGitIgnore bool
390435
var addGitIgnoreSurvey []*survey.Question
@@ -408,7 +453,7 @@ func interactiveGitIgnore(client *api.Client, hostname string) (string, error) {
408453
if addGitIgnore {
409454
var gitIg []*survey.Question
410455

411-
gitIgnoretemplates, err := ListGitIgnoreTemplates(client, hostname)
456+
gitIgnoretemplates, err := listGitIgnoreTemplates(client, hostname)
412457
if err != nil {
413458
return "", err
414459
}
@@ -429,7 +474,7 @@ func interactiveGitIgnore(client *api.Client, hostname string) (string, error) {
429474
return wantedIgnoreTemplate, nil
430475
}
431476

432-
func interactiveLicense(client *api.Client, hostname string) (string, error) {
477+
func interactiveLicense(client *http.Client, hostname string) (string, error) {
433478
var addLicense bool
434479
var addLicenseSurvey []*survey.Question
435480
var wantedLicense string
@@ -451,7 +496,7 @@ func interactiveLicense(client *api.Client, hostname string) (string, error) {
451496
licenseKey := map[string]string{}
452497

453498
if addLicense {
454-
licenseTemplates, err := ListLicenseTemplates(client, hostname)
499+
licenseTemplates, err := listLicenseTemplates(client, hostname)
455500
if err != nil {
456501
return "", err
457502
}

pkg/cmd/repo/create/http.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,21 @@ func resolveOrganizationTeam(client *api.Client, hostname, orgName, teamSlug str
230230
return &response, err
231231
}
232232

233-
// ListGitIgnoreTemplates uses API v3 here because gitignore template isn't supported by GraphQL yet.
234-
func ListGitIgnoreTemplates(client *api.Client, hostname string) ([]string, error) {
233+
// listGitIgnoreTemplates uses API v3 here because gitignore template isn't supported by GraphQL yet.
234+
func listGitIgnoreTemplates(httpClient *http.Client, hostname string) ([]string, error) {
235235
var gitIgnoreTemplates []string
236+
client := api.NewClientFromHTTP(httpClient)
236237
err := client.REST(hostname, "GET", "gitignore/templates", nil, &gitIgnoreTemplates)
237238
if err != nil {
238239
return []string{}, err
239240
}
240241
return gitIgnoreTemplates, nil
241242
}
242243

243-
// ListLicenseTemplates uses API v3 here because license template isn't supported by GraphQL yet.
244-
func ListLicenseTemplates(client *api.Client, hostname string) ([]api.License, error) {
244+
// listLicenseTemplates uses API v3 here because license template isn't supported by GraphQL yet.
245+
func listLicenseTemplates(httpClient *http.Client, hostname string) ([]api.License, error) {
245246
var licenseTemplates []api.License
247+
client := api.NewClientFromHTTP(httpClient)
246248
err := client.REST(hostname, "GET", "licenses", nil, &licenseTemplates)
247249
if err != nil {
248250
return nil, err

pkg/cmdutil/json_flags.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ func AddJSONFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) {
2929

3030
_ = cmd.RegisterFlagCompletionFunc("json", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
3131
var results []string
32-
if idx := strings.IndexRune(toComplete, ','); idx >= 0 {
32+
var prefix string
33+
if idx := strings.LastIndexByte(toComplete, ','); idx >= 0 {
34+
prefix = toComplete[:idx+1]
3335
toComplete = toComplete[idx+1:]
3436
}
3537
toComplete = strings.ToLower(toComplete)
3638
for _, f := range fields {
3739
if strings.HasPrefix(strings.ToLower(f), toComplete) {
38-
results = append(results, f)
40+
results = append(results, prefix+f)
3941
}
4042
}
4143
sort.Strings(results)

0 commit comments

Comments
 (0)