/
config.go
213 lines (176 loc) · 5.2 KB
/
config.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
package cmd
import (
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"github.com/luizalabs/teresa/pkg/client"
"github.com/spf13/cobra"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Setup cluster servers and view config file",
Long: `Setup clusters and view the configuration.
To perform any action, you must have at least one cluster setup.
To add one named "aws-staging", for instance:
$ teresa config set-cluster aws-staging --server mycluster.mydomain.com
You can also pass extra flags:
--port TCP port to use when communicating with the server
That will add the "aws-staging" cluster to the configuration file,
but won't set it as the default. To do that, you must run:
$ teresa config use-cluster aws-staging
From that point on, teresa will use this cluster until you select
another via: teresa config use-cluster another-cluster.
`,
}
// returns the config file
var viewConfigCmd = &cobra.Command{
Use: "view",
Short: "view the config file",
Long: `View the config file.
eg.:
$ teresa config view
`,
Run: viewConfigFile,
}
var setClusterCmd = &cobra.Command{
Use: "set-cluster name",
Short: "sets a cluster entry in the config file",
Long: `Add or update a cluster entry.
eg.:
$ teresa config set-cluster aws_staging --server staging.mydomain.com
`,
Run: setCluster,
}
var rmClusterCmd = &cobra.Command{
Use: "rm-cluster name",
Short: "removes a cluster entry from the config file",
Long: `Remove a cluster entry.
eg.:
$ teresa config rm-cluster aws_staging
`,
Run: rmCluster,
}
var useClusterCmd = &cobra.Command{
Use: "use-cluster name",
Short: "sets a cluster as the current in the config file",
Long: `Set a cluster as in-use, so every action will be sent to it.
eg.:
$ teresa config use-cluster aws_staging
`,
Run: useCluster,
}
func init() {
RootCmd.AddCommand(configCmd)
configCmd.AddCommand(viewConfigCmd)
setClusterCmd.Flags().String("server", "", "Server hostname or IP")
setClusterCmd.Flags().Bool("tls", false, "Enables TLS")
setClusterCmd.Flags().Bool("tlsinsecure", false, "Allow insecure TLS connections")
setClusterCmd.Flags().Bool("current", false, "Set this server to future use")
setClusterCmd.Flags().Int("port", 50051, "Server TCP port")
configCmd.AddCommand(setClusterCmd)
configCmd.AddCommand(useClusterCmd)
configCmd.AddCommand(rmClusterCmd)
}
func useCluster(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Usage()
return
}
name := args[0]
c, err := client.ReadConfigFile(cfgFile)
if err != nil {
client.PrintErrorAndExit("Cannot read the config file, have you created it with `teresa set-cluster` command?")
}
if _, ok := c.Clusters[name]; !ok {
client.PrintErrorAndExit("Cluster `%s` not configured yet", name)
}
c.CurrentCluster = name
if err = client.SaveConfigFile(cfgFile, c); err != nil {
client.PrintErrorAndExit("Error trying to save config file: %v", err)
}
}
// setCluster add a new server to the config file
func setCluster(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Usage()
return
}
server, _ := cmd.Flags().GetString("server")
if server == "" {
client.PrintErrorAndExit("Server URI not provided")
}
port, err := cmd.Flags().GetInt("port")
if err != nil {
client.PrintErrorAndExit("Invalid port parameter")
}
server = net.JoinHostPort(server, strconv.Itoa(port))
useTLS, err := cmd.Flags().GetBool("tls")
if err != nil {
client.PrintErrorAndExit("Invalid tls parameter")
}
insecure, err := cmd.Flags().GetBool("tlsinsecure")
if err != nil {
client.PrintErrorAndExit("Invalid tlsinsecure parameter")
}
current, err := cmd.Flags().GetBool("current")
if err != nil {
client.PrintErrorAndExit("Invalid current parameter")
}
name := args[0]
c, err := client.ReadConfigFile(cfgFile)
if err != nil {
c = &client.Config{
Clusters: make(map[string]client.ClusterConfig),
CurrentCluster: name,
}
}
c.Clusters[name] = client.ClusterConfig{
Server: server,
UseTLS: useTLS,
Insecure: insecure,
}
if current {
c.CurrentCluster = name
}
if err = client.SaveConfigFile(cfgFile, c); err != nil {
client.PrintErrorAndExit("Error trying to save config file: %v", err)
}
}
// rmCluster removes a server from the config file
func rmCluster(cmd *cobra.Command, args []string) {
if len(args) == 0 || args[0] == "" {
cmd.Usage()
return
}
name := args[0]
c, err := client.ReadConfigFile(cfgFile)
if err != nil {
client.PrintErrorAndExit("Error trying to read config file: %v", err)
}
if _, found := c.Clusters[name]; found {
delete(c.Clusters, name)
if err = client.SaveConfigFile(cfgFile, c); err != nil {
client.PrintErrorAndExit("Error trying to save config file: %v", err)
}
fmt.Printf("Config %s removed\n", name)
} else {
client.PrintErrorAndExit("Config %s not found", name)
}
}
func viewConfigFile(cmd *cobra.Command, args []string) {
y, err := ioutil.ReadFile(cfgFile)
if err != nil {
if _, ok := err.(*os.PathError); ok {
client.PrintErrorAndExit(
"Config file not found on `%s` use command `teresa config set-cluster` to create the config file",
cfgFile,
)
} else {
client.PrintErrorAndExit("Error trying to read config file: %v", err)
}
}
fmt.Println(string(y))
}