root.go 2.4 KB

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