-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
176 lines (162 loc) · 4.24 KB
/
path.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
package functions
import (
"fmt"
"github.com/echocat/kubor/template"
"os"
"path"
"path/filepath"
)
var FuncPathExists = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "Is <true> if provided <path> does exist.",
},
}.MustWithFunc(func(context template.ExecutionContext, path string) (bool, error) {
return statCheckOfContext(context, path, nil)
})
var FuncIsFile = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "<true> if provided <path> does exist and is a file.",
},
}.MustWithFunc(func(context template.ExecutionContext, path string) (bool, error) {
return statCheckOfContext(context, path, func(fi os.FileInfo) bool {
return !fi.IsDir()
})
})
var FuncIsDir = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "<true> if provided <path> does exist and is a directory.",
},
}.MustWithFunc(func(context template.ExecutionContext, path string) (bool, error) {
return statCheckOfContext(context, path, func(fi os.FileInfo) bool {
return fi.IsDir()
})
})
var FuncFileSize = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "The size of the provided <path>. It will fail with an error if the specified file does not exist or is not a file.",
},
}.MustWithFunc(func(context template.ExecutionContext, path string) (int64, error) {
if fi, err := statOfContext(context, path); err != nil {
return 0, err
} else {
return fi.Size(), nil
}
})
var FuncPathExt = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "The file extension.",
},
}.MustWithFunc(func(p string) string {
return path.Ext(p)
})
var FuncPathBase = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "The last element of path.",
},
}.MustWithFunc(func(p string) string {
return path.Base(p)
})
var FuncPathDir = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "All but the last element of path, typically the path's directory.",
},
}.MustWithFunc(func(p string) string {
return path.Dir(p)
})
var FuncPathClean = Function{
Parameters: Parameters{{
Name: "path",
}},
Returns: Return{
Description: "Shortest path name equivalent to path by purely lexical processing.",
},
}.MustWithFunc(func(p string) string {
return path.Clean(p)
})
var FuncReadFile = Function{
Parameters: Parameters{{
Name: "file",
}},
Returns: Return{
Description: "The content of the provided <file>.",
},
}.MustWithFunc(func(context template.ExecutionContext, file string) (string, error) {
if resolved, err := resolvePathOfContext(context, file); err != nil {
return "", fmt.Errorf("cannot resolve path of '%s': %w", file, err)
} else if b, err := os.ReadFile(resolved); err != nil {
return "", fmt.Errorf("cannot read path '%s' (source:%s ): %w", resolved, file, err)
} else {
return string(b), nil
}
})
var FuncsPath = Functions{
"readFile": FuncReadFile,
"fileSize": FuncFileSize,
"pathExists": FuncPathExists,
"isFile": FuncIsFile,
"isDir": FuncIsDir,
"pathExt": FuncPathExt,
"pathBase": FuncPathBase,
"pathDir": FuncPathDir,
"pathClean": FuncPathClean,
}
var CategoryPath = Category{
Functions: FuncsPath,
}
func resolvePathOfContext(context template.ExecutionContext, path string) (string, error) {
if filepath.IsAbs(path) {
return path, nil
}
var dir string
sourceFile := context.GetTemplate().GetSourceFile()
if sourceFile != nil {
dir = filepath.Dir(*sourceFile)
} else {
if cwd, err := os.Getwd(); err != nil {
return "", err
} else {
dir = cwd
}
}
cleaned := filepath.Clean(dir + path)
return filepath.Abs(cleaned)
}
func statOfContext(context template.ExecutionContext, path string) (os.FileInfo, error) {
if resolved, err := resolvePathOfContext(context, path); err != nil {
return nil, err
} else {
return os.Stat(resolved)
}
}
func statCheckOfContext(context template.ExecutionContext, path string, predicate func(fi os.FileInfo) bool) (bool, error) {
if fi, err := statOfContext(context, path); os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
} else if predicate != nil {
return predicate(fi), nil
} else {
return true, nil
}
}