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

     1  // Copyright 2016 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/spf13/cobra"
    21  )
    22  
    23  var (
    24  	cmdConfig = &cobra.Command{
    25  		Use:   "config",
    26  		Short: "Print configuration for each stage in JSON format",
    27  		Long: `The output will be parsable JSON with "stage0" and stage1" as keys and rkt configuration entries as values.
    28  The generated configuration entries resemble the original rkt configuration format.`,
    29  		Run: runWrapper(runConfig),
    30  	}
    31  	flagConfigPrettyPrint bool
    32  )
    33  
    34  func init() {
    35  	cmdRkt.AddCommand(cmdConfig)
    36  	cmdConfig.Flags().BoolVar(&flagConfigPrettyPrint, "pretty-print", true, "apply indent to format the output")
    37  }
    38  
    39  func runConfig(cmd *cobra.Command, args []string) int {
    40  	config, err := getConfig()
    41  	if err != nil {
    42  		stderr.PrintE("cannot get configuration", err)
    43  		return 254
    44  	}
    45  
    46  	var b []byte
    47  	if flagConfigPrettyPrint {
    48  		b, err = json.MarshalIndent(config, "", "\t")
    49  	} else {
    50  		b, err = json.Marshal(config)
    51  	}
    52  	if err != nil {
    53  		stderr.PanicE("error marshaling configuration", err)
    54  	}
    55  
    56  	stdout.Print(string(b))
    57  	return 0
    58  }