Skip to content

Commit 0345461

Browse files
committed
Add shell completion for --profile
Signed-off-by: Ariel Bachar <[email protected]> Signed-off-by: relrelb <[email protected]>
1 parent 80856ea commit 0345461

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
@@ -448,6 +448,10 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
448448
return []string{"yaml", "yml"}, cobra.ShellCompDirectiveFilterFileExt
449449
},
450450
)
451+
c.RegisterFlagCompletionFunc( //nolint:errcheck
452+
"profile",
453+
completeProfileNames(&opts),
454+
)
451455

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

0 commit comments

Comments
 (0)