forked from donny-dont/drone-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
59 lines (53 loc) · 1.3 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
package script
import (
"bytes"
"encoding/base64"
"fmt"
)
// Writes the netrc file.
func writeNetrc(machine, login, password string) string {
var buf bytes.Buffer
if len(machine) == 0 {
return buf.String()
}
out := fmt.Sprintf(
netrcScript,
machine,
login,
password,
)
buf.WriteString(out)
return buf.String()
}
// Writes the RSA private key
func writeKey(key string) string {
var buf bytes.Buffer
if len(key) == 0 {
return buf.String()
}
buf.WriteString(fmt.Sprintf(keyScript, key))
buf.WriteString(keyConfScript)
return buf.String()
}
// writeCmds is a helper fuction that writes a slice
// of bash commands as a single script.
func writeCmds(cmds []string) string {
var buf bytes.Buffer
for _, cmd := range cmds {
buf.WriteString(trace(cmd))
}
return buf.String()
}
// trace is a helper function that allows us to echo
// commands back to the console for debugging purposes.
func trace(cmd string) string {
echo := fmt.Sprintf("$ %s\n", cmd)
base := base64.StdEncoding.EncodeToString([]byte(echo))
return fmt.Sprintf(traceScript, base, cmd)
}
// encode is a helper function that base64 encodes
// a shell command (or entire script)
func encode(script []byte) string {
encoded := base64.StdEncoding.EncodeToString(script)
return fmt.Sprintf("echo %s | base64 -d | /bin/sh", encoded)
}