Skip to content

Commit

Permalink
demonstrate Cobra completion V2 support
Browse files Browse the repository at this point in the history
(see docker/cli#3429)

Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Feb 25, 2022
1 parent f7a6c3b commit 46d01a1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
29 changes: 27 additions & 2 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ import (
"github.com/compose-spec/compose-go/types"
dockercli "github.com/docker/cli/cli"
"github.com/docker/cli/cli-plugins/manager"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/morikuni/aec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
"github.com/docker/compose/v2/pkg/utils"
)

// Command defines a compose CLI command as a func with args
Expand Down Expand Up @@ -218,7 +219,8 @@ const PluginName = "compose"

// RunningAsStandalone detects when running as a standalone program
func RunningAsStandalone() bool {
return len(os.Args) < 2 || os.Args[1] != manager.MetadataSubcommandName && os.Args[1] != PluginName
rootCommands := []string{manager.MetadataSubcommandName, cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd}
return len(os.Args) < 2 || !utils.StringContains(rootCommands, os.Args[1])
}

// RootCommand returns the compose command with its child commands
Expand Down Expand Up @@ -310,6 +312,29 @@ func RootCommand(backend api.Service) *cobra.Command {
)
command.Flags().SetInterspersed(false)
opts.addProjectFlags(command.Flags())
command.RegisterFlagCompletionFunc(
"project-name",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, err := backend.List(cmd.Context(), api.ListOptions{
All: true,
})
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var values []string
for _, stack := range list {
values = append(values, stack.Name)
}
return values, cobra.ShellCompDirectiveNoFileComp
},
)
command.RegisterFlagCompletionFunc(
"file",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"yaml", "yml"}, cobra.ShellCompDirectiveFilterFileExt
},
)

command.Flags().StringVar(&ansi, "ansi", "auto", `Control when to print ANSI control characters ("never"|"always"|"auto")`)
command.Flags().BoolVarP(&version, "version", "v", false, "Show the Docker Compose version information")
command.Flags().MarkHidden("version") //nolint:errcheck
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ func pluginMain() {
return cmd
},
manager.Metadata{
SchemaVersion: "0.1.0",
SchemaVersion: "0.2.0",
Vendor: "Docker Inc.",
Version: internal.Version,
Completions: true,
})
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ require (

// (for buildx)
replace (
github.com/docker/cli => github.com/docker/cli v20.10.3-0.20210702143511-f782d1355eff+incompatible
github.com/docker/cli => github.com/ndeloof/cli v0.0.0-20220225144918-5a64693e331f
github.com/docker/docker => github.com/docker/docker v20.10.3-0.20220121014307-40bb9831756f+incompatible
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc => github.com/tonistiigi/opentelemetry-go-contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.0.0-20210714055410-d010b05b4939
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace => github.com/tonistiigi/opentelemetry-go-contrib/instrumentation/net/http/httptrace/otelhttptrace v0.0.0-20210714055410-d010b05b4939
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e/go.mo
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/docker/buildx v0.7.1 h1:l2DlW8YDbB3pH2bUFY5Q9pPQdhd42wqlnO5hoyWrjYM=
github.com/docker/buildx v0.7.1/go.mod h1:PzxALHhYWPNhw/8JajPOJBkvx1w2tgOnppL4ESg0wOY=
github.com/docker/cli v20.10.3-0.20210702143511-f782d1355eff+incompatible h1:CaaxCD/l9Dxogu6lxf7AQautlv3sHULrasPadayp0fM=
github.com/docker/cli v20.10.3-0.20210702143511-f782d1355eff+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli-docs-tool v0.2.1 h1:ffZhhdws6kE+dCKHLMlYROZz8z01RtWbz+g4xz0eBIU=
github.com/docker/cli-docs-tool v0.2.1/go.mod h1:rgW5KKdNpLMBIuH4WQ/1RNh38nH+/Ay5jgL4P0ZMPpY=
github.com/docker/compose-on-kubernetes v0.4.19-0.20190128150448-356b2919c496/go.mod h1:iT2pYfi580XlpaV4KmK0T6+4/9+XoKmk/fhoDod1emE=
Expand Down Expand Up @@ -1145,6 +1143,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+
github.com/nakabonne/nestif v0.3.0/go.mod h1:dI314BppzXjJ4HsCnbo7XzrJHPszZsjnk5wEBSYHI2c=
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
github.com/ndeloof/cli v0.0.0-20220225144918-5a64693e331f h1:dMT6eSYpTfaglXvWZrpEeRdfxwNJq23vmY2mdwuWl7s=
github.com/ndeloof/cli v0.0.0-20220225144918-5a64693e331f/go.mod h1:wxmb4t3v3ZwISBr3B/B631+3VJe+erAxkl3vXQwXj2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down
15 changes: 8 additions & 7 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,14 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op
if len(opts) == 0 {
return nil, nil
}
serverInfo, err := s.serverInfo(ctx)
if err != nil {
return nil, err
}
if buildkitEnabled, err := command.BuildKitEnabled(serverInfo); err != nil || !buildkitEnabled {
return s.doBuildClassic(ctx, opts)
}
// TODO depend on https://github.com/docker/cli/pull/3435 & https://github.com/docker/compose/pull/9198
//serverInfo, err := s.serverInfo(ctx)
//if err != nil {
// return nil, err
//}
//if buildkitEnabled, err := command.BuildKitEnabled(serverInfo); err != nil || !buildkitEnabled {
// return s.doBuildClassic(ctx, opts)
//}
return s.doBuildBuildkit(ctx, project, opts, mode)
}

Expand Down

0 comments on commit 46d01a1

Please sign in to comment.