github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/root.go (about) 1 /* 2 * Copyright 2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cmd 18 19 import ( 20 "fmt" 21 "os" 22 23 homedir "github.com/mitchellh/go-homedir" 24 "github.com/spf13/cobra" 25 "github.com/spf13/viper" 26 "github.com/projectriff/riff-cli/global" 27 ) 28 29 var cfgFile string 30 31 // rootCmd represents the base command when called without any subcommands 32 var rootCmd = &cobra.Command{ 33 Use: "riff", 34 Short: "Commands for creating and managing function resources", 35 Long: `riff is for functions 36 37 version ` + global.CLI_VERSION + ` 38 39 the riff tool is used to create and manage function resources for the riff FaaS platform https://projectriff.io/`, 40 SilenceErrors: true, 41 DisableAutoGenTag: true, 42 } 43 44 // Execute adds all child commands to the root command and sets flags appropriately. 45 // This is called by main.main(). It only needs to happen once to the rootCmd. 46 func Execute() { 47 if err := rootCmd.Execute(); err != nil { 48 fmt.Println(err) 49 os.Exit(1) 50 } 51 } 52 53 func init() { 54 55 cobra.OnInitialize(initConfig) 56 57 // Here you will define your flags and configuration settings. 58 // Cobra supports persistent flags, which, if defined here, 59 // will be global for your application. 60 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.riff.yaml)") 61 62 // Cobra also supports local flags, which will only run 63 // when this action is called directly. 64 //rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 65 } 66 67 // initConfig reads in config file and ENV variables if set. 68 func initConfig() { 69 70 if cfgFile != "" { 71 // Use config file from the flag. 72 viper.SetConfigFile(cfgFile) 73 } else { 74 // Find home directory. 75 home, err := homedir.Dir() 76 if err != nil { 77 fmt.Println(err) 78 os.Exit(1) 79 } 80 81 // Search config in home directory with name ".riff" (without extension). 82 viper.AddConfigPath(home) 83 viper.SetConfigName(".riff") 84 } 85 86 viper.SetEnvPrefix("RIFF") // use RIFF_ as prefix 87 viper.AutomaticEnv() // read in environment variables that match 88 89 // If a config file is found, read it in. 90 viper.ReadInConfig() 91 92 InitGlobal() 93 } 94 95 func InitGlobal() { 96 97 if os.Getenv("RIFF_VERSION") != "" { 98 global.RIFF_VERSION = os.Getenv("RIFF_VERSION") 99 } else { 100 if viper.GetString("riffVersion") != "" { 101 global.RIFF_VERSION = viper.GetString("riffVersion") 102 } else { 103 if viper.GetString("riff-version") != "" { 104 global.RIFF_VERSION = viper.GetString("riff-version") 105 } 106 } 107 } 108 }