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