Skip to content

Commit 53fe213

Browse files
fix(deisctl): don't panic when config key/value is malformed
1 parent 293d657 commit 53fe213

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

deisctl/config/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"os"
9-
"strings"
9+
"regexp"
1010

1111
"github.com/deis/deis/deisctl/utils"
1212
)
@@ -65,12 +65,17 @@ func doConfig(target string, action string, key []string, cb Backend, w io.Write
6565

6666
func doConfigSet(cb Backend, root string, kvs []string) ([]string, error) {
6767
var result []string
68+
regex := regexp.MustCompile(`^(.+)=([\s\S]+)$`)
6869

6970
for _, kv := range kvs {
7071

72+
if !regex.MatchString(kv) {
73+
return []string{}, fmt.Errorf("'%s' does not match the pattern 'key=var', ex: foo=bar\n", kv)
74+
}
75+
7176
// split k/v from args
72-
split := strings.SplitN(kv, "=", 2)
73-
k, v := split[0], split[1]
77+
captures := regex.FindStringSubmatch(kv)
78+
k, v := captures[1], captures[2]
7479

7580
// prepare path and value
7681
path := root + k

deisctl/config/config_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"bytes"
55
"encoding/base64"
6-
"fmt"
76
"io/ioutil"
87
"os"
98
"testing"
@@ -27,7 +26,7 @@ func TestGetConfig(t *testing.T) {
2726
expected := "foo\n8000\n"
2827
output := testWriter.String()
2928
if output != expected {
30-
t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
29+
t.Errorf("Expected: '%s', Got:'%s'", expected, output)
3130
}
3231
}
3332

@@ -59,7 +58,21 @@ func TestSetConfig(t *testing.T) {
5958
expected := "bar\n1000\n"
6059
output := testWriter.String()
6160
if output != expected {
62-
t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
61+
t.Errorf("Expected: '%s', Got:'%s'", expected, output)
62+
}
63+
}
64+
65+
func TestSetConfigError(t *testing.T) {
66+
t.Parallel()
67+
68+
testMock := mock.ConfigBackend{Expected: []*model.ConfigNode{}}
69+
testWriter := bytes.Buffer{}
70+
71+
expected := "'foo' does not match the pattern 'key=var', ex: foo=bar\n"
72+
err := doConfig("controller", "set", []string{"foo", "=", "bar"}, testMock, &testWriter)
73+
74+
if err.Error() != expected {
75+
t.Errorf("Expected: '%s', Got:'%q'", expected, err)
6376
}
6477
}
6578

@@ -78,7 +91,7 @@ func TestDeleteConfig(t *testing.T) {
7891
expected := "testing\nport\n"
7992
output := testWriter.String()
8093
if output != expected {
81-
t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
94+
t.Errorf("Expected: '%s', Got:'%s'", expected, output)
8295
}
8396
}
8497

0 commit comments

Comments
 (0)