github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/root.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     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 cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"github.com/spf13/cobra"
    23  	"github.com/spf13/viper"
    24  
    25  	"github.com/alibaba/sealer/common"
    26  	"github.com/alibaba/sealer/logger"
    27  )
    28  
    29  type rootOpts struct {
    30  	cfgFile     string
    31  	debugModeOn bool
    32  	hideLogTime bool
    33  	hideLogPath bool
    34  }
    35  
    36  var rootOpt rootOpts
    37  
    38  // rootCmd represents the base command when called without any subcommands
    39  var rootCmd = &cobra.Command{
    40  	Use:   "sealer",
    41  	Short: "",
    42  	Long:  ``,
    43  }
    44  
    45  // Execute adds all child commands to the root command and sets flags appropriately.
    46  // This is called by main.main(). It only needs to happen once to the rootCmd.
    47  func Execute() {
    48  	if err := rootCmd.Execute(); err != nil {
    49  		fmt.Fprintf(os.Stderr, "%v\n", err)
    50  		os.Exit(1)
    51  	}
    52  }
    53  
    54  func init() {
    55  	cobra.OnInitialize(initConfig)
    56  	rootCmd.PersistentFlags().StringVar(&rootOpt.cfgFile, "config", "", "config file (default is $HOME/.sealer.json)")
    57  	rootCmd.PersistentFlags().BoolVarP(&rootOpt.debugModeOn, "debug", "d", false, "turn on debug mode")
    58  	rootCmd.PersistentFlags().BoolVar(&rootOpt.hideLogTime, "hide-time", false, "hide the log time")
    59  	rootCmd.PersistentFlags().BoolVar(&rootOpt.hideLogPath, "hide-path", false, "hide the log path")
    60  	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    61  	rootCmd.DisableAutoGenTag = true
    62  }
    63  
    64  // initConfig reads in config file and ENV variables if set.
    65  func initConfig() {
    66  	if rootOpt.cfgFile == "" {
    67  		// Find home directory.
    68  		rootOpt.cfgFile = filepath.Join(common.GetHomeDir(), ".sealer.json")
    69  	}
    70  	// Use config file from the flag.
    71  	// if not set config file, Search config in home directory with name ".sealer.json" (without extension).
    72  	//viper.AddConfigPath(home)
    73  	viper.SetConfigFile(rootOpt.cfgFile)
    74  
    75  	viper.AutomaticEnv() // read in environment variables that match
    76  
    77  	logger.InitLogger(logger.Config{DebugMode: rootOpt.debugModeOn})
    78  
    79  	logger.SetLogPath(!rootOpt.hideLogPath)
    80  
    81  	if !rootOpt.hideLogTime {
    82  		logger.SetTimeFormat(logger.LogTimeDefaultFormat)
    83  	}
    84  	logger.Cfg(rootOpt.debugModeOn)
    85  }