github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/plugin/enable.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/cli/cli"
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type enableOpts struct {
    15  	timeout int
    16  	name    string
    17  }
    18  
    19  func newEnableCommand(dockerCli command.Cli) *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(cmd.Context(), dockerCli, &opts)
    29  		},
    30  	}
    31  
    32  	flags := cmd.Flags()
    33  	flags.IntVar(&opts.timeout, "timeout", 30, "HTTP client timeout (in seconds)")
    34  	return cmd
    35  }
    36  
    37  func runEnable(ctx context.Context, dockerCli command.Cli, opts *enableOpts) error {
    38  	name := opts.name
    39  	if opts.timeout < 0 {
    40  		return errors.Errorf("negative timeout %d is invalid", opts.timeout)
    41  	}
    42  
    43  	if err := dockerCli.Client().PluginEnable(ctx, name, types.PluginEnableOptions{Timeout: opts.timeout}); err != nil {
    44  		return err
    45  	}
    46  	fmt.Fprintln(dockerCli.Out(), name)
    47  	return nil
    48  }