-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
183 lines (145 loc) · 2.75 KB
/
common.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
package dbot
import (
"os"
"strings"
"sync"
"github.com/fatih/color"
)
var gLogLock sync.Mutex
func log(a ...interface{}) {
gLogLock.Lock()
defer gLogLock.Unlock()
for i := 0; i < len(a); i += 2 {
if s := a[i].(string); s != "" {
_, _ = color.New(a[i+1].(color.Attribute), color.Bold).Print(s)
}
}
}
type Env map[string]string
func (p Env) ParseString(v string, defaultStr string, trimSpace bool) string {
replaceArray := make([]string, 0)
for key, value := range p {
replaceArray = append(replaceArray, "${"+key+"}", value)
}
ret := strings.NewReplacer(replaceArray...).Replace(v)
if trimSpace {
ret = strings.TrimSpace(ret)
}
if ret == "" {
ret = defaultStr
}
return ret
}
func (p Env) ParseStringArray(arr []string) []string {
ret := make([]string, len(arr))
for i := 0; i < len(arr); i++ {
ret[i] = p.ParseString(arr[i], "", false)
}
return ret
}
func (p Env) ParseEnv(env Env) Env {
ret := make(Env)
for key, value := range env {
ret[key] = p.ParseString(value, "", false)
}
return ret
}
func (p Env) Merge(env Env) Env {
ret := make(Env)
for key, value := range p {
ret[key] = value
}
for key, value := range env {
ret[key] = value
}
return ret
}
type Import struct {
Name string
File string
Env Env
}
type Input struct {
Type string
Desc string
}
type Remote struct {
Port string
User string
Host string
}
type Job struct {
Async bool
Imports map[string]*Import
Remotes map[string][]*Remote
Inputs map[string]*Input
Env Env
Commands []*Command
}
type Command struct {
Tag string
Exec string
On string
Stdin []string
Env Env
Args Env
File string
}
func GetStandradOut(s string) string {
if s != "" && s[len(s)-1] != '\n' {
return s + "\n"
} else {
return s
}
}
func IsDir(path string) bool {
f, e := os.Stat(path)
return e == nil && f.Mode().IsDir()
}
func IsFile(path string) bool {
f, e := os.Stat(path)
return e == nil && f.Mode().IsRegular()
}
func SplitCommand(str string) []string {
command := " " + str + " "
ret := make([]string, 0)
isSingleQuote := false
isDoubleQuotes := false
preChar := uint8(0)
cmdStart := -1
for i := 0; i < len(command); i++ {
if isSingleQuote {
if command[i] == 0x27 {
isSingleQuote = false
}
preChar = command[i]
continue
}
if isDoubleQuotes {
if command[i] == 0x22 && preChar != 0x5C {
isDoubleQuotes = false
}
preChar = command[i]
continue
}
if command[i] == ' ' {
if cmdStart >= 0 {
ret = append(ret, command[cmdStart:i])
cmdStart = -1
}
preChar = command[i]
continue
}
if cmdStart < 0 {
cmdStart = i
}
if command[i] == 0x27 {
isSingleQuote = true
}
if command[i] == 0x22 {
isDoubleQuotes = true
}
preChar = command[i]
}
return ret
}