1 // Copyright © 2015 Steve Francia <spf@spf13.com>.
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 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
19 homedir "github.com/mitchellh/go-homedir"
20 "github.com/spf13/cobra"
21 "github.com/spf13/viper"
26 cfgFile, userLicense string
28 rootCmd = &cobra.Command{
30 Short: "A generator for Cobra based Applications",
31 Long: `Cobra is a CLI library for Go that empowers applications.
32 This application is a tool to generate the needed files
33 to quickly create a Cobra application.`,
37 // Execute executes the root command.
43 cobra.OnInitialize(initConfig)
45 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
46 rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
47 rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
48 rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
49 viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
50 viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
51 viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
52 viper.SetDefault("license", "apache")
54 rootCmd.AddCommand(addCmd)
55 rootCmd.AddCommand(initCmd)
60 // Use config file from the flag.
61 viper.SetConfigFile(cfgFile)
63 // Find home directory.
64 home, err := homedir.Dir()
69 // Search config in home directory with name ".cobra" (without extension).
70 viper.AddConfigPath(home)
71 viper.SetConfigName(".cobra")
76 if err := viper.ReadInConfig(); err == nil {
77 fmt.Println("Using config file:", viper.ConfigFileUsed())