github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/cli/command/plugin/enable.go (about) 1 package plugin 2 3 import ( 4 "fmt" 5 6 "github.com/docker/docker/api/types" 7 "github.com/docker/docker/cli" 8 "github.com/docker/docker/cli/command" 9 "github.com/docker/docker/reference" 10 "github.com/spf13/cobra" 11 "golang.org/x/net/context" 12 ) 13 14 type enableOpts struct { 15 timeout int 16 name string 17 } 18 19 func newEnableCommand(dockerCli *command.DockerCli) *cobra.Command { 20 var opts enableOpts 21 22 cmd := &cobra.Command{ 23 Use: "enable [OPTIONS] PLUGIN", 24 Short: "Enable a plugin", 25 Args: cli.ExactArgs(1), 26 RunE: func(cmd *cobra.Command, args []string) error { 27 opts.name = args[0] 28 return runEnable(dockerCli, &opts) 29 }, 30 } 31 32 flags := cmd.Flags() 33 flags.IntVar(&opts.timeout, "timeout", 0, "HTTP client timeout (in seconds)") 34 return cmd 35 } 36 37 func runEnable(dockerCli *command.DockerCli, opts *enableOpts) error { 38 name := opts.name 39 40 named, err := reference.ParseNamed(name) // FIXME: validate 41 if err != nil { 42 return err 43 } 44 if reference.IsNameOnly(named) { 45 named = reference.WithDefaultTag(named) 46 } 47 ref, ok := named.(reference.NamedTagged) 48 if !ok { 49 return fmt.Errorf("invalid name: %s", named.String()) 50 } 51 if opts.timeout < 0 { 52 return fmt.Errorf("negative timeout %d is invalid", opts.timeout) 53 } 54 55 if err := dockerCli.Client().PluginEnable(context.Background(), ref.String(), types.PluginEnableOptions{Timeout: opts.timeout}); err != nil { 56 return err 57 } 58 fmt.Fprintln(dockerCli.Out(), name) 59 return nil 60 }