1 // Copyright © 2017 NAME HERE <EMAIL ADDRESS>
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 homedir "github.com/mitchellh/go-homedir"
22 "github.com/spf13/cobra"
23 "github.com/spf13/viper"
28 // RootCmd represents the base command when called without any subcommands
29 var RootCmd = &cobra.Command{
31 Short: "A brief description of your application",
32 Long: `A longer description that spans multiple lines and likely contains
33 examples and usage of using your application. For example:
35 Cobra is a CLI library for Go that empowers applications.
36 This application is a tool to generate the needed files
37 to quickly create a Cobra application.`,
38 // Uncomment the following line if your bare application
39 // has an action associated with it:
40 // Run: func(cmd *cobra.Command, args []string) { },
43 // Execute adds all child commands to the root command and sets flags appropriately.
44 // This is called by main.main(). It only needs to happen once to the rootCmd.
46 if err := RootCmd.Execute(); err != nil {
53 cobra.OnInitialize(initConfig)
55 // Here you will define your flags and configuration settings.
56 // Cobra supports persistent flags, which, if defined here,
57 // will be global for your application.
58 RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")
60 // Cobra also supports local flags, which will only run
61 // when this action is called directly.
62 RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
65 // initConfig reads in config file and ENV variables if set.
68 // Use config file from the flag.
69 viper.SetConfigFile(cfgFile)
71 // Find home directory.
72 home, err := homedir.Dir()
78 // Search config in home directory with name ".testproject" (without extension).
79 viper.AddConfigPath(home)
80 viper.SetConfigName(".testproject")
83 viper.AutomaticEnv() // read in environment variables that match
85 // If a config file is found, read it in.
86 if err := viper.ReadInConfig(); err == nil {
87 fmt.Println("Using config file:", viper.ConfigFileUsed())