2023-08-19 13:25:53 +00:00
|
|
|
package types
|
2022-10-31 14:37:50 +00:00
|
|
|
|
2023-08-19 13:25:53 +00:00
|
|
|
import "strings"
|
2022-10-31 14:37:50 +00:00
|
|
|
|
2023-02-08 09:16:01 +00:00
|
|
|
// StringSliceFlag is a flag type which support comma separated values and escaping to not split at unwanted lines.
|
2022-10-31 14:37:50 +00:00
|
|
|
type StringSliceFlag struct {
|
|
|
|
slice []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StringSliceFlag) String() string {
|
|
|
|
return strings.Join(s.slice, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StringSliceFlag) Set(value string) error {
|
|
|
|
s.slice = splitWithEscaping(value, ",", "\\")
|
2023-02-08 09:16:01 +00:00
|
|
|
|
2022-10-31 14:37:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StringSliceFlag) Get() []string {
|
|
|
|
return s.slice
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:16:01 +00:00
|
|
|
func splitWithEscaping(in, separator, escapeString string) []string {
|
|
|
|
if len(in) == 0 {
|
2022-10-31 14:37:50 +00:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:16:01 +00:00
|
|
|
out := strings.Split(in, separator)
|
2022-10-31 14:37:50 +00:00
|
|
|
|
2024-05-12 09:08:23 +00:00
|
|
|
//nolint:mnd
|
2023-02-08 09:16:01 +00:00
|
|
|
for i := len(out) - 2; i >= 0; i-- {
|
|
|
|
if strings.HasSuffix(out[i], escapeString) {
|
|
|
|
out[i] = out[i][:len(out[i])-len(escapeString)] + separator + out[i+1]
|
|
|
|
out = append(out[:i+1], out[i+2:]...)
|
2022-10-31 14:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:16:01 +00:00
|
|
|
return out
|
2022-10-31 14:37:50 +00:00
|
|
|
}
|