Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(env-get): add command #643

Merged
merged 1 commit into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ var (

// Environment
envCommand,
envGetCommand,
envSetCommand,
envUnsetCommand,

Expand Down
43 changes: 37 additions & 6 deletions cmd/env.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/Scalingo/cli/appdetect"
"github.com/Scalingo/cli/cmd/autocomplete"
"github.com/Scalingo/cli/env"
Expand All @@ -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)
Expand All @@ -37,16 +39,45 @@ 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",
Flags: []cli.Flag{appFlag},
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)
Expand All @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion env/display.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package env

import (
"errors"
"fmt"

"github.com/Scalingo/cli/config"
Expand All @@ -14,11 +15,29 @@ 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 {
fmt.Printf("%s=%s\n", v.Name, v.Value)
}
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used VariablesListWithoutAlias rather than VariablesList. For instance, if a customer execute env-get DATABASE_URL, I want to display the actual value rather than $SCALINGO_POSTGRESQL_VALUE.

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")
}