github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/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  	pkgPod "github.com/rkt/rkt/pkg/pod"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  var (
    25  	cmdCatManifest = &cobra.Command{
    26  		Use:   "cat-manifest --uuid-file=FILE | UUID ...",
    27  		Short: "Inspect and print the pod manifest",
    28  		Long:  `UUID should be the UUID of a pod`,
    29  		Run:   runWrapper(runCatManifest),
    30  	}
    31  	flagPMPrettyPrint bool
    32  )
    33  
    34  func init() {
    35  	cmdRkt.AddCommand(cmdCatManifest)
    36  	cmdCatManifest.Flags().BoolVar(&flagPMPrettyPrint, "pretty-print", true, "apply indent to format the output")
    37  	cmdCatManifest.Flags().StringVar(&flagUUIDFile, "uuid-file", "", "read pod UUID from file instead of argument")
    38  }
    39  
    40  func runCatManifest(cmd *cobra.Command, args []string) (exit int) {
    41  	var podUUID string
    42  
    43  	if flagUUIDFile != "" {
    44  		uuid, err := pkgPod.ReadUUIDFromFile(flagUUIDFile)
    45  		if err != nil {
    46  			stderr.PrintE("unable to resolve UUID from file", err)
    47  			return 254
    48  		}
    49  		podUUID = uuid
    50  	} else {
    51  		if len(args) != 1 {
    52  			cmd.Usage()
    53  			return 254
    54  		}
    55  		podUUID = args[0]
    56  	}
    57  
    58  	pod, err := pkgPod.PodFromUUIDString(getDataDir(), podUUID)
    59  	if err != nil {
    60  		stderr.PrintE("problem retrieving pod", err)
    61  		return 254
    62  	}
    63  
    64  	defer pod.Close()
    65  
    66  	_, manifest, err := pod.PodManifest()
    67  	if err != nil {
    68  		return 254
    69  	}
    70  
    71  	var b []byte
    72  	if flagPMPrettyPrint {
    73  		b, err = json.MarshalIndent(manifest, "", "\t")
    74  	} else {
    75  		b, err = json.Marshal(manifest)
    76  	}
    77  	if err != nil {
    78  		stderr.PrintE("cannot read the pod manifest", err)
    79  		return 254
    80  	}
    81  
    82  	stdout.Print(string(b))
    83  	return 0
    84  }