root.go 2.7 KB

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