github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/cmd/detach-volume.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/sirupsen/logrus"
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/emc-advanced-dev/pkg/errors"
    10  	"github.com/solo-io/unik/pkg/client"
    11  )
    12  
    13  var detachCmd = &cobra.Command{
    14  	Use:     "detach-volume",
    15  	Aliases: []string{"detach"},
    16  	Short:   "Detach an attached volume from a stopped instance",
    17  	Long: `Detaches a volume to a stopped instance at a specified mount point.
    18  You specify the volume by name or id.
    19  
    20  After detaching the volume, the volume can be mounted to another instance.
    21  
    22  If the instance is not stopped, detach will result in an error.`,
    23  	Run: func(cmd *cobra.Command, args []string) {
    24  		if err := func() error {
    25  			if err := readClientConfig(); err != nil {
    26  				return err
    27  			}
    28  			if volumeName == "" {
    29  				return errors.New("must specify --volume", nil)
    30  			}
    31  			if host == "" {
    32  				host = clientConfig.Host
    33  			}
    34  			logrus.WithFields(logrus.Fields{"host": host, "volume": volumeName}).Info("detaching volume")
    35  			if err := client.UnikClient(host).Volumes().Detach(volumeName); err != nil {
    36  				return err
    37  			}
    38  			return nil
    39  		}(); err != nil {
    40  			logrus.Errorf("failed deleting volume: %v", err)
    41  			os.Exit(-1)
    42  		}
    43  	},
    44  }
    45  
    46  func init() {
    47  	RootCmd.AddCommand(detachCmd)
    48  	detachCmd.Flags().StringVar(&volumeName, "volume", "", "<string,required> name or id of volume to detach. unik accepts a prefix of the name or id")
    49  }