github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/cmd/delete-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 volumeName string
    14  
    15  var rmvCmd = &cobra.Command{
    16  	Use:     "delete-volume",
    17  	Aliases: []string{"rmv"},
    18  	Short:   "Delete a unikernel volume",
    19  	Long: `Deletes a volume.
    20  You may specify the volume by name or id.`,
    21  	Run: func(cmd *cobra.Command, args []string) {
    22  		if err := func() error {
    23  			if err := readClientConfig(); err != nil {
    24  				return err
    25  			}
    26  			if volumeName == "" {
    27  				return errors.New("must specify --volume", nil)
    28  			}
    29  			if host == "" {
    30  				host = clientConfig.Host
    31  			}
    32  			logrus.WithFields(logrus.Fields{"host": host, "force": force, "volume": volumeName}).Info("deleting volume")
    33  			if err := client.UnikClient(host).Volumes().Delete(volumeName, force); err != nil {
    34  				return err
    35  			}
    36  			return nil
    37  		}(); err != nil {
    38  			logrus.Errorf("failed deleting volume: %v", err)
    39  			os.Exit(-1)
    40  		}
    41  	},
    42  }
    43  
    44  func init() {
    45  	RootCmd.AddCommand(rmvCmd)
    46  	rmvCmd.Flags().StringVar(&volumeName, "volume", "", "<string,required> name or id of volume. unik accepts a prefix of the name or id")
    47  	rmvCmd.Flags().BoolVar(&force, "force", false, "<bool, optional> forces detaching the volume before deletion if it is currently attached")
    48  }