浏览代码

Add completion to "compose ps"

Signed-off-by: Ulysses Souza <[email protected]>
Ulysses Souza 4 年之前
父节点
当前提交
37961c51ca
共有 4 个文件被更改,包括 25 次插入10 次删除
  1. 11 10
      cli/main.go
  2. 0 0
      cmd/compose/completion.go
  3. 3 0
      cmd/compose/compose.go
  4. 11 0
      cmd/compose/ps.go

+ 11 - 10
cli/main.go

@@ -68,6 +68,9 @@ var (
 		"serve":            {},
 		"version":          {},
 		"backend-metadata": {},
+		// Special hidden commands used by cobra for completion
+		"__complete":       {},
+		"__completeNoDesc": {},
 	}
 	unknownCommandRegexp = regexp.MustCompile(`unknown docker command: "([^"]*)"`)
 )
@@ -167,7 +170,7 @@ func main() {
 	})
 
 	// populate the opts with the global flags
-	flags.Parse(os.Args[1:]) //nolint: errcheck
+	flags.Parse(os.Args[1:]) // nolint: errcheck
 
 	level, err := logrus.ParseLevel(opts.LogLevel)
 	if err != nil {
@@ -223,18 +226,16 @@ func main() {
 		volume.Command(ctype),
 	)
 
-	if ctype != store.DefaultContextType {
-		// On default context, "compose" is implemented by CLI Plugin
-		proxy := api.NewServiceProxy().WithService(service.ComposeService())
-		command := compose2.RootCommand(ctype, proxy)
+	// On default context, "compose" is implemented by CLI Plugin
+	proxy := api.NewServiceProxy().WithService(service.ComposeService())
+	command := compose2.RootCommand(ctype, proxy)
 
-		if ctype == store.AciContextType {
-			customizeCliForACI(command, proxy)
-		}
-
-		root.AddCommand(command)
+	if ctype == store.AciContextType {
+		customizeCliForACI(command, proxy)
 	}
 
+	root.AddCommand(command)
+
 	if err = root.ExecuteContext(ctx); err != nil {
 		handleError(ctx, err, ctype, currentContext, cc, root)
 	}

+ 0 - 0
cli/cmd/compose/completion.go → cmd/compose/completion.go


+ 3 - 0
cmd/compose/compose.go

@@ -44,6 +44,9 @@ import (
 // Command defines a compose CLI command as a func with args
 type Command func(context.Context, []string) error
 
+// ValidArgsFn defines a completion func to be returned to fetch completion options
+type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
+
 // Adapt a Command func to cobra library
 func Adapt(fn Command) func(cmd *cobra.Command, args []string) error {
 	return func(cmd *cobra.Command, args []string) error {

+ 11 - 0
cmd/compose/ps.go

@@ -76,6 +76,7 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
 		RunE: Adapt(func(ctx context.Context, args []string) error {
 			return runPs(ctx, backend, args, opts)
 		}),
+		ValidArgsFunction: psCompletion(p),
 	}
 	flags := cmd.Flags()
 	flags.StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json]")
@@ -184,3 +185,13 @@ func filterByStatus(containers []api.ContainerSummary, status string) []api.Cont
 	}
 	return filtered
 }
+
+func psCompletion(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
+		}
+		return project.ServiceNames(), cobra.ShellCompDirectiveNoFileComp
+	}
+}