From 2e34198c585f43596845cb0d777a7cf6ec421eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Michon?= Date: Wed, 31 Mar 2021 17:45:16 +0200 Subject: [PATCH] feat(env-get): add command --- CHANGELOG.md | 1 + cmd/commands.go | 1 + cmd/env.go | 43 +++++++++++++++++++++++++++++++++++++------ env/display.go | 21 ++++++++++++++++++++- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf619602f..c89140698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### To be Released +* Add `env-get` command to retrieve the value of a specific environment variable [#643](https://github.com/Scalingo/cli/pull/643) * Error message are outputted on stderr [#639](https://github.com/Scalingo/cli/pull/639) * Automatically prefix the integration URL with https if none is provided [#642](https://github.com/Scalingo/cli/pull/642) * `backups-download` downloads the most recent backup if none is specified [#636](https://github.com/Scalingo/cli/pull/636) diff --git a/cmd/commands.go b/cmd/commands.go index 7614d174b..f4d10a066 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -122,6 +122,7 @@ var ( // Environment envCommand, + envGetCommand, envSetCommand, envUnsetCommand, diff --git a/cmd/env.go b/cmd/env.go index 1e4e147af..bffb5767d 100644 --- a/cmd/env.go +++ b/cmd/env.go @@ -1,6 +1,8 @@ package cmd import ( + "fmt" + "github.com/Scalingo/cli/appdetect" "github.com/Scalingo/cli/cmd/autocomplete" "github.com/Scalingo/cli/env" @@ -15,9 +17,9 @@ var ( Usage: "Display the environment of your apps", Description: `List all the environment variables: - $ scalingo -a myapp env + $ scalingo --app my-app env - # See also commands 'env-set' and 'env-unset'`, + # See also commands 'env-get', 'env-set' and 'env-unset'`, Action: func(c *cli.Context) { currentApp := appdetect.CurrentApp(c) @@ -37,6 +39,35 @@ var ( }, } + envGetCommand = cli.Command{ + Name: "env-get", + Category: "Environment", + Flags: []cli.Flag{appFlag}, + Usage: "Get the requested environment variable from your app", + Description: `Get the value of a specific environment variable: + + $ scalingo --app my-app env-get VAR1 + + # See also commands 'env', 'env-set' and 'env-unset'`, + + Action: func(c *cli.Context) { + if len(c.Args()) != 1 { + cli.ShowCommandHelp(c, "env") + return + } + + currentApp := appdetect.CurrentApp(c) + variableValue, err := env.Get(currentApp, c.Args()[0]) + if err != nil { + errorQuit(err) + } + fmt.Println(variableValue) + }, + BashComplete: func(c *cli.Context) { + autocomplete.CmdFlagsAutoComplete(c, "env") + }, + } + envSetCommand = cli.Command{ Name: "env-set", Category: "Environment", @@ -44,9 +75,9 @@ var ( Usage: "Set the environment variables of your apps", Description: `Set variables: - $ scalingo -a myapp env-set VAR1=VAL1 VAR2=VAL2 + $ scalingo --app my-app env-set VAR1=VAL1 VAR2=VAL2 - # See also commands 'env' and 'env-unset'`, + # See also commands 'env', 'env-get' and 'env-unset'`, Action: func(c *cli.Context) { currentApp := appdetect.CurrentApp(c) @@ -73,9 +104,9 @@ var ( Usage: "Unset environment variables of your apps", Description: `Unset variables: - $ scalingo -a myapp env-unset VAR1 VAR2 + $ scalingo --app my-app env-unset VAR1 VAR2 - # See also commands 'env' and 'env-set'`, + # See also commands 'env', 'env-get' and 'env-set'`, Action: func(c *cli.Context) { currentApp := appdetect.CurrentApp(c) diff --git a/env/display.go b/env/display.go index 496fb3fdd..0e472cbbc 100644 --- a/env/display.go +++ b/env/display.go @@ -1,6 +1,7 @@ package env import ( + "errors" "fmt" "github.com/Scalingo/cli/config" @@ -14,7 +15,7 @@ func Display(app string) error { } vars, err := c.VariablesList(app) if err != nil { - return errgo.Mask(err, errgo.Any) + return errgo.Notef(err, "fail to list the environment variables") } for _, v := range vars { @@ -22,3 +23,21 @@ func Display(app string) error { } return nil } + +func Get(appName, variableName string) (string, error) { + c, err := config.ScalingoClient() + if err != nil { + return "", errgo.Notef(err, "fail to get Scalingo client to get an environment variable") + } + vars, err := c.VariablesListWithoutAlias(appName) + if err != nil { + return "", errgo.Notef(err, "fail to list the environment variables") + } + + for _, v := range vars { + if v.Name == variableName { + return v.Value, nil + } + } + return "", errors.New("environment variable not found") +}