forked from xm1k3/cent
/
jobs.go
256 lines (222 loc) · 5.36 KB
/
jobs.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
package jobs
import (
"crypto/sha256"
"fmt"
"io"
"io/fs"
"log"
"os"
filepath "path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/fatih/color"
"github.com/go-git/go-git/v5"
"github.com/spf13/viper"
"github.com/adampielak/cent/internal/utils"
)
func cloneRepo(gitPath string, console bool, index string, timestamp string) error {
cloneOptions := git.CloneOptions{
URL: gitPath,
}
if console {
cloneOptions.Progress = os.Stdout
}
_, err := git.PlainClone("/tmp/cent"+timestamp+"/repo"+index, false, &cloneOptions)
if err != nil {
return err
}
fmt.Println(color.GreenString("[CLONED] " + gitPath))
return nil
}
func worker(work chan [2]string, wg *sync.WaitGroup, console bool, timestamp string, defaultTimeout int) {
defer wg.Done()
for repo := range work {
err := cloneRepo(repo[1], console, repo[0], timestamp)
if err != nil {
fmt.Println(color.RedString("[ERR] clone: " + repo[1] + " - " + err.Error()))
}
}
}
func Start(_path string, keepfolders bool, console bool, threads int, defaultTimeout int) {
timestamp := strconv.Itoa(int(time.Now().Unix()))
if _, err := os.Stat(filepath.Join(_path)); os.IsNotExist(err) {
os.Mkdir(filepath.Join(_path), 0700)
}
work := make(chan [2]string)
go func() {
for index, gitPath := range viper.GetStringSlice("community-templates") {
work <- [2]string{strconv.Itoa(index), gitPath}
}
close(work)
}()
wg := &sync.WaitGroup{}
for i := 0; i < threads; i++ {
wg.Add(1)
go worker(work, wg, console, timestamp, defaultTimeout)
}
wg.Wait()
dirname := "/tmp/cent" + timestamp + "/"
filepath.Walk(dirname,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
directory := getDirPath(strings.TrimPrefix(path, dirname))
if info.IsDir() {
if keepfolders {
if _, err := os.Stat(filepath.Join(_path, directory)); os.IsNotExist(err) {
// fmt.Println(_path, name, directory)
os.Mkdir(filepath.Join(_path, directory), 0700)
}
}
} else {
basename := info.Name()
if filepath.Ext(basename) == ".yaml" {
if !keepfolders {
directory = ""
}
utils.RunCommand("rsync -avzh --ignore-errors "+path+" "+filepath.Join(_path, directory), console, defaultTimeout)
}
}
return nil
})
DeleteFromTmp(dirname)
}
func UpdateRepo(path string, remDirs bool, remFiles bool, printOut bool) {
filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if remDirs {
for _, exDirs := range viper.GetStringSlice("exclude-dirs") {
if strings.Contains(path, exDirs) {
err := os.RemoveAll(path)
if err != nil {
log.Fatal(err)
}
if printOut {
fmt.Println(color.RedString("[D][-] Dir removed\t" + path))
}
return filepath.SkipDir
}
}
}
} else {
if remFiles {
for _, exFiles := range viper.GetStringSlice("exclude-files") {
// fmt.Println("Path: ", path, exFiles)
if strings.Contains(path, exFiles) {
e := os.Remove(path)
if e != nil {
log.Fatal(e)
}
if printOut {
fmt.Println(color.RedString("[F][-] File removed\t" + path))
}
}
// break
}
}
}
return nil
})
}
func RemoveDuplicates(path string, console bool) {
fmt.Println("Removing duplicate templates...")
files := getFilePaths(path)
hashes := make(map[string]string)
// get file hashes
for _, file := range files {
hashes[file] = getFileHash(file)
}
// get hash files
hashfiles := make(map[string][]string)
for file, hash := range hashes {
hashfiles[hash] = append(hashfiles[hash], file)
}
// for each hash, remove all the files except the first one
for _, files := range hashfiles {
sort.Strings(files)
for _, fileToRemove := range files[1:] {
if console {
fmt.Printf("Removing duplicate file: %s\n", fileToRemove)
}
os.Remove(fileToRemove)
}
}
}
func getFilePaths(path string) []string {
var files []string
// go through each file
err := filepath.WalkDir(path, func(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if !d.IsDir() {
files = append(files, s)
}
return nil
})
if err != nil {
log.Fatal(err)
}
return files
}
func getFileHash(path string) string {
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func getDirPath(path string) string {
reponame := strings.Split(path, "/")[0]
endpoint := strings.TrimPrefix(path, reponame)
return endpoint
}
func RemoveEmptyFolders(dirname string) {
f, err := os.Open(dirname)
if err != nil {
log.Fatal(err)
}
files, err := f.Readdir(-1)
f.Close()
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.IsDir() {
if IsEmpty(filepath.Join(dirname, file.Name())) {
err := os.RemoveAll(filepath.Join(dirname, file.Name()))
if err != nil {
log.Fatal(err)
}
}
}
}
}
func IsEmpty(name string) bool {
f, err := os.Open(name)
if err != nil {
return false
}
defer f.Close()
_, err = f.Readdirnames(1) // Or f.Readdir(1)
return err == io.EOF // Either not empty or error, suits both cases
}
func DeleteFromTmp(dirname string) {
err := os.RemoveAll(dirname)
if err != nil {
log.Fatal(err)
}
}