-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstring_slice.go
More file actions
69 lines (60 loc) · 1.37 KB
/
string_slice.go
File metadata and controls
69 lines (60 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package logify
import (
"strings"
)
// StringSlice is a custom type for string slices with file support
type StringSlice struct {
values []string
supportFile bool
}
// NewStringSlice creates a new StringSlice
func NewStringSlice(supportFile bool) *StringSlice {
return &StringSlice{
values: []string{},
supportFile: supportFile,
}
}
// String implements flag.Value interface
func (s *StringSlice) String() string {
return strings.Join(s.values, ",")
}
// Set implements flag.Value interface
func (s *StringSlice) Set(value string) error {
// Check if it's a file
if s.supportFile && fileExists(value) {
lines, err := readLines(value)
if err != nil {
return err
}
s.values = append(s.values, lines...)
return nil
}
// Support comma-separated values
if strings.Contains(value, ",") {
parts := strings.Split(value, ",")
for _, part := range parts {
trimmed := strings.TrimSpace(part)
if trimmed != "" {
s.values = append(s.values, trimmed)
}
}
} else {
trimmed := strings.TrimSpace(value)
if trimmed != "" {
s.values = append(s.values, trimmed)
}
}
return nil
}
// Get returns the slice values
func (s *StringSlice) Get() []string {
return s.values
}
// Len returns the number of elements
func (s *StringSlice) Len() int {
return len(s.values)
}
// Reset clears all values
func (s *StringSlice) Reset() {
s.values = []string{}
}