-
Notifications
You must be signed in to change notification settings - Fork 125
/
cmd.go
235 lines (201 loc) · 6.24 KB
/
cmd.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
goflag "flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/openziti/ziti/ziti/cmd/agentcli"
"github.com/openziti/ziti/ziti/cmd/common"
"github.com/openziti/ziti/ziti/cmd/create"
"github.com/openziti/ziti/ziti/cmd/database"
"github.com/openziti/ziti/ziti/cmd/demo"
"github.com/openziti/ziti/ziti/cmd/edge"
"github.com/openziti/ziti/ziti/cmd/fabric"
"github.com/openziti/ziti/ziti/cmd/pki"
"github.com/openziti/ziti/ziti/cmd/templates"
c "github.com/openziti/ziti/ziti/constants"
"github.com/openziti/ziti/ziti/controller"
"github.com/openziti/ziti/ziti/internal/log"
"github.com/openziti/ziti/ziti/router"
"github.com/openziti/ziti/ziti/tunnel"
"github.com/openziti/ziti/ziti/util"
"github.com/openziti/ziti/common/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// InitOptions the flags for running init
type MainOptions struct {
common.CommonOptions
}
type RootCmd struct {
configFile string
cobraCommand *cobra.Command
}
func GetRootCommand() *cobra.Command {
return rootCommand.cobraCommand
}
var rootCommand = RootCmd{
cobraCommand: &cobra.Command{
Use: "ziti",
Short: "ziti is a CLI for working with Ziti",
Long: `
'ziti' is a CLI for working with a Ziti deployment.
`},
}
// exitWithError will terminate execution with an error result
// It prints the error to stderr and exits with a non-zero exit code
func exitWithError(err error) {
fmt.Fprintf(os.Stderr, "\n%v\n", err)
os.Exit(1)
}
// Execute is ...
func Execute() {
if err := rootCommand.cobraCommand.Execute(); err != nil {
exitWithError(err)
}
}
func init() {
cobra.OnInitialize(initConfig)
NewCmdRoot(os.Stdin, os.Stdout, os.Stderr, rootCommand.cobraCommand)
}
func NewCmdRoot(in io.Reader, out, err io.Writer, cmd *cobra.Command) *cobra.Command {
goflag.CommandLine.VisitAll(func(goflag *goflag.Flag) {
switch goflag.Name {
// Skip things that are dragged in by our dependencies
case "alsologtostderr":
case "logtostderr":
case "log_backtrace_at":
case "log_dir":
case "stderrthreshold":
case "vmodule":
case "v":
default:
cmd.PersistentFlags().AddGoFlag(goflag)
}
})
viper.SetEnvPrefix(c.ZITI) // All env vars we seek will be prefixed with "ZITI_"
viper.AutomaticEnv()
replacer := strings.NewReplacer("-", "_") // We use underscores in env var names, but use dashes in flag names
viper.SetEnvKeyReplacer(replacer)
// cmd.PersistentFlags().StringVar(&rootCommand.configFile, "config", "", "yaml config file (default is $HOME/.ziti.yaml)")
// viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
// viper.SetDefault("config", "$HOME/.ziti.yaml")
// cmd.PersistentFlags().StringVar(&rootCommand.RegistryPath, "state", "", "Location of state storage (ziti 'config' file). Overrides ZITI_STATE_STORE environment variable")
// viper.BindPFlag("ZITI_STATE_STORE", cmd.PersistentFlags().Lookup("state"))
// viper.BindEnv("ZITI_STATE_STORE")
// defaultClusterName := os.Getenv("ZITI_CLUSTER_NAME")
// cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", defaultClusterName, "Name of cluster. Overrides ZITI_CLUSTER_NAME environment variable")
p := common.NewOptionsProvider(out, err)
createCommands := create.NewCmdCreate(out, err)
controllerCmd := controller.NewControllerCmd()
tunnelCmd := tunnel.NewTunnelCmd(false)
routerCmd := router.NewRouterCmd()
agentCommands := agentcli.NewAgentCmd(p)
pkiCommands := pki.NewCmdPKI(out, err)
fabricCommand := fabric.NewFabricCmd(p)
edgeCommand := edge.NewCmdEdge(out, err, p)
demoCmd := demo.NewDemoCmd(p)
opsCommands := &cobra.Command{
Use: "ops ",
Short: "Various utilities useful when operating a Ziti network",
}
opsCommands.AddCommand(database.NewCmdDb(out, err))
opsCommands.AddCommand(NewCmdLogFormat(out, err))
opsCommands.AddCommand(NewUnwrapIdentityFileCommand(out, err))
groups := templates.CommandGroups{
{
Message: "Working with Ziti resources:",
Commands: []*cobra.Command{
createCommands,
},
},
{
Message: "Executing Ziti components:",
Commands: []*cobra.Command{
agentCommands,
controllerCmd,
routerCmd,
tunnelCmd,
pkiCommands,
},
},
{
Message: "Interacting with the Ziti controller",
Commands: []*cobra.Command{
fabricCommand,
edgeCommand,
},
},
{
Message: "Utilities",
Commands: []*cobra.Command{
opsCommands,
},
},
{
Message: "Learning Ziti",
Commands: []*cobra.Command{
demoCmd,
},
},
}
groups.Add(cmd)
cmd.Version = version.GetVersion()
cmd.SetVersionTemplate("{{printf .Version}}\n")
cmd.AddCommand(NewCmdArt(out, err))
cmd.AddCommand(common.NewVersionCmd())
return cmd
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
// Config file precedence: --config flag, ${HOME}/.ziti.yaml ${HOME}/.ziti/config
configFile := rootCommand.configFile
if configFile == "" {
home := util.HomeDir()
configPaths := []string{
filepath.Join(home, ".ziti.yaml"),
filepath.Join(home, ".ziti", "config"),
}
for _, p := range configPaths {
_, err := os.Stat(p)
if err == nil {
configFile = p
break
} else if !os.IsNotExist(err) {
log.Infof("error checking for file %s: %v", p, err)
}
}
}
if configFile != "" {
viper.SetConfigFile(configFile)
viper.SetConfigType("yaml")
if err := viper.ReadInConfig(); err != nil {
log.Warnf("error reading config: %v", err)
}
}
}
func NewRootCommand(in io.Reader, out, err io.Writer) *cobra.Command {
//need to make new CMD every time because the flags are not thread safe...
ret := &cobra.Command{
Use: "ziti",
Short: "ziti is a CLI for working with Ziti",
Long: `
'ziti' is a CLI for working with a Ziti deployment.
`}
NewCmdRoot(in, out, err, ret)
return ret
}