-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
246 lines (224 loc) · 5.68 KB
/
utils.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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"reflect"
"strings"
"time"
"unicode"
"github.com/go-git/go-billy/v5/osfs"
gitignore "github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
)
func smartPrint(text string) {
text = strings.TrimRight(text, "\r\n")
fmt.Println(text)
}
func exitErr(err string) {
smartPrint(err)
os.Exit(1)
}
func checkErr(err error) {
if err != nil {
exitErr("Error: " + err.Error())
}
}
func jsonEncode(data interface{}) string {
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
err := encoder.Encode(data)
checkErr(err)
return string(buf.String())
}
func jsonDecodeBytes(data []byte) map[string]interface{} {
var result map[string]interface{}
err := json.Unmarshal(data, &result)
checkErr(err)
return result
}
func isBlank(str string) bool {
for _, r := range str {
if !unicode.IsSpace(r) {
return false
}
}
return true
}
func removeBlankLines(reader io.Reader, writer io.Writer) {
breader := bufio.NewReader(reader)
bwriter := bufio.NewWriter(writer)
for {
line, err := breader.ReadString('\n')
if !isBlank(line) {
_, err := bwriter.WriteString(line)
checkErr(err)
}
if err != nil {
break
}
}
bwriter.Flush()
}
func getCacheDir() string {
baseDir, err := os.UserCacheDir()
checkErr(err)
cacheDir := filepath.Join(baseDir, "bitcart-cli")
createIfNotExists(cacheDir, os.ModePerm)
return cacheDir
}
func parseVersionFromURL(url string) string {
parts := strings.Split(url, "/")
if len(parts) < 2 {
exitErr("Invalid version string provided. Only bitcart-hosted schema URLs are supported")
}
return parts[len(parts)-2]
}
func prepareSchema(url string) *jsonschema.Schema {
cacheDir := getCacheDir()
schemaPath := filepath.Join(cacheDir, "plugin.schema.json")
versionFile := filepath.Join(cacheDir, "schema.version")
schemaVersion := parseVersionFromURL(url)
version, versionErr := os.ReadFile(versionFile)
if statResult, err := os.Stat(schemaPath); os.IsNotExist(err) ||
time.Since(
statResult.ModTime().AddDate(0, 0, 7),
) > time.Since(
time.Now(),
) || (versionErr == nil && string(version) != schemaVersion) {
resp, err := http.Get(url)
checkErr(err)
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
checkErr(err)
checkErr(os.WriteFile(schemaPath, data, os.ModePerm))
checkErr(
os.WriteFile(
filepath.Join(cacheDir, "schema.version"),
[]byte(schemaVersion),
os.ModePerm,
),
)
}
sch, err := jsonschema.Compile(schemaPath)
checkErr(err)
return sch
}
func readManifest(path string) interface{} {
manifestPath := filepath.Join(path, "manifest.json")
data, err := os.ReadFile(manifestPath)
checkErr(err)
var manifest interface{}
checkErr(json.Unmarshal(data, &manifest))
return manifest
}
func getOutputDirectory(componentType string, author string, name string) string {
if componentType == "docker" {
return filepath.Join("compose/plugins/docker", author+"_"+name)
}
if componentType != "backend" {
author = "@" + author
}
return filepath.Join("modules", author, name)
}
type installationProcessor func(string, string, string)
func iterateInstallations(path string, manifest map[string]interface{}, fn installationProcessor) {
for _, installData := range manifest["installs"].([]interface{}) {
installData := installData.(map[string]interface{})
componentPath := filepath.Join(path, installData["path"].(string))
componentName := filepath.Base(componentPath)
installType := installData["type"].(string)
fn(componentPath, componentName, installType)
}
}
func setField(v interface{}, name string, value string) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Struct {
exitErr("v must be pointer to struct")
}
rv = rv.Elem()
fv := rv.FieldByName(name)
if !fv.IsValid() {
exitErr(fmt.Sprintf("not a field name: %s", name))
}
if !fv.CanSet() {
exitErr(fmt.Sprintf("cannot set field %s", name))
}
if fv.Kind() != reflect.String {
exitErr(fmt.Sprintf("%s is not a string field", name))
}
fv.SetString(value)
}
func getComponentConfigEntry(componentType string) *string {
switch componentType {
case "backend":
return &rootOptions.BitcartDirectory
case "admin":
return &rootOptions.BitcartAdminDirectory
case "store":
return &rootOptions.BitcartStoreDirectory
case "docker":
return &rootOptions.BitcartDockerDirectory
}
return nil
}
type RejectByNameFunc func(path string) bool
func rejectGitignored(targets []string) (RejectByNameFunc, error) {
var patterns []gitignore.Pattern
fs := osfs.New("/")
for _, target := range targets {
parts, err := pathToArray(target)
if err != nil {
return nil, err
}
patternsNow, err := gitignore.ReadPatterns(fs, parts)
if err != nil {
return nil, err
}
patterns = append(patterns, patternsNow...)
}
matcher := gitignore.NewMatcher(patterns)
return func(filename string) bool {
isDir := isDir(filename)
p, err := pathToArray(filename)
if err != nil {
return false
}
return matcher.Match(p, isDir)
}, nil
}
func isDir(filename string) bool {
file, err := os.Open(filename)
if err != nil {
return false
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return false
}
isDir := fileInfo.IsDir()
return isDir
}
func pathToArray(path string) ([]string, error) {
absolute, err := filepath.Abs(path)
if err != nil {
return nil, err
}
parts := strings.Split(absolute, string(filepath.Separator))
result := []string{}
for _, p := range parts {
if p != "" {
result = append(result, p)
}
}
return result, nil
}