-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
286 lines (239 loc) · 6.81 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
// ____ ____ _ _ _
// | _ \ __ _ ___| _ \(_) | ___ | |_
// | |_) / _` |/ __| |_) | | |/ _ \| __|
// | __/ (_| | (__| __/| | | (_) | |_
// |_| \__,_|\___|_| |_|_|\___/ \__|
//
//
// This code implements a versatile set of utilities meticulously designed to simplify
// tasks during hook execution with PacPilot. These purpose-built utilities enhance
// PacPilot's functionality by providing convenient methods for common operations,
// making hook execution more seamless and efficient.
//
//// IMPORTS
//
import (
// Modules in GOROOT
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
// External modules
ptywrapper "github.com/fearlessdots/ptywrapper"
color "github.com/gookit/color"
)
//
//// DISPLAY
//
func utilsMsg(colorHEX string, msg string, program Program) {
// Create the color object
color := color.HEX(colorHEX)
// Unquote the message (to render new lines)
msg, _ = strconv.Unquote(`"` + msg + `"`)
// Print the message using the specified color
printColoredMessage(color, msg, program.indentLevel)
}
func utilsShowAttention(msg string, program Program) {
showAttention(msg, program.indentLevel)
}
func utilsShowError(msg string, program Program) {
showError(msg, program.indentLevel)
}
func utilsShowSuccess(msg string, program Program) {
showSuccess(msg, program.indentLevel)
}
func utilsShowSection(msg string, program Program) {
showInfoSectionTitle(msg, program.indentLevel)
}
func utilsHr(char string, factor float64, program Program) {
hr(char, factor, program)
}
//
//// USER INPUT
//
func utilsAskConfirmation(msg string, program Program) {
var confirm bool
confirm = askConfirmation(msg, program)
if confirm {
finishProgram(0)
} else {
finishProgram(1)
}
}
//
//// SSH
//
func utilsSSHAgentStart(privateKeyPath string, tempDir string, program Program) functionResponse {
pidFile := filepath.Join(tempDir, "sshagent.pid")
sockFile := filepath.Join(tempDir, "sshauth.sock")
if _, err := os.Stat(pidFile); err == nil {
return functionResponse{
exitCode: 1,
logLevel: "error",
message: fmt.Sprintf("File 'sshagent.pid' already found at %v. Please, remove it manually.", pidFile),
indentLevel: program.indentLevel,
}
}
if _, err := os.Stat(sockFile); err == nil {
return functionResponse{
exitCode: 1,
logLevel: "error",
message: fmt.Sprintf("File 'sshauth.sock' already found at %v. Please, remove it manually.", pidFile),
indentLevel: program.indentLevel,
}
}
//
////
//
showInfo("Starting ssh-agent process", program.indentLevel)
cmd := &ptywrapper.Command{
Entry: "ssh-agent",
Args: []string{"-s"},
Discard: true,
}
completedCmd, err := cmd.RunInPTY()
if err != nil {
return functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to execute command -> " + err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
}
if completedCmd.ExitCode != 0 {
return functionResponse{
exitCode: completedCmd.ExitCode,
message: fmt.Sprintf("Failed to execute command: exit code %v", completedCmd.ExitCode),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
} else {
response := functionResponse{
exitCode: 0,
message: "Finished",
logLevel: "success",
indentLevel: program.indentLevel + 1,
}
handleFunctionResponse(response, true)
}
outputLines := strings.Split(string(completedCmd.Output), "\n")
sshAuthSock := strings.Split(strings.Split(outputLines[0], "=")[1], ";")[0]
sshAgentPid := strings.Split(strings.Split(outputLines[1], "=")[1], ";")[0]
sshAgentEnvVars := make(map[string]string)
sshAgentEnvVars[pidFile] = sshAgentPid
sshAgentEnvVars[sockFile] = sshAuthSock
for key, value := range sshAgentEnvVars {
err := ioutil.WriteFile(key, []byte(value), 0644)
if err != nil {
return functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to write to file %v -> %v", key, err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
}
}
os.Setenv("SSH_AGENT_PID", sshAgentPid)
os.Setenv("SSH_AUTH_SOCK", sshAuthSock)
//
////
//
space()
showInfo("Adding private key (a passphrase may be needed)", program.indentLevel)
cmd = &ptywrapper.Command{
Entry: "ssh-add",
Args: []string{privateKeyPath},
Discard: false,
}
completedCmd, err = cmd.RunInPTY()
if err != nil {
return functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to execute command -> " + err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
}
if completedCmd.ExitCode != 0 {
return functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to execute command: exit code %v", completedCmd.ExitCode),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
} else {
return functionResponse{
exitCode: 0,
message: "Finished",
logLevel: "success",
indentLevel: program.indentLevel + 1,
}
}
}
func utilsSSHAgentStop(tempDir string, program Program) functionResponse {
pidFile := filepath.Join(tempDir, "sshagent.pid")
sockFile := filepath.Join(tempDir, "sshauth.sock")
content, err := ioutil.ReadFile(pidFile)
if err != nil {
return functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to read PID file -> " + err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
}
sshAgentPid := string(content)
showInfo("Killing ssh-agent process", program.indentLevel)
err = exec.Command("kill", sshAgentPid).Run()
if err != nil {
return functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to kill process -> " + err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
} else {
response := functionResponse{
exitCode: 0,
message: "Finished",
logLevel: "success",
indentLevel: program.indentLevel + 1,
}
handleFunctionResponse(response, true)
}
space()
showInfo("Removing temporary files", program.indentLevel)
for _, file := range []string{pidFile, sockFile} {
err = os.Remove(file)
if err != nil {
return functionResponse{
exitCode: 1,
message: fmt.Sprintf("Failed to remove file %v -> %v", file, err.Error()),
logLevel: "error",
indentLevel: program.indentLevel + 1,
}
}
}
return functionResponse{
exitCode: 0,
message: "Finished",
logLevel: "success",
indentLevel: program.indentLevel + 1,
}
}
func utilsSSHAgentGetPID(tempDir string) {
pidFile := filepath.Join(tempDir, "sshagent.pid")
content, _ := ioutil.ReadFile(pidFile)
sshAgentPid := string(content)
fmt.Println(sshAgentPid)
}
func utilsSSHAgentGetSock(tempDir string) {
sockFile := filepath.Join(tempDir, "sshauth.sock")
content, _ := ioutil.ReadFile(sockFile)
sshAgentSock := string(content)
fmt.Println(sshAgentSock)
}