Skip to content

Commit

Permalink
Merge pull request #10878 from relrelb/profiles_completion
Browse files Browse the repository at this point in the history
Add shell completion for `--profile`
  • Loading branch information
glours committed Aug 10, 2023
2 parents 5bbdf3d + 0345461 commit 0511b0c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/compose/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package compose

import (
"sort"
"strings"

"github.com/docker/compose/v2/pkg/api"
Expand Down Expand Up @@ -65,3 +66,23 @@ func completeProjectNames(backend api.Service) func(cmd *cobra.Command, args []s
return values, cobra.ShellCompDirectiveNoFileComp
}
}

func completeProfileNames(p *ProjectOptions) validArgsFn {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
project, err := p.ToProject(nil)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

allProfileNames := project.AllServices().GetProfiles()
sort.Strings(allProfileNames)

var values []string
for _, profileName := range allProfileNames {
if strings.HasPrefix(profileName, toComplete) {
values = append(values, profileName)
}
}
return values, cobra.ShellCompDirectiveNoFileComp
}
}
4 changes: 4 additions & 0 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
return []string{"yaml", "yml"}, cobra.ShellCompDirectiveFilterFileExt
},
)
c.RegisterFlagCompletionFunc( //nolint:errcheck
"profile",
completeProfileNames(&opts),
)

c.Flags().StringVar(&progress, "progress", buildx.PrinterModeAuto, fmt.Sprintf(`Set type of progress output (%s)`, strings.Join(printerModes, ", ")))

Expand Down

0 comments on commit 0511b0c

Please sign in to comment.