OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / spf13 / cobra / cobra / cmd / root.go
1 // Copyright © 2015 Steve Francia <spf@spf13.com>.
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 // http://www.apache.org/licenses/LICENSE-2.0
7 //
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.
13
14 package cmd
15
16 import (
17         "fmt"
18
19         homedir "github.com/mitchellh/go-homedir"
20         "github.com/spf13/cobra"
21         "github.com/spf13/viper"
22 )
23
24 var (
25         // Used for flags.
26         cfgFile, userLicense string
27
28         rootCmd = &cobra.Command{
29                 Use:   "cobra",
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.`,
34         }
35 )
36
37 // Execute executes the root command.
38 func Execute() {
39         rootCmd.Execute()
40 }
41
42 func init() {
43         cobra.OnInitialize(initConfig)
44
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")
53
54         rootCmd.AddCommand(addCmd)
55         rootCmd.AddCommand(initCmd)
56 }
57
58 func initConfig() {
59         if cfgFile != "" {
60                 // Use config file from the flag.
61                 viper.SetConfigFile(cfgFile)
62         } else {
63                 // Find home directory.
64                 home, err := homedir.Dir()
65                 if err != nil {
66                         er(err)
67                 }
68
69                 // Search config in home directory with name ".cobra" (without extension).
70                 viper.AddConfigPath(home)
71                 viper.SetConfigName(".cobra")
72         }
73
74         viper.AutomaticEnv()
75
76         if err := viper.ReadInConfig(); err == nil {
77                 fmt.Println("Using config file:", viper.ConfigFileUsed())
78         }
79 }