Skip to content

Commit 50d60fd

Browse files
authored
Feat/job env (#210)
* feat: job envs * test: job envs * feat: job unbind * test: job bind/unbind * chore: update app bind/unbind path to newer version * refactor: review suggestions
1 parent 00513e7 commit 50d60fd

4 files changed

Lines changed: 714 additions & 88 deletions

File tree

tsuru/client/env.go

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,25 @@ Example:
3535
3636
tsuru env-set NAME=value OTHER_NAME="value with spaces" ANOTHER_NAME='using single quotes' -p`
3737

38+
const ErrMissingAppOrJob = "You must pass an application or job"
39+
const ErrAppAndJobNotAllowedTogether = "You must pass an application or job, not both"
40+
3841
type EnvGet struct {
39-
cmd.AppNameMixIn
42+
appName string
43+
jobName string
4044

4145
fs *gnuflag.FlagSet
4246
json bool
4347
}
4448

4549
func (c *EnvGet) Flags() *gnuflag.FlagSet {
4650
if c.fs == nil {
47-
c.fs = c.AppNameMixIn.Flags()
51+
c.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError)
52+
53+
c.fs.StringVar(&c.appName, "app", "", "The name of the app.")
54+
c.fs.StringVar(&c.appName, "a", "", "The name of the app.")
55+
c.fs.StringVar(&c.jobName, "job", "", "The name of the job.")
56+
c.fs.StringVar(&c.jobName, "j", "", "The name of the job.")
4857
c.fs.BoolVar(&c.json, "json", false, "Display JSON format")
4958

5059
}
@@ -54,17 +63,25 @@ func (c *EnvGet) Flags() *gnuflag.FlagSet {
5463
func (c *EnvGet) Info() *cmd.Info {
5564
return &cmd.Info{
5665
Name: "env-get",
57-
Usage: "env get [-a/--app appname] [ENVIRONMENT_VARIABLE1] [ENVIRONMENT_VARIABLE2] ...",
58-
Desc: `Retrieves environment variables for an application.`,
66+
Usage: "env get [-a/--app appname] [-j/--job jobname] [ENVIRONMENT_VARIABLE1] [ENVIRONMENT_VARIABLE2] ...",
67+
Desc: `Retrieves environment variables for an application or job.`,
5968
MinArgs: 0,
6069
}
6170
}
6271

6372
func (c *EnvGet) Run(context *cmd.Context, client *cmd.Client) error {
64-
b, err := requestEnvGetURL(c.AppNameMixIn, context.Args, client)
73+
context.RawOutput()
74+
75+
err := checkAppAndJobInputs(c.appName, c.jobName)
76+
if err != nil {
77+
return err
78+
}
79+
80+
b, err := requestEnvGetURL(c, context.Args, client)
6581
if err != nil {
6682
return err
6783
}
84+
6885
var variables []map[string]interface{}
6986
err = json.Unmarshal(b, &variables)
7087
if err != nil {
@@ -115,7 +132,8 @@ func (c *EnvGet) renderJSON(context *cmd.Context, variables []map[string]interfa
115132
}
116133

117134
type EnvSet struct {
118-
cmd.AppNameMixIn
135+
appName string
136+
jobName string
119137
fs *gnuflag.FlagSet
120138
private bool
121139
noRestart bool
@@ -124,21 +142,24 @@ type EnvSet struct {
124142
func (c *EnvSet) Info() *cmd.Info {
125143
return &cmd.Info{
126144
Name: "env-set",
127-
Usage: "env set <NAME=value> [NAME=value] ... [-a/--app appname] [-p/--private] [--no-restart]",
128-
Desc: `Sets environment variables for an application.`,
145+
Usage: "env set <NAME=value> [NAME=value] ... [-a/--app appname] [-j/--job jobname] [-p/--private] [--no-restart]",
146+
Desc: `Sets environment variables for an application or job.`,
129147
MinArgs: 1,
130148
}
131149
}
132150

133151
func (c *EnvSet) Run(context *cmd.Context, client *cmd.Client) error {
134152
context.RawOutput()
135-
appName, err := c.AppName()
153+
154+
err := checkAppAndJobInputs(c.appName, c.jobName)
136155
if err != nil {
137156
return err
138157
}
158+
139159
if len(context.Args) < 1 {
140160
return errors.New(EnvSetValidationMessage)
141161
}
162+
142163
envs := make([]apiTypes.Env, len(context.Args))
143164
for i := range context.Args {
144165
parts := strings.SplitN(context.Args[i], "=", 2)
@@ -153,10 +174,22 @@ func (c *EnvSet) Run(context *cmd.Context, client *cmd.Client) error {
153174
NoRestart: c.noRestart,
154175
Private: c.private,
155176
}
156-
url, err := cmd.GetURL(fmt.Sprintf("/apps/%s/env", appName))
177+
178+
var path, apiVersion string
179+
switch c.appName {
180+
case "":
181+
path = fmt.Sprintf("/jobs/%s/env", c.jobName)
182+
apiVersion = "1.13"
183+
default:
184+
path = fmt.Sprintf("/apps/%s/env", c.appName)
185+
apiVersion = "1.0"
186+
}
187+
188+
url, err := cmd.GetURLVersion(apiVersion, path)
157189
if err != nil {
158190
return err
159191
}
192+
160193
v, err := form.EncodeToValues(&e)
161194
if err != nil {
162195
return err
@@ -175,7 +208,12 @@ func (c *EnvSet) Run(context *cmd.Context, client *cmd.Client) error {
175208

176209
func (c *EnvSet) Flags() *gnuflag.FlagSet {
177210
if c.fs == nil {
178-
c.fs = c.AppNameMixIn.Flags()
211+
c.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError)
212+
213+
c.fs.StringVar(&c.appName, "app", "", "The name of the app.")
214+
c.fs.StringVar(&c.appName, "a", "", "The name of the app.")
215+
c.fs.StringVar(&c.jobName, "job", "", "The name of the job.")
216+
c.fs.StringVar(&c.jobName, "j", "", "The name of the job.")
179217
c.fs.BoolVar(&c.private, "private", false, "Private environment variables")
180218
c.fs.BoolVar(&c.private, "p", false, "Private environment variables")
181219
c.fs.BoolVar(&c.noRestart, "no-restart", false, "Sets environment varibles without restart the application")
@@ -184,14 +222,20 @@ func (c *EnvSet) Flags() *gnuflag.FlagSet {
184222
}
185223

186224
type EnvUnset struct {
187-
cmd.AppNameMixIn
225+
appName string
226+
jobName string
188227
fs *gnuflag.FlagSet
189228
noRestart bool
190229
}
191230

192231
func (c *EnvUnset) Flags() *gnuflag.FlagSet {
193232
if c.fs == nil {
194-
c.fs = c.AppNameMixIn.Flags()
233+
c.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError)
234+
235+
c.fs.StringVar(&c.appName, "app", "", "The name of the app.")
236+
c.fs.StringVar(&c.appName, "a", "", "The name of the app.")
237+
c.fs.StringVar(&c.jobName, "job", "", "The name of the job.")
238+
c.fs.StringVar(&c.jobName, "j", "", "The name of the job.")
195239
c.fs.BoolVar(&c.noRestart, "no-restart", false, "Unset environment variables without restart the application")
196240
}
197241
return c.fs
@@ -200,28 +244,42 @@ func (c *EnvUnset) Flags() *gnuflag.FlagSet {
200244
func (c *EnvUnset) Info() *cmd.Info {
201245
return &cmd.Info{
202246
Name: "env-unset",
203-
Usage: "env unset <ENVIRONMENT_VARIABLE1> [ENVIRONMENT_VARIABLE2] ... [ENVIRONMENT_VARIABLEN] [-a/--app appname] [--no-restart]",
204-
Desc: `Unset environment variables for an application.`,
247+
Usage: "env unset <ENVIRONMENT_VARIABLE1> [ENVIRONMENT_VARIABLE2] ... [ENVIRONMENT_VARIABLEN] [-a/--app appname] [-j/--job jobname] [--no-restart]",
248+
Desc: `Unset environment variables for an application or job.`,
205249
MinArgs: 1,
206250
}
207251
}
208252

209253
func (c *EnvUnset) Run(context *cmd.Context, client *cmd.Client) error {
210254
context.RawOutput()
211-
appName, err := c.AppName()
255+
256+
err := checkAppAndJobInputs(c.appName, c.jobName)
212257
if err != nil {
213258
return err
214259
}
260+
215261
v := url.Values{}
216262
for _, e := range context.Args {
217263
v.Add("env", e)
218264
}
219265
v.Set("noRestart", strconv.FormatBool(c.noRestart))
220-
u, err := cmd.GetURL(fmt.Sprintf("/apps/%s/env?%s", appName, v.Encode()))
266+
267+
var path, apiVersion string
268+
switch c.appName {
269+
case "":
270+
path = fmt.Sprintf("/jobs/%s/env?%s", c.jobName, v.Encode())
271+
apiVersion = "1.13"
272+
default:
273+
path = fmt.Sprintf("/apps/%s/env?%s", c.appName, v.Encode())
274+
apiVersion = "1.0"
275+
}
276+
277+
url, err := cmd.GetURLVersion(apiVersion, path)
221278
if err != nil {
222279
return err
223280
}
224-
request, err := http.NewRequest(http.MethodDelete, u, nil)
281+
282+
request, err := http.NewRequest(http.MethodDelete, url, nil)
225283
if err != nil {
226284
return err
227285
}
@@ -232,16 +290,23 @@ func (c *EnvUnset) Run(context *cmd.Context, client *cmd.Client) error {
232290
return cmd.StreamJSONResponse(context.Stdout, response)
233291
}
234292

235-
func requestEnvGetURL(g cmd.AppNameMixIn, args []string, client *cmd.Client) ([]byte, error) {
236-
appName, err := g.AppName()
237-
if err != nil {
238-
return nil, err
239-
}
293+
func requestEnvGetURL(c *EnvGet, args []string, client *cmd.Client) ([]byte, error) {
240294
v := url.Values{}
241295
for _, e := range args {
242296
v.Add("env", e)
243297
}
244-
url, err := cmd.GetURL(fmt.Sprintf("/apps/%s/env?%s", appName, v.Encode()))
298+
299+
var path, apiVersion string
300+
switch c.appName {
301+
case "":
302+
path = fmt.Sprintf("/jobs/%s/env?%s", c.jobName, v.Encode())
303+
apiVersion = "1.16"
304+
default:
305+
path = fmt.Sprintf("/apps/%s/env?%s", c.appName, v.Encode())
306+
apiVersion = "1.0"
307+
}
308+
309+
url, err := cmd.GetURLVersion(apiVersion, path)
245310
if err != nil {
246311
return nil, err
247312
}
@@ -260,3 +325,15 @@ func requestEnvGetURL(g cmd.AppNameMixIn, args []string, client *cmd.Client) ([]
260325
}
261326
return b, nil
262327
}
328+
329+
func checkAppAndJobInputs(appName string, jobName string) error {
330+
if appName == "" && jobName == "" {
331+
return errors.New(ErrMissingAppOrJob)
332+
}
333+
334+
if appName != "" && jobName != "" {
335+
return errors.New(ErrAppAndJobNotAllowedTogether)
336+
}
337+
338+
return nil
339+
}

0 commit comments

Comments
 (0)