-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.go
111 lines (92 loc) · 2.03 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/Sirupsen/logrus"
)
// fatal prints the error's details
// then exits the program with an exit status of 1.
func fatal(err error) {
// make sure the error is written to the logger
logrus.Error(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// DeDuplicate make a slice with no duplicated elements.
func DeDuplicate(input []string) []string {
if input == nil {
return nil
}
result := []string{}
internal := map[string]struct{}{}
for _, value := range input {
value = strings.TrimSpace(value)
if value == "" {
continue
}
if _, exist := internal[value]; !exist {
internal[value] = struct{}{}
result = append(result, value)
}
}
return result
}
func isJSON(s string) bool {
var js map[string]interface{}
return json.Unmarshal([]byte(s), &js) == nil
}
func jsonPrettyPrint(in string) string {
var out bytes.Buffer
// make indent four space
err := json.Indent(&out, []byte(in), "", " ")
if err != nil {
return in
}
return out.String()
}
func checkURLScheme(addr, scheme string) string {
var address string
if strings.HasPrefix(addr, scheme) {
address = addr
} else {
address = fmt.Sprintf("%s://%s", scheme, addr)
}
return address
}
func checkIPAddr(iparray []string) error {
for _, ip := range iparray {
ip = strings.TrimSpace(ip)
if ip == "" {
continue
}
i := net.ParseIP(ip)
if i == nil {
return fmt.Errorf("%s is invalid ip addr", ip)
}
}
return nil
}
// GetCurrPath get current path string
func GetCurrPath() string {
file, _ := exec.LookPath(os.Args[0])
path, _ := filepath.Abs(file)
index := strings.LastIndex(path, string(os.PathSeparator))
ret := path[:index]
return ret
}
// YesOrDie get yes answer
func YesOrDie(msg string) {
fmt.Printf("%s\n(type 'yes' to accept): ", msg)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if !strings.EqualFold(strings.TrimSpace(text), "yes") {
logrus.Fatalf("*** Aborting...")
}
}