-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
120 lines (95 loc) · 1.97 KB
/
helper.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strings"
)
func contains(slice []string, searchString string) bool {
for _, value := range slice {
if value == searchString {
return true
}
}
return false
}
func checkErrAndExit(err error) {
if err != nil {
log.Fatal("ERROR: ", err)
}
}
func checkErrAndContinue(err error) {
if err != nil {
log.Print("WARNING: ", err)
}
}
func copyFile(sourceFile, destinationFile string) error {
input, err := ioutil.ReadFile(filepath.FromSlash(sourceFile))
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.FromSlash(destinationFile), input, os.ModePerm)
if err != nil {
return err
}
return nil
}
func getUsername() (string, error) {
if runtime.GOOS == "windows" {
return os.Getenv("username"), nil
}
user, err := user.Current()
if err != nil {
return "", err
}
return user.Name, nil
}
func getDestFolder() string {
destFolder := ""
if runtime.GOOS == "windows" {
destFolder = filepath.Join(os.Getenv("appdata"), "Microsoft", "Signatures")
} else {
destFolder, _ = os.Getwd()
}
return destFolder
}
func removeContents(dir string) error {
items, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return err
}
for _, item := range items {
err = os.RemoveAll(item)
if err != nil {
return err
}
}
return nil
}
func winExpandEnv(path string) string {
re := regexp.MustCompile(`%[^\%]+%`)
compatPath := re.ReplaceAllStringFunc(path, func(match string) string {
match = strings.Replace(match, "%", "", -1)
match = "${" + match + "}"
return match
})
return os.ExpandEnv(compatPath)
}
func askForConfirmation(question string) bool {
response := ""
for {
fmt.Printf("%s [y/n]: ", question)
fmt.Scanf("%s\n", &response)
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
}
}