-
Notifications
You must be signed in to change notification settings - Fork 4
/
glob.go
99 lines (87 loc) · 2.34 KB
/
glob.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package flagvar
import (
"fmt"
"path/filepath"
"strings"
"github.com/gobwas/glob"
)
// Glob is a `flag.Value` for glob syntax arguments.
// By default, `filepath.Separator` is used as a separator.
// If `Separators` is non-nil, its elements are used as separators.
// To have no separators, set `Separators` to a (non-nil) pointer to an empty slice.
type Glob struct {
Separators *[]rune
Value glob.Glob
Text string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Glob) Help() string {
separators := []rune{filepath.Separator}
if fv.Separators != nil {
separators = *fv.Separators
}
if len(separators) == 0 {
return "a glob expression"
}
if len(separators) == 1 {
return fmt.Sprintf("a glob expression with separator %q", separators[0])
}
return fmt.Sprintf("a glob expression with separators %q", separators)
}
// Set is flag.Value.Set
func (fv *Glob) Set(v string) error {
separators := fv.Separators
if separators == nil {
separators = &[]rune{filepath.Separator}
}
g, err := glob.Compile(v, *separators...)
if err != nil {
return err
}
fv.Text = v
fv.Value = g
return nil
}
func (fv *Glob) String() string {
return fv.Text
}
// Globs is a `flag.Value` for glob syntax arguments.
// By default, `filepath.Separator` is used as a separator.
// If `Separators` is non-nil, its elements are used as separators.
// To have no separators, set `Separators` to a (non-nil) pointer to an empty slice.
type Globs struct {
Separators *[]rune
Values []glob.Glob
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Globs) Help() string {
separators := []rune{filepath.Separator}
if fv.Separators != nil {
separators = *fv.Separators
}
if len(separators) == 0 {
return "a glob expression"
}
if len(separators) == 1 {
return fmt.Sprintf("a glob expression with separator %q", separators[0])
}
return fmt.Sprintf("a glob expression with separators %q", separators)
}
// Set is flag.Value.Set
func (fv *Globs) Set(v string) error {
separators := fv.Separators
if separators == nil {
separators = &[]rune{filepath.Separator}
}
g, err := glob.Compile(v, *separators...)
if err != nil {
return err
}
fv.Texts = append(fv.Texts, v)
fv.Values = append(fv.Values, g)
return nil
}
func (fv *Globs) String() string {
return strings.Join(fv.Texts, ",")
}