Trim quotes from TLS flags.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-01-03 15:58:41 -05:00 committed by Vincent Demeester
parent 8eeed60a68
commit 52e9a69df9
2 changed files with 17 additions and 6 deletions

View File

@ -2,11 +2,13 @@ package opts
// QuotedString is a string that may have extra quotes around the value. The // QuotedString is a string that may have extra quotes around the value. The
// quotes are stripped from the value. // quotes are stripped from the value.
type QuotedString string type QuotedString struct {
value *string
}
// Set sets a new value // Set sets a new value
func (s *QuotedString) Set(val string) error { func (s *QuotedString) Set(val string) error {
*s = QuotedString(trimQuotes(val)) *s.value = trimQuotes(val)
return nil return nil
} }
@ -16,7 +18,7 @@ func (s *QuotedString) Type() string {
} }
func (s *QuotedString) String() string { func (s *QuotedString) String() string {
return string(*s) return string(*s.value)
} }
func trimQuotes(value string) string { func trimQuotes(value string) string {
@ -28,3 +30,8 @@ func trimQuotes(value string) string {
} }
return value return value
} }
// NewQuotedString returns a new quoted string option
func NewQuotedString(value *string) *QuotedString {
return &QuotedString{value: value}
}

View File

@ -6,19 +6,23 @@ import (
) )
func TestQuotedStringSetWithQuotes(t *testing.T) { func TestQuotedStringSetWithQuotes(t *testing.T) {
qs := QuotedString("") value := ""
qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("\"something\"")) assert.NilError(t, qs.Set("\"something\""))
assert.Equal(t, qs.String(), "something") assert.Equal(t, qs.String(), "something")
assert.Equal(t, value, "something")
} }
func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) { func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
qs := QuotedString("") value := ""
qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("\"something'")) assert.NilError(t, qs.Set("\"something'"))
assert.Equal(t, qs.String(), "\"something'") assert.Equal(t, qs.String(), "\"something'")
} }
func TestQuotedStringSetWithNoQuotes(t *testing.T) { func TestQuotedStringSetWithNoQuotes(t *testing.T) {
qs := QuotedString("") value := ""
qs := NewQuotedString(&value)
assert.NilError(t, qs.Set("something")) assert.NilError(t, qs.Set("something"))
assert.Equal(t, qs.String(), "something") assert.Equal(t, qs.String(), "something")
} }