-
Notifications
You must be signed in to change notification settings - Fork 0
/
kape.go
107 lines (91 loc) · 2.8 KB
/
kape.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
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
type TKape struct {
ID string `yaml:"Id"`
Description string `yaml:"Description"`
Author string `yaml:"Author"`
Version string `yaml:"Version"`
RecreateDirectories bool `yaml:"RecreateDirectories"`
Targets []struct {
Name string `yaml:"Name"`
Category string `yaml:"Category"`
Path string `yaml:"Path"`
Recursive bool `yaml:"Recursive"`
FileMask string `yaml:"FileMask"`
Comment string `yaml:"Comment"`
} `yaml:"Targets"`
}
type MKape struct {
ID string `yaml:"Id"`
Description string `yaml:"Description"`
Category string `yaml:"Category"`
Author string `yaml:"Author"`
Version string `yaml:"Version"`
BinaryUrl string `yaml:"BinaryUrl"`
ExportFormat string `yaml:"ExportFormat"`
WaitTimeout int `yaml:"WaitTimeout"`
FileMask string `yaml:"FileMask"`
Processors []struct {
Executable string `yaml:"Executable"`
CommandLine string `yaml:"CommandLine"`
ExportFormat string `yaml:"ExportFormat"`
} `yaml:"Processors"`
}
func ParseKapeTargets(target string, dir string) ([]Matcher, []string, error) {
targets := map[string]string{}
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
targets[strings.ToLower(filepath.Base(path))] = path
return nil
})
return convert(targets, target)
}
func convert(targets map[string]string, name string) ([]Matcher, []string, error) {
fh, err := os.Open(targets[strings.ToLower(name)])
if err != nil {
return nil, nil, err
}
tkape := TKape{}
d := yaml.NewDecoder(fh)
err = d.Decode(&tkape)
if err != nil {
return nil, nil, err
}
var matchers []Matcher
var paths []string
for _, t := range tkape.Targets {
switch {
case strings.HasSuffix(t.Path, ".tkape"):
m, p, err := convert(targets, t.Path)
if err != nil {
return nil, nil, err
}
matchers = append(matchers, m...)
paths = append(paths, p...)
case t.FileMask == "" && t.Recursive:
pattern := fmt.Sprintf("%s\\**", strings.TrimSuffix(t.Path, "\\"))
pattern = strings.ReplaceAll(pattern, "%user%", "*")
matchers = append(matchers, NewGlobMatcher(pattern))
case t.FileMask == "" && !t.Recursive:
pattern := fmt.Sprintf("%s\\*", strings.TrimSuffix(t.Path, "\\"))
pattern = strings.ReplaceAll(pattern, "%user%", "*")
matchers = append(matchers, NewGlobMatcher(pattern))
case t.FileMask != "":
pattern := fmt.Sprintf("%s\\%s", strings.TrimSuffix(t.Path, "\\"), t.FileMask)
pattern = strings.ReplaceAll(pattern, "%user%", "*")
matchers = append(matchers, NewGlobMatcher(pattern))
default:
return nil, nil, fmt.Errorf("unsupported kape target: %+v", t)
}
}
return matchers, paths, nil
}