github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/plugin/plugin_pipeline/cmd/root.go (about)

     1  // Copyright © 2016 NAME HERE <EMAIL ADDRESS>
     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  
    21  	"github.com/spf13/cobra"
    22  	"github.com/spf13/viper"
    23  )
    24  
    25  var cfgFile string
    26  
    27  // RootCmd represents the base command when called without any subcommands
    28  var RootCmd = &cobra.Command{
    29  	Use:   "plugin_pipeline",
    30  	Short: "A brief description of your application",
    31  	Long: `A longer description that spans multiple lines and likely contains
    32  examples and usage of using your application. For example:
    33  
    34  Cobra is a CLI library for Go that empowers applications.
    35  This application is a tool to generate the needed files
    36  to quickly create a Cobra application.`,
    37  // Uncomment the following line if your bare application
    38  // has an action associated with it:
    39  //	Run: func(cmd *cobra.Command, args []string) { },
    40  }
    41  
    42  // Execute adds all child commands to the root command sets flags appropriately.
    43  // This is called by main.main(). It only needs to happen once to the rootCmd.
    44  func Execute() {
    45  	if err := RootCmd.Execute(); err != nil {
    46  		fmt.Println(err)
    47  		os.Exit(-1)
    48  	}
    49  }
    50  
    51  func init() {
    52  	cobra.OnInitialize(initConfig)
    53  
    54  	// Here you will define your flags and configuration settings.
    55  	// Cobra supports Persistent Flags, which, if defined here,
    56  	// will be global for your application.
    57  
    58  	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.plugin_pipeline.yaml)")
    59  	// Cobra also supports local flags, which will only run
    60  	// when this action is called directly.
    61  	RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    62  }
    63  
    64  // initConfig reads in config file and ENV variables if set.
    65  func initConfig() {
    66  	if cfgFile != "" { // enable ability to specify config file via flag
    67  		viper.SetConfigFile(cfgFile)
    68  	}
    69  
    70  	viper.SetConfigName(".plugin_pipeline") // name of config file (without extension)
    71  	viper.AddConfigPath("$HOME")  // adding home directory as first search path
    72  	viper.AutomaticEnv()          // read in environment variables that match
    73  
    74  	// If a config file is found, read it in.
    75  	if err := viper.ReadInConfig(); err == nil {
    76  		fmt.Println("Using config file:", viper.ConfigFileUsed())
    77  	}
    78  }