Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 3d83989

Browse files
authored
Merge pull request #1773 from ulyssessouza/completion
2 parents 1b76c74 + c28aec2 commit 3d83989

26 files changed

+91
-18
lines changed

cli/main.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ var (
6868
"serve": {},
6969
"version": {},
7070
"backend-metadata": {},
71+
// Special hidden commands used by cobra for completion
72+
"__complete": {},
73+
"__completeNoDesc": {},
7174
}
7275
unknownCommandRegexp = regexp.MustCompile(`unknown docker command: "([^"]*)"`)
7376
)
@@ -167,7 +170,7 @@ func main() {
167170
})
168171

169172
// populate the opts with the global flags
170-
flags.Parse(os.Args[1:]) //nolint: errcheck
173+
flags.Parse(os.Args[1:]) // nolint: errcheck
171174

172175
level, err := logrus.ParseLevel(opts.LogLevel)
173176
if err != nil {
@@ -223,18 +226,16 @@ func main() {
223226
volume.Command(ctype),
224227
)
225228

226-
if ctype != store.DefaultContextType {
227-
// On default context, "compose" is implemented by CLI Plugin
228-
proxy := api.NewServiceProxy().WithService(service.ComposeService())
229-
command := compose2.RootCommand(ctype, proxy)
229+
// On default context, "compose" is implemented by CLI Plugin
230+
proxy := api.NewServiceProxy().WithService(service.ComposeService())
231+
command := compose2.RootCommand(ctype, proxy)
230232

231-
if ctype == store.AciContextType {
232-
customizeCliForACI(command, proxy)
233-
}
234-
235-
root.AddCommand(command)
233+
if ctype == store.AciContextType {
234+
customizeCliForACI(command, proxy)
236235
}
237236

237+
root.AddCommand(command)
238+
238239
if err = root.ExecuteContext(ctx); err != nil {
239240
handleError(ctx, err, ctype, currentContext, cc, root)
240241
}

cmd/compose/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func buildCommand(p *projectOptions, backend api.Service) *cobra.Command {
6161
RunE: Adapt(func(ctx context.Context, args []string) error {
6262
return runBuild(ctx, backend, opts, args)
6363
}),
64+
ValidArgsFunction: serviceCompletion(p),
6465
}
6566
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT")
6667
cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")

cmd/compose/completion.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"strings"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// validArgsFn defines a completion func to be returned to fetch completion options
26+
type validArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
27+
28+
func noCompletion() validArgsFn {
29+
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
30+
return nil, cobra.ShellCompDirectiveNoFileComp
31+
}
32+
}
33+
34+
func serviceCompletion(p *projectOptions) validArgsFn {
35+
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
36+
project, err := p.toProject(nil)
37+
if err != nil {
38+
return nil, cobra.ShellCompDirectiveNoFileComp
39+
}
40+
var serviceNames []string
41+
for _, s := range project.ServiceNames() {
42+
if toComplete == "" || strings.HasPrefix(s, toComplete) {
43+
serviceNames = append(serviceNames, s)
44+
}
45+
}
46+
return serviceNames, cobra.ShellCompDirectiveNoFileComp
47+
}
48+
}

cmd/compose/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ import (
4141
"github.com/docker/compose-cli/pkg/compose"
4242
)
4343

44-
//Command defines a compose CLI command as a func with args
44+
// Command defines a compose CLI command as a func with args
4545
type Command func(context.Context, []string) error
4646

47-
//Adapt a Command func to cobra library
47+
// Adapt a Command func to cobra library
4848
func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
4949
return func(cmd *cobra.Command, args []string) error {
5050
ctx := cmd.Context()

cmd/compose/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func convertCommand(p *projectOptions, backend api.Service) *cobra.Command {
8686

8787
return runConvert(ctx, backend, opts, args)
8888
}),
89+
ValidArgsFunction: serviceCompletion(p),
8990
}
9091
flags := cmd.Flags()
9192
flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")

cmd/compose/cp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func copyCommand(p *projectOptions, backend api.Service) *cobra.Command {
6060
opts.destination = args[1]
6161
return runCopy(ctx, backend, opts)
6262
}),
63+
ValidArgsFunction: serviceCompletion(p),
6364
}
6465

6566
flags := copyCmd.Flags()

cmd/compose/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func createCommand(p *projectOptions, backend api.Service) *cobra.Command {
6464
QuietPull: false,
6565
})
6666
}),
67+
ValidArgsFunction: serviceCompletion(p),
6768
}
6869
flags := cmd.Flags()
6970
flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")

cmd/compose/down.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
5757
RunE: Adapt(func(ctx context.Context, args []string) error {
5858
return runDown(ctx, backend, opts)
5959
}),
60+
ValidArgsFunction: noCompletion(),
6061
}
6162
flags := downCmd.Flags()
6263
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")

cmd/compose/events.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func eventsCommand(p *projectOptions, backend api.Service) *cobra.Command {
4343
RunE: Adapt(func(ctx context.Context, args []string) error {
4444
return runEvents(ctx, backend, opts, args)
4545
}),
46+
ValidArgsFunction: serviceCompletion(p),
4647
}
4748

4849
cmd.Flags().BoolVar(&opts.json, "json", false, "Output events as a stream of json objects")

cmd/compose/exec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func execCommand(p *projectOptions, backend api.Service) *cobra.Command {
6161
RunE: Adapt(func(ctx context.Context, args []string) error {
6262
return runExec(ctx, backend, opts)
6363
}),
64+
ValidArgsFunction: serviceCompletion(p),
6465
}
6566

6667
runCmd.Flags().BoolVarP(&opts.detach, "detach", "d", false, "Detached mode: Run command in the background.")

0 commit comments

Comments
 (0)