github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/image_cat_manifest.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"encoding/json"
    19  
    20  	"github.com/rkt/rkt/store/imagestore"
    21  
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  var (
    26  	cmdImageCatManifest = &cobra.Command{
    27  		Use:   "cat-manifest IMAGE",
    28  		Short: "Inspect and print the image manifest",
    29  		Long:  `IMAGE should be a string referencing an image; either a ID or an image name.`,
    30  		Run:   runWrapper(runImageCatManifest),
    31  	}
    32  	flagPrettyPrint bool
    33  )
    34  
    35  func init() {
    36  	cmdImage.AddCommand(cmdImageCatManifest)
    37  	cmdImageCatManifest.Flags().BoolVar(&flagPrettyPrint, "pretty-print", true, "apply indent to format the output")
    38  }
    39  
    40  func runImageCatManifest(cmd *cobra.Command, args []string) (exit int) {
    41  	if len(args) != 1 {
    42  		cmd.Usage()
    43  		return 254
    44  	}
    45  
    46  	s, err := imagestore.NewStore(storeDir())
    47  	if err != nil {
    48  		stderr.PrintE("cannot open store", err)
    49  		return 254
    50  	}
    51  
    52  	key, err := getStoreKeyFromAppOrHash(s, args[0])
    53  	if err != nil {
    54  		stderr.Error(err)
    55  		return 254
    56  	}
    57  
    58  	manifest, err := s.GetImageManifest(key)
    59  	if err != nil {
    60  		stderr.PrintE("cannot get image manifest", err)
    61  		return 254
    62  	}
    63  
    64  	var b []byte
    65  	if flagPrettyPrint {
    66  		b, err = json.MarshalIndent(manifest, "", "\t")
    67  	} else {
    68  		b, err = json.Marshal(manifest)
    69  	}
    70  	if err != nil {
    71  		stderr.PrintE("cannot read the image manifest", err)
    72  		return 254
    73  	}
    74  
    75  	stdout.Print(string(b))
    76  	return 0
    77  }