|
| 1 | +// Copyright © 2018 Ken'ichiro Oyama <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package cmd |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + |
| 27 | + homedir "github.com/mitchellh/go-homedir" |
| 28 | + "github.com/spf13/cobra" |
| 29 | + "github.com/spf13/viper" |
| 30 | +) |
| 31 | + |
| 32 | +var cfgFile string |
| 33 | + |
| 34 | +// rootCmd represents the base command when called without any subcommands |
| 35 | +var rootCmd = &cobra.Command{ |
| 36 | + Use: "tbls", |
| 37 | + Short: "A brief description of your application", |
| 38 | + Long: `A longer description that spans multiple lines and likely contains |
| 39 | +examples and usage of using your application. For example: |
| 40 | +
|
| 41 | +Cobra is a CLI library for Go that empowers applications. |
| 42 | +This application is a tool to generate the needed files |
| 43 | +to quickly create a Cobra application.`, |
| 44 | + // Uncomment the following line if your bare application |
| 45 | + // has an action associated with it: |
| 46 | + // Run: func(cmd *cobra.Command, args []string) { }, |
| 47 | +} |
| 48 | + |
| 49 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 50 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 51 | +func Execute() { |
| 52 | + if err := rootCmd.Execute(); err != nil { |
| 53 | + fmt.Println(err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func init() { |
| 59 | + cobra.OnInitialize(initConfig) |
| 60 | + |
| 61 | + // Here you will define your flags and configuration settings. |
| 62 | + // Cobra supports persistent flags, which, if defined here, |
| 63 | + // will be global for your application. |
| 64 | + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.tbls.yaml)") |
| 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 | +// initConfig reads in config file and ENV variables if set. |
| 72 | +func initConfig() { |
| 73 | + if cfgFile != "" { |
| 74 | + // Use config file from the flag. |
| 75 | + viper.SetConfigFile(cfgFile) |
| 76 | + } else { |
| 77 | + // Find home directory. |
| 78 | + home, err := homedir.Dir() |
| 79 | + if err != nil { |
| 80 | + fmt.Println(err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + |
| 84 | + // Search config in home directory with name ".tbls" (without extension). |
| 85 | + viper.AddConfigPath(home) |
| 86 | + viper.SetConfigName(".tbls") |
| 87 | + } |
| 88 | + |
| 89 | + viper.AutomaticEnv() // read in environment variables that match |
| 90 | + |
| 91 | + // If a config file is found, read it in. |
| 92 | + if err := viper.ReadInConfig(); err == nil { |
| 93 | + fmt.Println("Using config file:", viper.ConfigFileUsed()) |
| 94 | + } |
| 95 | +} |
0 commit comments