github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/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/coreos/rkt/store"
    21  
    22  	"github.com/coreos/rkt/Godeps/_workspace/src/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 hash 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", false, "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 1
    44  	}
    45  
    46  	s, err := store.NewStore(globalFlags.Dir)
    47  	if err != nil {
    48  		stderr("image cat-manifest: cannot open store: %v", err)
    49  		return 1
    50  	}
    51  
    52  	key, err := getStoreKeyFromAppOrHash(s, args[0])
    53  	if err != nil {
    54  		stderr("image cat-manifest: %v", err)
    55  		return 1
    56  	}
    57  
    58  	manifest, err := s.GetImageManifest(key)
    59  	if err != nil {
    60  		stderr("image cat-manifest: cannot get image manifest: %v", err)
    61  		return 1
    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("image cat-manifest: cannot read the image manifest: %v", err)
    72  		return 1
    73  	}
    74  
    75  	stdout(string(b))
    76  	return 0
    77  }