This repository has been archived by the owner on Aug 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
517 lines (456 loc) · 11.5 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package main
import (
"fmt"
"strings"
"flag"
"os"
"os/user"
"os/exec"
"path/filepath"
"bytes"
"encoding/gob"
"text/template"
re "regexp"
"github.com/vaughan0/go-logging"
"github.com/kylelemons/go-gypsy/yaml"
"codetag/log_setup"
tgrs "codetag/taggers"
)
// Path with a few extra convenience methods.
type path_t string
// Expand paths like "~/path" using HOME env var or /etc/passwd.
func (path path_t) ExpandUser() (path_ret path_t, err error) {
path_str := filepath.Clean(string(path))
if !strings.HasPrefix(path_str, "~/") {
return path, nil
}
parts := strings.SplitN(path_str, "/", 2)
parts[0] = os.Getenv("HOME")
if len(parts[0]) == 0 {
user, err := user.Current()
if err != nil {
parts = nil
} else {
parts[0] = user.HomeDir
}
}
if parts != nil {
return path_t(strings.Join(parts, "/")), nil
}
return path, err
}
// Default places to look for config file.
// First one is dynamic, depending on argv[0].
var config_search = []path_t{"", "~/.codetag.yaml", "/etc/codetag.yaml"}
// CLI
// Config file that is used.
var config_path string
// Don't actually run tmsu
var dry_run bool
type ctx_t map[string]map[string]interface{}
// Clone context object using gob serialization
func ctx_clone(src, dst interface{}) {
var err error
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
err = enc.Encode(src)
if err != nil {
panic(err)
}
err = dec.Decode(dst)
if err != nil {
panic(err)
}
}
type ctx_stack_t struct {
slug string
ctx ctx_t
}
// Parsed filters.
type path_filter struct {
verdict bool
pattern *re.Regexp
}
type path_filters []path_filter
// Match path against a list of regexp-filters and return whether it
// should be processed or not.
func (filters *path_filters) match(path string) bool {
for _, filter := range *filters {
if filter.pattern.Match([]byte(path)) {
return filter.verdict
}
}
return true
}
// Writer which line-buffers data, passing each line to log_func
type log_pipe struct {
log_func func(string)
buff string ""
}
func (pipe *log_pipe) Log(line string) {
pipe.log_func(strings.TrimSpace(line))
}
func (pipe *log_pipe) Write(p []byte) (n int, err error) {
pipe.buff += string(p)
for strings.Contains(pipe.buff, "\n") {
lines := strings.SplitN(pipe.buff, "\n", 2)
pipe.Log(lines[0])
pipe.buff = lines[1]
}
return len(p), nil
}
func (pipe *log_pipe) Flush() {
if len(pipe.buff) > 0 {
pipe.Log(pipe.buff)
}
pipe.buff = ""
}
func init() {
gob.Register(tgrs.CtxTagset{})
}
func main() {
config_search[0] = path_t(os.Args[0] + ".yaml")
flag.Usage = func() {
tpl := template.Must(template.New("test").Parse(""+
`usage: {{.cmd}} [ <options> ]
Index code files, using parameters specified in the config file.
If not specified exmplicitly, config file is searched within the
following paths (in that order):
{{range .paths}} - {{.}}
{{end}}
Examples:
% {{.cmd}}
% {{.cmd}} --config config.yaml
Options:
`))
tpl.Execute(os.Stdout, map[string]interface{}{"cmd": os.Args[0], "paths": config_search})
flag.PrintDefaults()
}
flag.StringVar(&config_path, "config", "", "Configuration file to use.")
flag.BoolVar(&dry_run, "dry-run", false, "Don't actually run tmsu, just process all paths.")
flag.Parse()
if flag.NArg() > 0 {
fmt.Fprintf(os.Stderr, "Error: no command-line"+
" arguments are allowed (provided: %v)\n", flag.Args())
os.Exit(1)
}
// Find config path to use
if len(config_path) == 0 {
for _, path := range config_search {
path, err := path.ExpandUser()
if err != nil {
continue
}
config_path = string(path)
_, err = os.Stat(config_path)
if err == nil {
break
}
config_path = ""
}
if len(config_path) == 0 {
fmt.Fprintf(os.Stderr, "Failed to find any suitable configuration file")
os.Exit(1)
}
}
var (
log *logging.Logger
log_init = false
config_init = false
)
defer func() {
if config_init {
return
}
// Recover only for configuration issues.
if err := recover(); err != nil {
if log_init {
log.Fatalf("Failed to process configuration file (%q): %v", config_path, err)
} else {
fmt.Fprintf(os.Stderr, "Failed to process configuration file (%q): %v\n", config_path, err)
}
os.Exit(1)
}
}()
// Read the config as yaml
config, err := yaml.ReadFile(config_path)
if err != nil {
panic(err)
os.Exit(1)
}
// Configure logging
log = logging.Get("codetag")
// Common processing vars
var (
ok bool
node yaml.Node
config_map yaml.Map
config_list yaml.List
)
node, err = yaml.Child(config.Root, ".logging")
if err != nil || node == nil {
logging.DefaultSetup()
log.Debugf("No logging config defined (err: %#v), using defaults", err)
} else {
config_map, ok = node.(yaml.Map)
if !ok {
logging.DefaultSetup()
log.Error("'logging' config section is not a map, ignoring")
} else {
err = log_setup.SetupYAML(config_map)
if err != nil {
logging.DefaultSetup()
log.Errorf("Failed to configure logging: %v", err)
}
}
}
log_init = true
// Configure filtering
filters := path_filters{}
node, err = yaml.Child(config.Root, ".filter")
if err != nil || node == nil {
log.Debug("No path-filters configured")
} else {
config_list, ok = node.(yaml.List)
if !ok {
log.Fatal("'filters' must be a list of string patterns")
os.Exit(1)
}
for _, node := range config_list {
pattern, ok := node.(yaml.Scalar)
if !ok {
log.Errorf("Pattern must be a string: %v", node)
continue
}
filter, pattern_str := path_filter{}, strings.Trim(string(pattern), "'")
filter.verdict = strings.HasPrefix(pattern_str, "+")
if !filter.verdict && !strings.HasPrefix(pattern_str, "-") {
log.Errorf("Pattern must start with either '+' or '-': %v", pattern_str)
continue
}
pattern_str = pattern_str[1:]
filter.pattern, err = re.Compile(pattern_str)
if err != nil {
log.Errorf("Failed to compile pattern (%v) as regexp: %v", pattern_str, err)
continue
}
filters = append(filters, filter)
}
}
// Get the list of paths to process
config_map, ok = config.Root.(yaml.Map)
if !ok {
log.Fatal("Config must be a map and have 'paths' key")
os.Exit(1)
}
node, ok = config_map["paths"]
if !ok {
log.Fatal("'paths' list must be defined in config")
os.Exit(1)
}
var paths []string
config_list, ok = node.(yaml.List)
if !ok {
path, ok := node.(yaml.Scalar)
if !ok {
log.Fatal("'paths' must be a list or (worst-case) scalar")
os.Exit(1)
}
paths = append(paths, string(path))
} else {
for _, node := range config_list {
path, ok := node.(yaml.Scalar)
if !ok {
log.Warnf("Skipped invalid path specification: %v", node)
} else {
paths = append(paths, string(path))
}
}
}
// Init taggers
node, ok = config_map["taggers"]
if ok {
config_map, ok = node.(yaml.Map)
}
if !ok {
log.Warn("No 'taggers' defined, nothing to do")
os.Exit(0)
}
taggers := make(map[string][]tgrs.Tagger)
init_tagger := func(ns, name string, config *yaml.Node) {
tagger, err := tgrs.Get(name, config, log)
if err != nil {
log.Warnf("Failed to init tagger %v (ns: %v): %v", ns, name, err)
} else {
taggers[ns] = append(taggers[ns], tagger)
}
}
for ns, node := range config_map {
if ns == "_none" {
ns = ""
}
if strings.HasPrefix(ns, "_") {
log.Warnf("Ignoring namespace name, starting with underscore: %v", ns)
continue
}
config_list, ok := node.(yaml.List)
if !ok {
// It's also ok to have "ns: tagger" spec, if there's just one for ns
tagger, ok := node.(yaml.Scalar)
if !ok {
log.Warnf("Invalid tagger(-list) specification (ns: %v): %v", ns, node)
continue
}
init_tagger(ns, string(tagger), nil)
continue
}
for _, node = range config_list {
tagger_map, ok := node.(yaml.Map)
if !ok {
tagger, ok := node.(yaml.Scalar)
if !ok {
log.Warnf("Invalid tagger specification - "+
"must be map or string (ns: %v): %v", ns, node)
continue
}
init_tagger(ns, string(tagger), nil)
continue
}
if len(tagger_map) != 1 {
log.Warnf("Invalid tagger specification - "+
"map must contain only one element (ns: %v): %v", ns, tagger_map)
continue
}
for tagger, node := range tagger_map {
init_tagger(ns, tagger, &node)
continue
}
}
}
config_init = true
// Walk the paths
var (
ctx ctx_t
ctx_stack_tuple ctx_stack_t
ctx_stack = make([]ctx_stack_t, 0)
ctx_tags tgrs.CtxTagset
)
log_tmsu := logging.Get("codetag.tmsu")
pipe := log_pipe{}
pipe.log_func = func(line string) {
log_tmsu.Debug(line)
}
tmsu_log_pipe := &pipe
ctx_stack = append(ctx_stack, ctx_stack_t{"", make(ctx_t)})
for _, root := range paths {
log.Tracef("Processing path: %s", root)
walk_iter := func (path string, info os.FileInfo, err error) (ret_err error) {
if err != nil {
log.Debugf(" - path: %v (info: %v), error: %v", path, info, err)
return
}
if !strings.HasPrefix(path, root) {
panic(fmt.Errorf("filepath.Walk went outside of root path (%v): %v", root, path))
}
path_match := path[len(root):]
if info.IsDir() {
path_match += "/"
}
if !filters.match(path_match) {
if info.IsDir() {
return filepath.SkipDir
}
return
}
// Get context for this path or copy it from parent path
n, slug := 0, path
for n, slug = range strings.Split(slug, fmt.Sprintf("%c", os.PathSeparator)) {
if len(ctx_stack) > n {
ctx_stack_tuple = ctx_stack[n]
if ctx_stack_tuple.slug == slug {
ctx = ctx_stack_tuple.ctx
} else {
ctx_stack_tuple = ctx_stack[n-1]
ctx = nil
ctx_clone(ctx_stack_tuple.ctx, &ctx)
ctx_stack[n] = ctx_stack_t{slug, ctx}
ctx_stack = ctx_stack[:n + 1]
break
}
} else {
ctx_stack_tuple = ctx_stack_t{slug, nil}
ctx_clone(ctx, &ctx_stack_tuple.ctx)
ctx_stack, ctx = append(ctx_stack, ctx_stack_tuple), ctx_stack_tuple.ctx
}
}
if len(ctx_stack) > n + 1 {
ctx_stack = ctx_stack[:n + 1]
}
// Run all taggers
for ns, tagger_list := range taggers {
ctx_ns, ok := ctx[ns]
if !ok {
ctx[ns] = make(map[string]interface{}, len(taggers) + 1)
ctx_ns = ctx[ns]
}
for _, tagger := range tagger_list {
tags := tagger(path, info, &ctx_ns)
if tags == nil {
continue
}
// Push new tags to the context
ctx_tags_if, ok := ctx_ns["tags"]
if !ok {
ctx_tags = make(tgrs.CtxTagset, len(taggers))
} else {
ctx_tags = ctx_tags_if.(tgrs.CtxTagset)
}
for _, tag := range tags {
_, ok = ctx_tags[tag]
if !ok {
ctx_tags[tag] = true
}
}
ctx_ns["tags"] = ctx_tags
}
}
// Attach tags only to files
if info.Mode() & os.ModeType != 0 {
return
}
file_tags := []string{}
for ns, ctx_ns := range ctx {
ctx_tags_if, ok := ctx_ns["tags"]
if !ok {
continue
}
ctx_tags = ctx_tags_if.(tgrs.CtxTagset)
for tag, _ := range ctx_tags {
file_tags = append(file_tags, ns + ":" + tag)
}
}
log.Tracef(" - file: %v, tags: %v", path, file_tags)
if !dry_run {
cmd := exec.Command("tmsu", "tag", path)
cmd.Args = append(cmd.Args, file_tags...)
cmd.Stdout, cmd.Stderr = tmsu_log_pipe, tmsu_log_pipe
err = cmd.Run()
if err != nil {
log.Fatalf("Failure running tmsu (file: %v, tags: %v): %v", path, file_tags, err)
}
tmsu_log_pipe.Flush()
}
return
}
path_ext, err := path_t(root).ExpandUser()
if err == nil {
root = string(path_ext)
}
err = filepath.Walk(root, walk_iter)
if err != nil {
log.Errorf("Failed to process path: %s", root)
}
}
log.Debug("Finished")
}