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

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/emc-advanced-dev/pkg/errors"
    12  	"github.com/solo-io/unik/pkg/client"
    13  )
    14  
    15  var describeInstanceCmd = &cobra.Command{
    16  	Use:   "describe-instance",
    17  	Short: "Get instance info as a Json string",
    18  	Long:  `Get a json representation of an instance as it is stored in unik.`,
    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 name == "" {
    28  				return errors.New("must specify --instance", nil)
    29  			}
    30  			instance, err := client.UnikClient(host).Instances().Get(name)
    31  			if err != nil {
    32  				return err
    33  			}
    34  			data, err := json.Marshal(instance)
    35  			if err != nil {
    36  				return err
    37  			}
    38  			fmt.Printf("%s\n", string(data))
    39  			return nil
    40  		}(); err != nil {
    41  			logrus.Errorf("failed describing instance: %v", err)
    42  			os.Exit(-1)
    43  		}
    44  	},
    45  }
    46  
    47  func init() {
    48  	RootCmd.AddCommand(describeInstanceCmd)
    49  	describeInstanceCmd.Flags().StringVar(&name, "instance", "", "<string,required> name or id of instance. unik accepts a prefix of the name or id")
    50  }