-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
98 lines (85 loc) · 1.85 KB
/
util.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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
func fmtCyan(a ...interface{}) string {
if runtime.GOOS == "windows" {
return fmt.Sprint(a...)
}
return fmt.Sprint("\x1b[36m", fmt.Sprint(a...), "\x1b[39m")
}
func fmtGreen(a ...interface{}) string {
if runtime.GOOS == "windows" {
return fmt.Sprint(a...)
}
return fmt.Sprint("\x1b[32m", fmt.Sprint(a...), "\x1b[39m")
}
func debug(val interface{}) {
fmt.Printf("%+v\n", val)
}
func channelWait(c chan bool, num int) {
for i := 0; i < num; i++ {
<-c
}
close(c)
}
func timestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func commonPath(paths []string, index int) string {
if len(paths) < 2 {
return ""
} else if index >= len(paths[0]) {
return paths[0][:index-1]
}
value := paths[0][index]
for i, innerPath := range paths {
if index >= len(paths[0]) {
return paths[0][0 : index-1]
} else if innerPath[index] != value {
return paths[0][0 : index-1]
} else if innerPath[index] == value && i == len(paths)-1 {
return commonPath(paths, index+1)
}
}
return ""
}
func filesInPath(path string) (files, dirs []string, err error) {
filepath.Walk(path,
func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasPrefix(f.Name(), ".") {
if f.IsDir() {
return filepath.SkipDir
}
} else if f.IsDir() {
dirs = append(dirs, filepath.ToSlash(path))
} else {
files = append(files, filepath.ToSlash(path))
}
return nil
})
return files, dirs, err
}
func errorMsg(message string, err error) {
if err != nil {
fmt.Printf("\nError: %s\n %s\n\n", message, err)
} else {
fmt.Printf("\nError: %s\n\n", message)
}
}
func stringInSlice(value string, slice []string) bool {
for _, item := range slice {
if value == item {
return true
}
}
return false
}