/
main.go
157 lines (127 loc) · 4.18 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.ketch.com/cli/ketch-cli/pkg/cli"
"go.ketch.com/cli/ketch-cli/pkg/flags"
"go.ketch.com/cli/ketch-cli/pkg/transponder"
"go.ketch.com/cli/ketch-cli/version"
"go.ketch.com/lib/orlop"
stdlog "log"
"os"
"path"
)
func main() {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
DisableTimestamp: true,
DisableLevelTruncation: true,
PadLevelText: true,
})
stdlog.SetOutput(logrus.New().Writer())
var rootCmd = &cobra.Command{
Use: version.Name,
Short: version.Description,
Version: version.String(),
TraverseChildren: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
configFile, err := cmd.Flags().GetString(flags.Config)
if err != nil {
return err
}
envFiles := []string{".env", path.Join(homeDir, ".ketchrc"), ".ketchrc", configFile}
for _, file := range envFiles {
if f, err := os.Stat(file); err == nil && !f.IsDir() {
_ = godotenv.Overload(file)
}
}
return nil
},
}
rootCmd.SetHelpCommand(&cobra.Command{
Use: "help [command]",
Short: "help about any command",
Long: `Help provides help for any command in the application.
Simply type ` + rootCmd.Name() + ` help [path to command] for full details.`,
Run: func(c *cobra.Command, args []string) {
cmd, _, e := c.Root().Find(args)
if cmd == nil || e != nil {
c.Printf("Unknown help topic %#q\n", args)
c.Root().Usage()
} else {
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cmd.Help()
}
},
})
var runner = orlop.NewRunner(version.Name)
rootCmd.PersistentFlags().String(flags.Token, runner.Getenv("TOKEN"), "auth token")
rootCmd.PersistentFlags().String(flags.Config, ".ketchrc", "environment file")
rootCmd.PersistentFlags().String(flags.URL, runner.Getenv("URL"), "url to Ketch API")
rootCmd.PersistentFlags().Bool(flags.TLSInsecure, runner.Getenv("INSECURE") == "true", "set true to skip certificate verification")
rootCmd.PersistentFlags().String(flags.TLSCert, runner.Getenv("TLS_CERT_FILE"), "TLS client certificate")
rootCmd.PersistentFlags().String(flags.TLSKey, runner.Getenv("TLS_KEY_FILE"), "TLS private key")
rootCmd.PersistentFlags().String(flags.TLSCACert, runner.Getenv("TLS_ROOTCA_FILE"), "TLS root CA certificate")
rootCmd.PersistentFlags().String(flags.TLSServerName, runner.Getenv("TLS_OVERRIDE"), "override the TLS server name")
rootCmd.SilenceUsage = true
//
// Login
//
var loginCmd = &cobra.Command{
Use: "login",
Short: "Connect the CLI to your Ketch account by logging in to persist your secret key locally.",
Args: cobra.NoArgs,
RunE: cli.Login,
}
rootCmd.AddCommand(loginCmd)
//
// Apps
//
var publishCmd = &cobra.Command{
Use: "publish",
Short: "publish an app",
RunE: cli.Publish,
}
rootCmd.AddCommand(publishCmd)
publishCmd.Flags().StringP(flags.File, "f", "ketch-manifest.yml", "app manifest filename")
publishCmd.Flags().String(flags.Version, runner.Getenv("VERSION"), "app version")
var validateCmd = &cobra.Command{
Use: "validate",
Short: "validate an app manifest file",
RunE: cli.Validate,
}
rootCmd.AddCommand(validateCmd)
validateCmd.Flags().StringP(flags.File, "f", "ketch-manifest.yml", "app config name")
//
// Transponder
//
var transponderCmd = &cobra.Command{
Use: "transponder",
Short: "transponder commands",
TraverseChildren: true,
}
rootCmd.AddCommand(transponderCmd)
var transponderListCmd = &cobra.Command{
Use: "ls",
Short: "list connections",
RunE: transponder.List,
}
transponderCmd.AddCommand(transponderListCmd)
var transponderConfigureCmd = &cobra.Command{
Use: "configure",
Short: "configure a connection",
RunE: transponder.Configure,
Args: cobra.ExactArgs(1),
}
transponderConfigureCmd.Flags().StringToStringP(flags.Parameter, "P", nil, "parameter key/value")
transponderCmd.AddCommand(transponderConfigureCmd)
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
os.Exit(1)
}
}