Signed-off-by: Daniel Nephin <dnephin@docker.com>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,30 @@ |
| 0 |
+package opts |
|
| 1 |
+ |
|
| 2 |
+// QuotedString is a string that may have extra quotes around the value. The |
|
| 3 |
+// quotes are stripped from the value. |
|
| 4 |
+type QuotedString string |
|
| 5 |
+ |
|
| 6 |
+// Set sets a new value |
|
| 7 |
+func (s *QuotedString) Set(val string) error {
|
|
| 8 |
+ *s = QuotedString(trimQuotes(val)) |
|
| 9 |
+ return nil |
|
| 10 |
+} |
|
| 11 |
+ |
|
| 12 |
+// Type returns the type of the value |
|
| 13 |
+func (s *QuotedString) Type() string {
|
|
| 14 |
+ return "string" |
|
| 15 |
+} |
|
| 16 |
+ |
|
| 17 |
+func (s *QuotedString) String() string {
|
|
| 18 |
+ return string(*s) |
|
| 19 |
+} |
|
| 20 |
+ |
|
| 21 |
+func trimQuotes(value string) string {
|
|
| 22 |
+ lastIndex := len(value) - 1 |
|
| 23 |
+ for _, char := range []byte{'\'', '"'} {
|
|
| 24 |
+ if value[0] == char && value[lastIndex] == char {
|
|
| 25 |
+ return value[1:lastIndex] |
|
| 26 |
+ } |
|
| 27 |
+ } |
|
| 28 |
+ return value |
|
| 29 |
+} |
| 0 | 30 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,24 @@ |
| 0 |
+package opts |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "github.com/docker/docker/pkg/testutil/assert" |
|
| 4 |
+ "testing" |
|
| 5 |
+) |
|
| 6 |
+ |
|
| 7 |
+func TestQuotedStringSetWithQuotes(t *testing.T) {
|
|
| 8 |
+ qs := QuotedString("")
|
|
| 9 |
+ assert.NilError(t, qs.Set("\"something\""))
|
|
| 10 |
+ assert.Equal(t, qs.String(), "something") |
|
| 11 |
+} |
|
| 12 |
+ |
|
| 13 |
+func TestQuotedStringSetWithMismatchedQuotes(t *testing.T) {
|
|
| 14 |
+ qs := QuotedString("")
|
|
| 15 |
+ assert.NilError(t, qs.Set("\"something'"))
|
|
| 16 |
+ assert.Equal(t, qs.String(), "\"something'") |
|
| 17 |
+} |
|
| 18 |
+ |
|
| 19 |
+func TestQuotedStringSetWithNoQuotes(t *testing.T) {
|
|
| 20 |
+ qs := QuotedString("")
|
|
| 21 |
+ assert.NilError(t, qs.Set("something"))
|
|
| 22 |
+ assert.Equal(t, qs.String(), "something") |
|
| 23 |
+} |