mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add quoted string flag Value.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
2ef6d80454
commit
e4c1f07729
2 changed files with 54 additions and 0 deletions
30
opts/quotedstring.go
Normal file
30
opts/quotedstring.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package opts
|
||||
|
||||
// QuotedString is a string that may have extra quotes around the value. The
|
||||
// quotes are stripped from the value.
|
||||
type QuotedString string
|
||||
|
||||
// Set sets a new value
|
||||
func (s *QuotedString) Set(val string) error {
|
||||
*s = QuotedString(trimQuotes(val))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Type returns the type of the value
|
||||
func (s *QuotedString) Type() string {
|
||||
return "string"
|
||||
}
|
||||
|
||||
func (s *QuotedString) String() string {
|
||||
return string(*s)
|
||||
}
|
||||
|
||||
func trimQuotes(value string) string {
|
||||
lastIndex := len(value) - 1
|
||||
for _, char := range []byte{'\'', '"'} {
|
||||
if value[0] == char && value[lastIndex] == char {
|
||||
return value[1:lastIndex]
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
24
opts/quotedstring_test.go
Normal file
24
opts/quotedstring_test.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package opts
|
||||
|
||||
import (
|
||||
"github.com/docker/docker/pkg/testutil/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestQuotedStringSetWithQuotes(t *testing.T) {
|
||||
qs := QuotedString("")
|
||||
assert.NilError(t, qs.Set("\"something\""))
|
||||
assert.Equal(t, qs.String(), "something")
|
||||
}
|
||||
|
||||
func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
|
||||
qs := QuotedString("")
|
||||
assert.NilError(t, qs.Set("\"something'"))
|
||||
assert.Equal(t, qs.String(), "\"something'")
|
||||
}
|
||||
|
||||
func TestQuotedStringSetWithNoQuotes(t *testing.T) {
|
||||
qs := QuotedString("")
|
||||
assert.NilError(t, qs.Set("something"))
|
||||
assert.Equal(t, qs.String(), "something")
|
||||
}
|
Loading…
Reference in a new issue