-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
132 lines (116 loc) · 3.13 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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// Expand finds matches for the provided Globs.
func handleGlobs(globs []string, ignored []string) ([]string, error) {
var matches = []string{""} // accumulate here
for _, glob := range globs {
if glob != "" && string(glob[0]) == "." {
// Check if first character is "."
// This eliminates cases where you have .ts instead of *.ts
glob = "*" + glob
}
if glob == "" {
glob = "."
}
// fmt.Println("Glob iteration", glob)
var hits []string
var hitMap = map[string]bool{}
for _, match := range matches {
// fmt.Println("Match iteration", match)
paths, err := filepath.Glob(match + glob)
// fmt.Println("Paths", paths)
if err != nil {
return nil, err
}
for _, path := range paths {
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// save de duped match from current iteration
if _, ok := hitMap[path]; !ok {
for _, ignorePattern := range ignored {
ok, err := filepath.Match(ignorePattern, path)
if ok || err != nil || strings.Contains(path, ignorePattern) {
// is an ignored file
return nil
}
}
if strings.Contains(path, "node_modules") {
return nil
}
hits = append(hits, path)
hitMap[path] = true
}
return nil
})
if err != nil {
return nil, err
}
}
}
matches = hits
}
// fix up return value for nil input
if globs == nil && len(matches) > 0 && matches[0] == "" {
matches = matches[1:]
}
return matches, nil
}
// Globs represents one filepath glob, with its elements joined by "**".
// Glob adds double-star support to the core path/filepath Glob function.
// It's useful when your globs might have double-stars, but you're not sure.
func Glob(pattern string) ([]string, error) {
if !strings.Contains(pattern, "**") {
// passthru to core package if no double-star
return filepath.Glob(pattern)
}
ingored := []string{"node_modules", "dist", ".next"}
ingored = append(ingored, globalConfig.IgnoredPaths...)
return handleGlobs(strings.Split(pattern, "**"), ingored)
}
func uniqueStringSlice(intSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range intSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func check(err error) {
if err != nil {
panic(err)
}
}
func MoveFile(sourcePath, destPath string) error {
inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("couldn't open source file: %s", err)
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return fmt.Errorf("couldn't open dest file: %s", err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return fmt.Errorf("writing to output file failed: %s", err)
}
// // The copy was successful, so now delete the original file
// err = os.Remove(sourcePath)
// if err != nil {
// return fmt.Errorf("Failed removing original file: %s", err)
// }
return nil
}