|
| 1 | +/* |
| 2 | +Copyright © 2019 NAME HERE <EMAIL ADDRESS> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "github.com/spf13/cobra" |
| 22 | + |
| 23 | + homedir "github.com/mitchellh/go-homedir" |
| 24 | + "github.com/spf13/viper" |
| 25 | + |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +var cfgFile string |
| 30 | + |
| 31 | + |
| 32 | +// rootCmd represents the base command when called without any subcommands |
| 33 | +var rootCmd = &cobra.Command{ |
| 34 | + Use: "example", |
| 35 | + Short: "A brief description of your application", |
| 36 | + Long: `A longer description that spans multiple lines and likely contains |
| 37 | +examples and usage of using your application. For example: |
| 38 | +
|
| 39 | +Cobra is a CLI library for Go that empowers applications. |
| 40 | +This application is a tool to generate the needed files |
| 41 | +to quickly create a Cobra application.`, |
| 42 | + // Uncomment the following line if your bare application |
| 43 | + // has an action associated with it: |
| 44 | + // Run: func(cmd *cobra.Command, args []string) { }, |
| 45 | +} |
| 46 | + |
| 47 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 48 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 49 | +func Execute() { |
| 50 | + if err := rootCmd.Execute(); err != nil { |
| 51 | + fmt.Println(err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func init() { |
| 57 | + cobra.OnInitialize(initConfig) |
| 58 | + |
| 59 | + // Here you will define your flags and configuration settings. |
| 60 | + // Cobra supports persistent flags, which, if defined here, |
| 61 | + // will be global for your application. |
| 62 | + |
| 63 | + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.example.yaml)") |
| 64 | + |
| 65 | + |
| 66 | + // Cobra also supports local flags, which will only run |
| 67 | + // when this action is called directly. |
| 68 | + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +// initConfig reads in config file and ENV variables if set. |
| 73 | +func initConfig() { |
| 74 | + if cfgFile != "" { |
| 75 | + // Use config file from the flag. |
| 76 | + viper.SetConfigFile(cfgFile) |
| 77 | + } else { |
| 78 | + // Find home directory. |
| 79 | + home, err := homedir.Dir() |
| 80 | + if err != nil { |
| 81 | + fmt.Println(err) |
| 82 | + os.Exit(1) |
| 83 | + } |
| 84 | + |
| 85 | + // Search config in home directory with name ".example" (without extension). |
| 86 | + viper.AddConfigPath(home) |
| 87 | + viper.SetConfigName(".example") |
| 88 | + } |
| 89 | + |
| 90 | + viper.AutomaticEnv() // read in environment variables that match |
| 91 | + |
| 92 | + // If a config file is found, read it in. |
| 93 | + if err := viper.ReadInConfig(); err == nil { |
| 94 | + fmt.Println("Using config file:", viper.ConfigFileUsed()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments