Skip to content

Commit 0511b0c

Browse files
authored
Merge pull request docker#10878 from relrelb/profiles_completion
Add shell completion for `--profile`
2 parents 5bbdf3d + 0345461 commit 0511b0c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cmd/compose/completion.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package compose
1818

1919
import (
20+
"sort"
2021
"strings"
2122

2223
"github.com/docker/compose/v2/pkg/api"
@@ -65,3 +66,23 @@ func completeProjectNames(backend api.Service) func(cmd *cobra.Command, args []s
6566
return values, cobra.ShellCompDirectiveNoFileComp
6667
}
6768
}
69+
70+
func completeProfileNames(p *ProjectOptions) validArgsFn {
71+
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
72+
project, err := p.ToProject(nil)
73+
if err != nil {
74+
return nil, cobra.ShellCompDirectiveNoFileComp
75+
}
76+
77+
allProfileNames := project.AllServices().GetProfiles()
78+
sort.Strings(allProfileNames)
79+
80+
var values []string
81+
for _, profileName := range allProfileNames {
82+
if strings.HasPrefix(profileName, toComplete) {
83+
values = append(values, profileName)
84+
}
85+
}
86+
return values, cobra.ShellCompDirectiveNoFileComp
87+
}
88+
}

cmd/compose/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
454454
return []string{"yaml", "yml"}, cobra.ShellCompDirectiveFilterFileExt
455455
},
456456
)
457+
c.RegisterFlagCompletionFunc( //nolint:errcheck
458+
"profile",
459+
completeProfileNames(&opts),
460+
)
457461

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

0 commit comments

Comments
 (0)