Trim quotes from TLS flags.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
Upstream-commit: 52e9a69df9
Component: cli
This commit is contained in:
Daniel Nephin
2017-05-15 11:57:21 +02:00
committed by Vincent Demeester
parent 41104f7868
commit 209e223594
2 changed files with 17 additions and 6 deletions
+10 -3
View File
@@ -2,11 +2,13 @@ package opts
// QuotedString is a string that may have extra quotes around the value. The
// quotes are stripped from the value.
type QuotedString string
type QuotedString struct {
value *string
}
// Set sets a new value
func (s *QuotedString) Set(val string) error {
*s = QuotedString(trimQuotes(val))
*s.value = trimQuotes(val)
return nil
}
@@ -16,7 +18,7 @@ func (s *QuotedString) Type() string {
}
func (s *QuotedString) String() string {
return string(*s)
return string(*s.value)
}
func trimQuotes(value string) string {
@@ -28,3 +30,8 @@ func trimQuotes(value string) string {
}
return value
}
// NewQuotedString returns a new quoted string option
func NewQuotedString(value *string) *QuotedString {
return &QuotedString{value: value}
}
+7 -3
View File
@@ -6,19 +6,23 @@ import (
)
func TestQuotedStringSetWithQuotes(t *testing.T) {
qs := QuotedString("")
value := ""
qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("\"something\""))
assert.Equal(t, qs.String(), "something")
assert.Equal(t, value, "something")
}
func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
qs := QuotedString("")
value := ""
qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("\"something'"))
assert.Equal(t, qs.String(), "\"something'")
}
func TestQuotedStringSetWithNoQuotes(t *testing.T) {
qs := QuotedString("")
value := ""
qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("something"))
assert.Equal(t, qs.String(), "something")
}