2016-12-29 17:28:28 -05:00
|
|
|
package opts
|
|
|
|
|
|
|
|
// QuotedString is a string that may have extra quotes around the value. The
|
|
|
|
// quotes are stripped from the value.
|
2017-01-03 15:58:41 -05:00
|
|
|
type QuotedString struct {
|
|
|
|
value *string
|
|
|
|
}
|
2016-12-29 17:28:28 -05:00
|
|
|
|
|
|
|
// Set sets a new value
|
|
|
|
func (s *QuotedString) Set(val string) error {
|
2017-01-03 15:58:41 -05:00
|
|
|
*s.value = trimQuotes(val)
|
2016-12-29 17:28:28 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of the value
|
|
|
|
func (s *QuotedString) Type() string {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *QuotedString) String() string {
|
2017-01-03 15:58:41 -05:00
|
|
|
return string(*s.value)
|
2016-12-29 17:28:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-01-03 15:58:41 -05:00
|
|
|
|
|
|
|
// NewQuotedString returns a new quoted string option
|
|
|
|
func NewQuotedString(value *string) *QuotedString {
|
|
|
|
return &QuotedString{value: value}
|
|
|
|
}
|