|
| 1 | +/* |
| 2 | +Copyright © 2020 Focus Centric inc. <dominicstpierre@gmail.com> |
| 3 | +
|
| 4 | +This program is free software: you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation, either version 3 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +
|
| 9 | +This program is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License |
| 15 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/gookit/color" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + "github.com/spf13/viper" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + VERSION = "v1.0.0-beta1" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + clgreen = color.FgGreen.Render |
| 34 | + clinfo = color.Info.Render |
| 35 | + clnote = color.Note.Render |
| 36 | + cllight = color.Light.Render |
| 37 | + clerror = color.Error.Render |
| 38 | + cldanger = color.Danger.Render |
| 39 | + cldebug = color.Debug.Render |
| 40 | + clnotice = color.Notice.Render |
| 41 | + clsuccess = color.Success.Render |
| 42 | + clcomment = color.Comment.Render |
| 43 | + clprimary = color.Primary.Render |
| 44 | + clwarning = color.Warn.Render |
| 45 | + clquestion = color.Question.Render |
| 46 | + clsecondary = color.Secondary.Render |
| 47 | + clbold = color.Bold.Render |
| 48 | +) |
| 49 | + |
| 50 | +var cfgFile string |
| 51 | + |
| 52 | +// rootCmd represents the base command when called without any subcommands |
| 53 | +var rootCmd = &cobra.Command{ |
| 54 | + Use: "backend", |
| 55 | + Short: "StaticBackend CLI for local development, managing resources, and your account.", |
| 56 | + Long: fmt.Sprintf(` |
| 57 | +%s |
| 58 | +
|
| 59 | +This CLI gives you the following functionalities: |
| 60 | +
|
| 61 | +- A local development server: %s |
| 62 | +- Managing your backend resources: %s |
| 63 | +- Managing your account: %s |
| 64 | + `, |
| 65 | + clbold(clsecondary("StaticBackend CLI "+VERSION)), |
| 66 | + clbold("backend server"), |
| 67 | + clsecondary("soon"), |
| 68 | + clsecondary("soon"), |
| 69 | + ), |
| 70 | + // Uncomment the following line if your bare application |
| 71 | + // has an action associated with it: |
| 72 | + Run: func(cmd *cobra.Command, args []string) { |
| 73 | + if cmd.Flag("version").Value.String() == "true" { |
| 74 | + fmt.Println(VERSION) |
| 75 | + } else { |
| 76 | + fmt.Println(cmd.Long) |
| 77 | + fmt.Println("") |
| 78 | + cmd.Usage() |
| 79 | + } |
| 80 | + }, |
| 81 | +} |
| 82 | + |
| 83 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 84 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 85 | +func Execute() { |
| 86 | + if err := rootCmd.Execute(); err != nil { |
| 87 | + fmt.Println(err) |
| 88 | + os.Exit(1) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func init() { |
| 93 | + cobra.OnInitialize(initConfig) |
| 94 | + |
| 95 | + // Here you will define your flags and configuration settings. |
| 96 | + // Cobra supports persistent flags, which, if defined here, |
| 97 | + // will be global for your application. |
| 98 | + |
| 99 | + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $PWD/.backend.yaml)") |
| 100 | + rootCmd.PersistentFlags().Bool("no-color", false, "turns color off") |
| 101 | + |
| 102 | + // Cobra also supports local flags, which will only run |
| 103 | + // when this action is called directly. |
| 104 | + rootCmd.Flags().BoolP("version", "v", false, "display current version") |
| 105 | +} |
| 106 | + |
| 107 | +// initConfig reads in config file and ENV variables if set. |
| 108 | +func initConfig() { |
| 109 | + if cfgFile != "" { |
| 110 | + // Use config file from the flag. |
| 111 | + viper.SetConfigFile(cfgFile) |
| 112 | + } else { |
| 113 | + // Find home directory. |
| 114 | + pwd, err := os.Getwd() |
| 115 | + if err != nil { |
| 116 | + fmt.Println(err) |
| 117 | + os.Exit(1) |
| 118 | + } |
| 119 | + |
| 120 | + // Search config in home directory with name ".cli" (without extension). |
| 121 | + viper.AddConfigPath(pwd) |
| 122 | + viper.SetConfigName(".backend") |
| 123 | + } |
| 124 | + |
| 125 | + viper.AutomaticEnv() // read in environment variables that match |
| 126 | + |
| 127 | + // If a config file is found, read it in. |
| 128 | + if err := viper.ReadInConfig(); err == nil { |
| 129 | + fmt.Println("Using config file:", viper.ConfigFileUsed()) |
| 130 | + } |
| 131 | +} |
0 commit comments