From ed2ad860c69738dc7e2fe876e16f57236ba6790c Mon Sep 17 00:00:00 2001 From: Narayanan Singaram Date: Mon, 4 Mar 2019 13:19:18 -0800 Subject: [PATCH] Move trim quotes logic to separate function --- pkg/commands/edit/add/addmetadata.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/commands/edit/add/addmetadata.go b/pkg/commands/edit/add/addmetadata.go index f39bc3bb1..cbb3bc8a7 100644 --- a/pkg/commands/edit/add/addmetadata.go +++ b/pkg/commands/edit/add/addmetadata.go @@ -152,11 +152,7 @@ func (o *addMetadataOptions) convertToMap(arg string) (map[string]string, error) } // remove quotes if value is quoted - if len(result[kv[0]]) > 0 && - result[kv[0]][:1] == "\"" && - result[kv[0]][len(result[kv[0]])-1:] == "\"" { - result[kv[0]] = result[kv[0]][1 : len(result[kv[0]])-1] - } + result[kv[0]] = trimQuotes(result[kv[0]]) } return result, nil } @@ -188,3 +184,12 @@ func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) err func (o *addMetadataOptions) makeError(input string, message string) error { return fmt.Errorf("invalid %s: %s (%s)", o.kind, input, message) } + +func trimQuotes(s string) string { + if len(s) >= 2 { + if s[0] == '"' && s[len(s)-1] == '"' { + return s[1 : len(s)-1] + } + } + return s +}