feat: add config:clear command

Closes #3537
This commit is contained in:
Jose Diaz-Gonzalez
2019-05-20 13:25:04 -07:00
parent a34d36c5d2
commit f977edabb2
7 changed files with 75 additions and 3 deletions

View File

@@ -6,12 +6,13 @@ The `config` plugin provides the following commands to manage your variables:
```
config (<app>|--global) Pretty-print an app or global environment
config:bundle (<app>|--global) [--merged] Bundle environment into tarfile
config:clear (<app>|--global) Clears environment variables
config:export (<app>|--global) [--envfile] Export a global or app environment
config:get (<app>|--global) KEY Display a global or app-specific config value
config:keys (<app>|--global) [--merged] Show keys set in environment
config:set [--encoded] [--no-restart] (<app>|--global) KEY1=VALUE1 [KEY2=VALUE2 ...] Set one or more config vars
config:unset [--no-restart] (<app>|--global) KEY1 [KEY2 ...] Unset one or more config vars
config:export (<app>|--global) [--envfile] Export a global or app environment
config:keys (<app>|--global) [--merged] Show keys set in environment
config:bundle (<app>|--global) [--merged] Bundle environment into tarfile
```
> For security reasons - and as per [docker recommendations](https://github.com/docker/docker/issues/13490) - Dockerfile-based deploys have variables available *only* during runtime, as noted in [this issue](https://github.com/dokku/dokku/issues/1860).

View File

@@ -92,6 +92,29 @@ func UnsetMany(appName string, keys []string, restart bool) (err error) {
return
}
//UnsetAll removes all config keys
func UnsetAll(appName string, restart bool) (err error) {
global := appName == ""
env, err := loadAppOrGlobalEnv(appName)
if err != nil {
return
}
var changed = false
for k := range env.Map() {
common.LogInfo1Quiet(fmt.Sprintf("Unsetting %s", k))
env.Unset(k)
changed = true
}
if changed {
env.Write()
triggerUpdate(appName, "clear", []string{})
}
if !global && restart && env.GetBoolDefault("DOKKU_APP_RESTORE", true) {
triggerRestart(appName)
}
return
}
func triggerRestart(appName string) {
common.LogInfo1(fmt.Sprintf("Restarting app %s", appName))
if err := common.PlugnTrigger("app-restart", appName); err != nil {

View File

@@ -80,6 +80,22 @@ func TestConfigSetMany(t *testing.T) {
Expect(SetMany(testAppName+"does_not_exist", vals, false)).ToNot(Succeed())
}
func TestConfigUnsetAll(t *testing.T) {
RegisterTestingT(t)
Expect(setupTestApp()).To(Succeed())
defer teardownTestApp()
expectValue(testAppName, "testKey", "TESTING")
expectValue("", "testKey", "GLOBAL_TESTING")
Expect(UnsetAll(testAppName, false)).To(Succeed())
expectNoValue(testAppName, "testKey")
expectNoValue(testAppName, "noKey")
expectNoValue(testAppName, "globalKey")
Expect(UnsetAll(testAppName+"does-not-exist", false)).ToNot(Succeed())
}
func TestConfigUnsetMany(t *testing.T) {
RegisterTestingT(t)
Expect(setupTestApp()).To(Succeed())

View File

@@ -46,6 +46,11 @@ config_get() {
config_sub get "$@"
}
config_clear() {
declare desc="clears config vars"
config_sub clear "$@"
}
config_set() {
declare desc="set value of given config var"
config_sub set "$@"

View File

@@ -22,6 +22,7 @@ Additional commands:`
helpContent = `
config (<app>|--global), Pretty-print an app or global environment
config:bundle (<app>|--global) [--merged], Bundle environment into tarfile
config:clear (<app>|--global), Clears environment variables
config:export (<app>|--global) [--envfile], Export a global or app environment
config:get (<app>|--global) KEY, Display a global or app-specific config value
config:keys (<app>|--global) [--merged], Show keys set in environment

View File

@@ -0,0 +1,17 @@
package main
import (
"flag"
"os"
"github.com/dokku/dokku/plugins/config"
)
//clear all entries from the given environment
func main() {
args := flag.NewFlagSet("config:clear", flag.ExitOnError)
global := args.Bool("global", false, "--global: use the global environment")
noRestart := args.Bool("no-restart", false, "--no-restart: no restart")
args.Parse(os.Args[2:])
config.CommandClear(args.Args(), *global, *noRestart)
}

View File

@@ -50,6 +50,15 @@ func CommandGet(args []string, global bool, quoted bool) {
}
}
//CommandClear implements config:clear
func CommandClear(args []string, global bool, noRestart bool) {
appName, _ := getCommonArgs(global, args)
err := UnsetAll(appName, !noRestart)
if err != nil {
common.LogFail(err.Error())
}
}
//CommandUnset implements config:unset
func CommandUnset(args []string, global bool, noRestart bool) {
appName, keys := getCommonArgs(global, args)