-
Notifications
You must be signed in to change notification settings - Fork 8
/
root.go
106 lines (89 loc) · 2.89 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cmd
import (
"flag"
"fmt"
"net/url"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/svix/svix-cli/config"
"github.com/svix/svix-cli/flags"
"github.com/svix/svix-cli/version"
svix "github.com/svix/svix-libs/go"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "svix",
Short: "A CLI to interact with the Svix API.",
Version: version.Version,
}
// 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() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.SetVersionTemplate(version.String())
// Root Flags
rootCmd.Flags().BoolP("version", "v", false, "Get the version of the Svix CLI") // overrides default msg
// Global Flags
color := "auto"
colorFlag := flags.NewEnum(&color, "auto", "always", "never")
flag.Var(colorFlag, "color", "auto|always|never")
rootCmd.PersistentFlags().AddGoFlag(flag.Lookup("color"))
cobra.CheckErr(viper.BindPFlag("color", rootCmd.PersistentFlags().Lookup("color"))) // allow color flag to be set in config
// Register Commands
rootCmd.AddCommand(newVersionCmd().cmd)
rootCmd.AddCommand(newLoginCmd().cmd)
rootCmd.AddCommand(newApplicationCmd().cmd)
rootCmd.AddCommand(newAuthenticationCmd().cmd)
rootCmd.AddCommand(newEventTypeCmd().cmd)
rootCmd.AddCommand(newEndpointCmd().cmd)
rootCmd.AddCommand(newMessageCmd().cmd)
rootCmd.AddCommand(newMessageAttemptCmd().cmd)
rootCmd.AddCommand(newVerifyCmd().cmd)
rootCmd.AddCommand(newOpenCmd().cmd)
rootCmd.AddCommand(newListenCmd().cmd)
rootCmd.AddCommand(newImportCmd().cmd)
rootCmd.AddCommand(newExportCmd().cmd)
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
// Setup config file
configFolder, err := config.Folder()
cobra.CheckErr(err)
configFile := filepath.Join(configFolder, config.FileName)
viper.SetConfigType("toml")
viper.SetConfigFile(configFile)
viper.SetConfigPermissions(config.FileMode)
// read in environment variables that match
viper.SetEnvPrefix("svix")
viper.AutomaticEnv()
// If a config file is found, read it in.
_ = viper.ReadInConfig()
}
func getSvixClientOrExit() *svix.Svix {
token := viper.GetString("auth_token")
if token == "" {
fmt.Fprintln(os.Stderr, "No SVIX_AUTH_TOKEN found!")
fmt.Fprintln(os.Stderr, "Try running `svix login` to get started!")
os.Exit(1)
}
opts := getSvixClientOptsOrExit()
return svix.New(token, opts)
}
func getSvixClientOptsOrExit() *svix.SvixOptions {
opts := &svix.SvixOptions{}
rawDebugURL := viper.GetString("debug_url")
if rawDebugURL != "" {
debugURL, err := url.Parse(rawDebugURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid debug_url set: \"%s\"\n", rawDebugURL)
os.Exit(1)
}
opts.DebugURL = debugURL
}
return opts
}