github.com/solo-io/unik@v0.0.0-20190717152701-a58d3e8e33b7/cmd/describe-image.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 describeImageCmd = &cobra.Command{
    16  	Use:   "describe-image",
    17  	Short: "Get image info as a Json string",
    18  	Long:  `Get a json representation of an image 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 --image", nil)
    29  			}
    30  			image, err := client.UnikClient(host).Images().Get(name)
    31  			if err != nil {
    32  				return err
    33  			}
    34  			data, err := json.Marshal(image)
    35  			if err != nil {
    36  				return err
    37  			}
    38  			fmt.Printf("%s\n", string(data))
    39  			return nil
    40  		}(); err != nil {
    41  			logrus.Errorf("describing image failed: %v", err)
    42  			os.Exit(-1)
    43  		}
    44  	},
    45  }
    46  
    47  func init() {
    48  	RootCmd.AddCommand(describeImageCmd)
    49  	describeImageCmd.Flags().StringVar(&name, "image", "", "<string,required> name or id of image. unik accepts a prefix of the name or id")
    50  }