-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoglobls.go
150 lines (122 loc) · 2.76 KB
/
goglobls.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/kr/fs"
glob "github.com/obeattie/ohmyglob"
yaml "gopkg.in/yaml.v2"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: goglobls [options] [path...]\n")
flag.PrintDefaults()
}
// Config Configuration structure
type Config struct {
Types []struct {
Name string `yaml:"name"`
Paths []string `yaml:"paths"`
}
}
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func main() {
flag.Usage = usage
configFile := flag.String("config", "", "Config file (required)")
ignoreCase := flag.Bool("i", false, "Ignore case (Unix Only)")
verbose := flag.Bool("v", false, "Verbose")
var types arrayFlags
flag.Var(&types, "type", "Type to filter (add more than one by repeating the option)")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
usage()
fmt.Fprintln(os.Stderr, "Path is missing.")
os.Exit(1)
}
if *configFile == "" {
usage()
fmt.Fprintln(os.Stderr, "Config file is missing.")
os.Exit(1)
}
data, err := ioutil.ReadFile(*configFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
config := Config{}
err = yaml.Unmarshal(data, &config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
patterns := make([]string, 0)
for _, _type := range config.Types {
if len(types) > 0 {
foundType, _ := inArray(_type.Name, types)
if !foundType {
continue
}
}
for _, pattern := range _type.Paths {
if runtime.GOOS == "windows" {
patterns = append(patterns, strings.ToLower(pattern))
} else {
if *ignoreCase == true {
patterns = append(patterns, strings.ToLower(pattern))
} else {
patterns = append(patterns, pattern)
}
}
}
}
if *verbose {
fmt.Printf("Patterns:\n")
fmt.Printf("%v\n", patterns)
}
set, _ := glob.CompileGlobSet(patterns, glob.DefaultOptions)
walker := fs.Walk(args[0])
for walker.Step() {
if err := walker.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
var target string
if runtime.GOOS == "windows" {
target = strings.ToLower(strings.Replace(walker.Path(), "\\", "/", -1))
} else {
if *ignoreCase == true {
target = strings.ToLower(walker.Path())
} else {
target = walker.Path()
}
}
match := walker.Stat().Mode().IsRegular() && set.MatchString(target)
if match && !*verbose {
fmt.Printf("%v\n", walker.Path())
} else if *verbose {
fmt.Printf("Match of %v is %v\n", target, match)
}
}
}
func inArray(val string, array []string) (exists bool, index int) {
exists = false
index = -1
for i, v := range array {
if val == v {
index = i
exists = true
return
}
}
return
}