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