summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2022-04-11 14:59:03 +0200
committerMartin Polden <mpolden@mpolden.no>2022-04-11 15:00:47 +0200
commit7fef8cb3cecc0e78bad0732780d53557a9086d69 (patch)
treef01494170632f588367e68f4f9506e2eaf0274d9
parentd3fbdda80bdaba506c9b2c8c0c37de5182d00053 (diff)
Add config unset command
-rw-r--r--client/go/cmd/config.go44
-rw-r--r--client/go/cmd/config_test.go10
-rw-r--r--client/go/cmd/root.go1
-rw-r--r--client/go/config/config.go7
-rw-r--r--client/go/config/config_test.go8
5 files changed, 64 insertions, 6 deletions
diff --git a/client/go/cmd/config.go b/client/go/cmd/config.go
index 4fa40d02433..b3a0c8133c1 100644
--- a/client/go/cmd/config.go
+++ b/client/go/cmd/config.go
@@ -153,6 +153,42 @@ $ vespa config set --local wait 600
return cmd
}
+func newConfigUnsetCmd(cli *CLI) *cobra.Command {
+ var localArg bool
+ cmd := &cobra.Command{
+ Use: "unset option-name",
+ Short: "Unset a configuration option.",
+ Long: `Unset a configuration option.
+
+Unsetting a configuration option will reset it to its default value, which may be empty.
+`,
+ Example: `# Reset target to its default value
+$ vespa config unset target
+
+# Stop overriding application option in local config
+$ vespa config unset --local application
+`,
+ DisableAutoGenTag: true,
+ SilenceUsage: true,
+ Args: cobra.ExactArgs(1),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ config := cli.config
+ if localArg {
+ if _, err := cli.applicationPackageFrom(nil, false); err != nil {
+ return fmt.Errorf("failed to write local configuration: %w", err)
+ }
+ config = cli.config.local
+ }
+ if err := config.unset(args[0]); err != nil {
+ return err
+ }
+ return config.write()
+ },
+ }
+ cmd.Flags().BoolVarP(&localArg, "local", "l", false, "Unset option in local configuration, i.e. for the current application")
+ return cmd
+}
+
func newConfigGetCmd(cli *CLI) *cobra.Command {
var localArg bool
cmd := &cobra.Command{
@@ -542,6 +578,14 @@ func (c *Config) set(option, value string) error {
return fmt.Errorf("invalid option or value: %s = %s", option, value)
}
+func (c *Config) unset(option string) error {
+ if err := c.checkOption(option); err != nil {
+ return err
+ }
+ c.config.Del(option)
+ return nil
+}
+
func (c *Config) checkOption(option string) error {
if _, ok := c.flags[option]; !ok {
return fmt.Errorf("invalid option: %s", option)
diff --git a/client/go/cmd/config_test.go b/client/go/cmd/config_test.go
index a3ce7ee959d..3a8766aae13 100644
--- a/client/go/cmd/config_test.go
+++ b/client/go/cmd/config_test.go
@@ -4,7 +4,6 @@ package cmd
import (
"os"
"path/filepath"
- "strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -51,6 +50,8 @@ func TestConfig(t *testing.T) {
assertConfigCommandErr(t, configHome, "Error: invalid option or value: color = foo\n", "config", "set", "color", "foo")
assertConfigCommand(t, configHome, "", "config", "set", "color", "never")
assertConfigCommand(t, configHome, "color = never\n", "config", "get", "color")
+ assertConfigCommand(t, configHome, "", "config", "unset", "color")
+ assertConfigCommand(t, configHome, "color = auto\n", "config", "get", "color")
// quiet
assertConfigCommand(t, configHome, "", "config", "set", "quiet", "true")
@@ -60,11 +61,14 @@ func TestConfig(t *testing.T) {
assertConfigCommand(t, configHome, "", "config", "set", "zone", "dev.us-east-1")
assertConfigCommand(t, configHome, "zone = dev.us-east-1\n", "config", "get", "zone")
- // Write empty value, which should be ignored. This is for compatibility with older config formats
+ // Write empty value to YAML config, which should be ignored. This is for compatibility with older config formats
configFile := filepath.Join(configHome, "config.yaml")
+ assertConfigCommand(t, configHome, "", "config", "unset", "zone")
data, err := os.ReadFile(configFile)
require.Nil(t, err)
- config := strings.ReplaceAll(string(data), "dev.us-east-1", `""`)
+ yamlConfig := string(data)
+ assert.NotContains(t, yamlConfig, "zone:")
+ config := yamlConfig + "zone: \"\"\n"
require.Nil(t, os.WriteFile(configFile, []byte(config), 0600))
assertConfigCommand(t, configHome, "zone = <unset>\n", "config", "get", "zone")
}
diff --git a/client/go/cmd/root.go b/client/go/cmd/root.go
index f5e4a3d6016..e88398a7fde 100644
--- a/client/go/cmd/root.go
+++ b/client/go/cmd/root.go
@@ -229,6 +229,7 @@ func (c *CLI) configureCommands() {
rootCmd.AddCommand(newCloneCmd(c)) // clone
configCmd.AddCommand(newConfigGetCmd(c)) // config get
configCmd.AddCommand(newConfigSetCmd(c)) // config set
+ configCmd.AddCommand(newConfigUnsetCmd(c)) // config unset
rootCmd.AddCommand(configCmd) // config
rootCmd.AddCommand(newCurlCmd(c)) // curl
rootCmd.AddCommand(newDeployCmd(c)) // deploy
diff --git a/client/go/config/config.go b/client/go/config/config.go
index 7e859d9908a..beffff6e257 100644
--- a/client/go/config/config.go
+++ b/client/go/config/config.go
@@ -46,6 +46,13 @@ func (c *Config) Set(key, value string) {
c.values[key] = value
}
+// Del removes the value associated with key.
+func (c *Config) Del(key string) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ delete(c.values, key)
+}
+
// Write writes config in YAML format to writer w.
func (c *Config) Write(w io.Writer) error {
c.mu.RLock()
diff --git a/client/go/config/config_test.go b/client/go/config/config_test.go
index 8bc3835efae..1458771a5f5 100644
--- a/client/go/config/config_test.go
+++ b/client/go/config/config_test.go
@@ -15,12 +15,14 @@ func TestConfig(t *testing.T) {
config := New()
config.Set("key1", "value1")
config.Set("key2", "value2")
- assert.Equal(t, []string{"key1", "key2"}, config.Keys())
+ config.Set("key3", "value3")
+ assert.Equal(t, []string{"key1", "key2", "key3"}, config.Keys())
- v, ok := config.Get("key1")
+ v, ok := config.Get("key3")
assert.True(t, ok)
- assert.Equal(t, "value1", v)
+ assert.Equal(t, "value3", v)
+ config.Del("key3")
_, ok = config.Get("key3")
assert.False(t, ok)