github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/volume/update.go (about)

     1  package volume
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/completion"
     9  	"github.com/docker/docker/api/types/volume"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  	"github.com/spf13/pflag"
    13  )
    14  
    15  func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
    16  	var availability string
    17  
    18  	cmd := &cobra.Command{
    19  		Use:   "update [OPTIONS] [VOLUME]",
    20  		Short: "Update a volume (cluster volumes only)",
    21  		Args:  cli.RequiresMaxArgs(1),
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			return runUpdate(cmd.Context(), dockerCli, args[0], availability, cmd.Flags())
    24  		},
    25  		Annotations: map[string]string{
    26  			"version": "1.42",
    27  			"swarm":   "manager",
    28  		},
    29  		ValidArgsFunction: completion.VolumeNames(dockerCli),
    30  	}
    31  
    32  	flags := cmd.Flags()
    33  	flags.StringVar(&availability, "availability", "active", `Cluster Volume availability ("active", "pause", "drain")`)
    34  	flags.SetAnnotation("availability", "version", []string{"1.42"})
    35  	flags.SetAnnotation("availability", "swarm", []string{"manager"})
    36  
    37  	return cmd
    38  }
    39  
    40  func runUpdate(ctx context.Context, dockerCli command.Cli, volumeID, availability string, flags *pflag.FlagSet) error {
    41  	// TODO(dperny): For this earliest version, the only thing that can be
    42  	// updated is Availability, which is necessary because to delete a cluster
    43  	// volume, the availability must first be set to "drain"
    44  
    45  	apiClient := dockerCli.Client()
    46  
    47  	vol, _, err := apiClient.VolumeInspectWithRaw(ctx, volumeID)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	if vol.ClusterVolume == nil {
    53  		return errors.New("Can only update cluster volumes")
    54  	}
    55  
    56  	if flags.Changed("availability") {
    57  		vol.ClusterVolume.Spec.Availability = volume.Availability(availability)
    58  	}
    59  
    60  	return apiClient.VolumeUpdate(
    61  		ctx, vol.ClusterVolume.ID, vol.ClusterVolume.Version,
    62  		volume.UpdateOptions{
    63  			Spec: &vol.ClusterVolume.Spec,
    64  		},
    65  	)
    66  }