-
Notifications
You must be signed in to change notification settings - Fork 13
/
helpers.go
166 lines (134 loc) · 2.93 KB
/
helpers.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
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"time"
)
var (
homeDir string
execLookPath = exec.LookPath
execCommand = exec.Command
osExit = os.Exit
skipSpinner bool
)
func init() {
homeDir, _ = os.UserHomeDir()
}
func runCmd(cmd *exec.Cmd) (err error) {
var (
stderr io.ReadCloser
stdout io.ReadCloser
)
if stderr, err = cmd.StderrPipe(); err != nil {
return
}
defer func() {
_ = stderr.Close()
}()
go func() { _, _ = io.Copy(os.Stderr, stderr) }()
if stdout, err = cmd.StdoutPipe(); err != nil {
return
}
defer func() {
_ = stdout.Close()
}()
go func() { _, _ = io.Copy(os.Stdout, stdout) }()
if err = cmd.Run(); err != nil {
err = fmt.Errorf("failed to run %s", cmd.String())
}
return
}
// replaces matching file patterns in a path, including subdirectories
func replace(path, pattern, old, new string) error {
return filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
return replaceWalkFn(path, info, pattern, []byte(old), []byte(new))
})
}
func replaceWalkFn(path string, info os.FileInfo, pattern string, old, new []byte) (err error) {
var matched bool
if matched, err = filepath.Match(pattern, info.Name()); err != nil {
return
}
if matched {
var oldContent []byte
if oldContent, err = ioutil.ReadFile(filepath.Clean(path)); err != nil {
return
}
if err = ioutil.WriteFile(path, bytes.Replace(oldContent, old, new, -1), 0); err != nil {
return
}
}
return
}
func createFile(filePath, content string) (err error) {
var f *os.File
if f, err = os.Create(filePath); err != nil {
return
}
defer func() { _ = f.Close() }()
_, err = f.WriteString(content)
return
}
func formatLatency(d time.Duration) time.Duration {
switch {
case d > time.Second:
return d.Truncate(time.Second / 100)
case d > time.Millisecond:
return d.Truncate(time.Millisecond / 100)
case d > time.Microsecond:
return d.Truncate(time.Microsecond / 100)
default:
return d
}
}
func loadConfig() (err error) {
configFilePath := configFilePath()
if fileExist(configFilePath) {
if err = loadJson(configFilePath, &rc); err != nil {
return
}
}
return
}
func storeConfig() {
_ = storeJson(configFilePath(), rc)
}
func configFilePath() string {
if homeDir == "" {
return configName
}
return fmt.Sprintf("%s%c%s", homeDir, os.PathSeparator, configName)
}
var fileExist = func(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}
func storeJson(filename string, v interface{}) error {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(filename, b, 0600)
}
func loadJson(filename string, v interface{}) error {
b, err := ioutil.ReadFile(path.Clean(filename))
if err != nil {
return err
}
return json.Unmarshal(b, v)
}