forked from bandesz/awsc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
root.go
53 lines (45 loc) · 1.28 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package command
import (
"fmt"
"os"
"path"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
// Global flags and options
var (
Region string
CacheDir string
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "awsc <command> <subcommand> [args]",
Short: "AWS companion app",
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Fprintln(RootCmd.OutOrStdout(), err)
os.Exit(1)
}
}
func init() {
homeDir, err := homedir.Dir()
if err != nil {
homeDir = "/tmp"
}
defaultCacheDir := path.Join(homeDir, ".awsc")
RootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "The region to use, overrides the value from the shared AWS credential files")
RootCmd.PersistentFlags().StringVarP(&CacheDir, "cache-dir", "c", defaultCacheDir, "Cache directory")
envs := map[string]string{
"AWS_REGION": "region",
}
for env, flag := range envs {
flag := RootCmd.PersistentFlags().Lookup(flag)
flag.Usage = fmt.Sprintf("%v [$%v]", flag.Usage, env)
if value := os.Getenv(env); value != "" {
flag.Value.Set(value)
}
}
}