You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

  1. /*
  2. Copyright © 2020 SliverHorn
  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 gva
  14. import (
  15. "github.com/mitchellh/go-homedir"
  16. "os"
  17. "github.com/gookit/color"
  18. "github.com/spf13/cobra"
  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: "gva",
  25. Short: "这是一款amazing的终端工具",
  26. Long: `欢迎使用gva终端工具
  27. ________ ____ ____ _____
  28. / _____/ \ \ / / / _ \
  29. / \ ___ \ Y / / /_\ \
  30. \ \_\ \ \ / / | \
  31. \______ / \___/ \____|__ /
  32. \/ \/
  33. `,
  34. // Uncomment the following line if your bare application
  35. // has an action associated with it:
  36. // Run: func(cmd *cobra.Command, args []string) { },
  37. }
  38. // Execute adds all child commands to the root command and sets flags appropriately.
  39. // This is called by main.main(). It only needs to happen once to the rootCmd.
  40. func Execute() {
  41. if err := rootCmd.Execute(); err != nil {
  42. color.Warn.Println(err)
  43. os.Exit(1)
  44. }
  45. }
  46. func init() {
  47. cobra.OnInitialize(initConfig)
  48. // Here you will define your flags and configuration settings.
  49. // Cobra supports persistent flags, which, if defined here,
  50. // will be global for your application.
  51. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gva.yaml)")
  52. // Cobra also supports local flags, which will only run
  53. // when this action is called directly.
  54. rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  55. }
  56. // initConfig reads in config file and ENV variables if set.
  57. func initConfig() {
  58. if cfgFile != "" {
  59. // Use config file from the flag.
  60. viper.SetConfigFile(cfgFile)
  61. } else {
  62. // Find home directory.
  63. home, err := homedir.Dir()
  64. if err != nil {
  65. color.Warn.Println(err)
  66. os.Exit(1)
  67. }
  68. // Search config in home directory with name ".gva" (without extension).
  69. viper.AddConfigPath(home)
  70. viper.SetConfigName(".gva")
  71. }
  72. viper.AutomaticEnv() // read in environment variables that match
  73. // If a config file is found, read it in.
  74. if err := viper.ReadInConfig(); err == nil {
  75. color.Warn.Println("Using config file:", viper.ConfigFileUsed())
  76. }
  77. }