-
Notifications
You must be signed in to change notification settings - Fork 0
/
envvar.go
212 lines (197 loc) · 6.61 KB
/
envvar.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
// Copyright 2016 EF CTX. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"net/http"
"regexp"
"sort"
"strings"
"github.com/tsuru/gnuflag"
"github.com/tsuru/tsuru/api"
"github.com/tsuru/tsuru/cmd"
tsuruerrors "github.com/tsuru/tsuru/errors"
)
type projectEnvVarSet struct {
projectName string
envs commaSeparatedFlag
private bool
noRestart bool
fs *gnuflag.FlagSet
}
func (c *projectEnvVarSet) Info() *cmd.Info {
return &cmd.Info{
Name: "envvar-set",
Desc: "defines environment variables for a given project",
Usage: "envvar-set <NAME=value> [NAME=value]... <-n/--project-name projectname> [-p/--private] [--no-restart]",
MinArgs: 1,
}
}
func (c *projectEnvVarSet) Run(ctx *cmd.Context, client *cmd.Client) error {
ctx.RawOutput()
if c.projectName == "" {
return errors.New("please provide the name of the project")
}
config, err := loadConfigFile()
if err != nil {
return errors.New("unable to load environments file, please make sure that tranor is properly configured")
}
raw := strings.Join(ctx.Args, "\n")
regex := regexp.MustCompile(`(\w+=[^\n]+)(\n|$)`)
decls := regex.FindAllStringSubmatch(raw, -1)
if len(decls) < 1 || len(decls) != len(ctx.Args) {
return errors.New("configuration vars must be specified in the form NAME=value")
}
envVars := api.Envs{
NoRestart: c.noRestart,
Private: c.private,
}
for _, decl := range decls {
parts := strings.SplitN(decl[1], "=", 2)
envVars.Envs = append(envVars.Envs, struct{ Name, Value string }{Name: parts[0], Value: parts[1]})
}
envNames := c.envs.Values()
if len(envNames) == 0 {
envNames = config.envNames()
}
var cmdErr error
for _, envName := range envNames {
appName := fmt.Sprintf("%s-%s", c.projectName, envName)
fmt.Fprintf(ctx.Stdout, "setting variables in environment %q... ", envName)
err := setEnvVars(client, appName, &envVars)
status := "ok"
if err != nil {
if e, ok := err.(*tsuruerrors.HTTP); ok && e.Code == http.StatusNotFound {
status = "not found"
} else {
status = "failed"
cmdErr = err
}
}
fmt.Fprintln(ctx.Stdout, status)
}
return cmdErr
}
func (c *projectEnvVarSet) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = gnuflag.NewFlagSet("envvar-set", gnuflag.ExitOnError)
c.fs.StringVar(&c.projectName, "project-name", "", "name of the project")
c.fs.StringVar(&c.projectName, "n", "", "name of the project")
c.fs.Var(&c.envs, "envs", "comma-separated list of environments to set the variables")
c.fs.Var(&c.envs, "e", "comma-separated list of environments to set the variables")
c.fs.BoolVar(&c.private, "private", false, "set the variables to private (not visible through command line)")
c.fs.BoolVar(&c.private, "p", false, "set the variables to private (not visible through command line)")
c.fs.BoolVar(&c.noRestart, "no-restart", false, "set the environment variables without restarting the application process")
}
return c.fs
}
type projectEnvVarGet struct {
projectName string
envs commaSeparatedFlag
fs *gnuflag.FlagSet
}
func (c *projectEnvVarGet) Info() *cmd.Info {
return &cmd.Info{
Name: "envvar-get",
Desc: "gets environment variables of the project in the given environments",
}
}
func (c *projectEnvVarGet) Run(ctx *cmd.Context, client *cmd.Client) error {
if c.projectName == "" {
return errors.New("please provide the name of the project")
}
config, err := loadConfigFile()
if err != nil {
return errors.New("unable to load environments file, please make sure that tranor is properly configured")
}
envNames := c.envs.Values()
if len(envNames) == 0 {
envNames = config.envNames()
}
for _, envName := range envNames {
appName := fmt.Sprintf("%s-%s", c.projectName, envName)
envVars, err := getEnvVars(client, appName)
if err != nil {
if e, ok := err.(*tsuruerrors.HTTP); ok && e.Code == http.StatusNotFound {
fmt.Fprintf(ctx.Stderr, "WARNING: project not found in environment %q\n", envName)
continue
}
return err
}
fmt.Fprintf(ctx.Stdout, "variables in %q:\n\n", envName)
lines := make([]string, len(envVars))
for i, evar := range envVars {
lines[i] = fmt.Sprintf(" %s", &evar)
}
sort.Strings(lines)
fmt.Fprintln(ctx.Stdout, strings.Join(lines, "\n"))
fmt.Fprint(ctx.Stdout, "\n\n")
}
return nil
}
func (c *projectEnvVarGet) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = gnuflag.NewFlagSet("envvar-get", gnuflag.ExitOnError)
c.fs.StringVar(&c.projectName, "project-name", "", "name of the project")
c.fs.StringVar(&c.projectName, "n", "", "name of the project")
c.fs.Var(&c.envs, "envs", "comma-separated list of environments to get the variables")
c.fs.Var(&c.envs, "e", "comma-separated list of environments to get the variables")
}
return c.fs
}
type projectEnvVarUnset struct {
projectName string
noRestart bool
envs commaSeparatedFlag
fs *gnuflag.FlagSet
}
func (c *projectEnvVarUnset) Info() *cmd.Info {
return &cmd.Info{
Name: "envvar-unset",
Desc: "unset environment variables of the project in the given environments",
MinArgs: 1,
}
}
func (c *projectEnvVarUnset) Run(ctx *cmd.Context, client *cmd.Client) error {
if c.projectName == "" {
return errors.New("please provide the name of the project")
}
config, err := loadConfigFile()
if err != nil {
return errors.New("unable to load environments file, please make sure that tranor is properly configured")
}
envNames := c.envs.Values()
if len(envNames) == 0 {
envNames = config.envNames()
}
var cmdErr error
for _, envName := range envNames {
appName := fmt.Sprintf("%s-%s", c.projectName, envName)
fmt.Fprintf(ctx.Stdout, "unsetting variables from environment %q... ", envName)
err := unsetEnvVars(client, appName, c.noRestart, ctx.Args)
status := "ok"
if err != nil {
if e, ok := err.(*tsuruerrors.HTTP); ok && e.Code == http.StatusNotFound {
status = "not found"
} else {
status = "failed"
cmdErr = err
}
}
fmt.Fprintln(ctx.Stdout, status)
}
return cmdErr
}
func (c *projectEnvVarUnset) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = gnuflag.NewFlagSet("envvar-get", gnuflag.ExitOnError)
c.fs.StringVar(&c.projectName, "project-name", "", "name of the project")
c.fs.StringVar(&c.projectName, "n", "", "name of the project")
c.fs.Var(&c.envs, "envs", "comma-separated list of environments to set the variables")
c.fs.Var(&c.envs, "e", "comma-separated list of environments to set the variables")
c.fs.BoolVar(&c.noRestart, "no-restart", false, "unset environment variables without restarting the application process")
}
return c.fs
}