-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
234 lines (216 loc) · 5.67 KB
/
main.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
package main
import (
"fmt"
"os"
"os/exec"
"io/ioutil"
"bytes"
"syscall"
"strings"
"encoding/json"
"github.com/boltdb/bolt"
"encoding/base64"
)
type Process struct {
Command []string
Stdout []byte
Stderr []byte
ReturnCode int
}
type ProcessJson struct {
Command []string
Stdout string
Stderr string
ReturnCode int
}
func encodeBase64(str []byte) string {
return base64.StdEncoding.EncodeToString(str)
}
func decodeBase64(str string) []byte {
data, _ := base64.StdEncoding.DecodeString(str)
return data
}
func openBoltDb() (*bolt.DB, error) {
location := os.Getenv("DATABASE_LOCATION")
if location == "" {
location = "processes.db"
}
return bolt.Open(location, 0600, nil)
}
func (p *Process) Json() []byte {
payload := ProcessJson{}
payload.Command = p.Command
payload.ReturnCode = p.ReturnCode
payload.Stdout = encodeBase64(p.Stdout)
payload.Stderr = encodeBase64(p.Stderr)
result, _ := json.Marshal(payload)
return result
}
func (p *Process) FromProcessJson(proc *ProcessJson) {
p.Command = proc.Command
p.Stdout = decodeBase64(proc.Stdout)
p.Stderr = decodeBase64(proc.Stderr)
p.ReturnCode = proc.ReturnCode
}
func (p *Process) FromJson(payload []byte) {
result := ProcessJson{}
json.Unmarshal(payload, &result)
p.FromProcessJson(&result)
}
func (p *Process) Key() []byte {
return ([]byte)(strings.Join(p.Command, " "))
}
func (p *Process) Print() {
fmt.Println("Command: " + strings.Join(p.Command, " "))
fmt.Println("Stdout: " + string(p.Stdout))
fmt.Println("Stderr: " + string(p.Stderr))
fmt.Printf("Return code: %d\n", p.ReturnCode)
}
func (p *Process) Save() error {
db, err := openBoltDb();
if err != nil {
return err
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("processes"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return b.Put(p.Key(), p.Json())
})
return err
}
func (p *Process) Capture() {
cmd := exec.Command(p.Command[0], p.Command[1:]...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run() ; err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
p.ReturnCode = waitStatus.ExitStatus()
} else {
fmt.Println(err.Error())
return
}
}
p.Stdout = stdout.Bytes()
p.Stderr = stderr.Bytes()
err := p.Save()
if err != nil {
fmt.Println(err.Error())
}
p.Playback()
}
func (p *Process) Lookup() error {
db, err := openBoltDb();
if err != nil {
return err
}
defer db.Close()
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("processes"))
if b == nil {
return fmt.Errorf("Nothing has been recorded yet. Try to capture some commands first")
}
v := b.Get(p.Key())
if v == nil {
return fmt.Errorf("%s does not know about this process. Run the command in capture mode first.", os.Args[0])
}
p.FromJson(v)
return nil
})
return err
}
func (p *Process) Playback() error {
os.Stdout.Write(p.Stdout)
os.Stderr.Write(p.Stderr)
os.Exit(p.ReturnCode)
return nil
}
func Export() error {
db, err := openBoltDb();
if err != nil {
return err
}
defer db.Close()
payload := []ProcessJson{}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("processes"))
if b == nil {
return nil
}
b.ForEach(func(k, v []byte) error {
p := ProcessJson{}
json.Unmarshal(v, &p)
payload = append(payload, p)
return nil
})
return nil
})
data, _ := json.Marshal(payload)
os.Stdout.Write(data)
return nil
}
func Import(file string) {
payload, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err.Error())
return
}
parsed := []ProcessJson{}
err = json.Unmarshal(payload, &parsed)
if err != nil {
fmt.Println(err.Error())
return
}
for _, j := range parsed {
p := Process{}
p.FromProcessJson(&j)
p.Save()
}
}
func Usage() {
fmt.Println(os.Args[0] + " [OPTS] [COMMAND] [[ARGS]]")
fmt.Println("")
fmt.Println(os.Args[0] + " can either capture or playback commands:")
fmt.Println("")
fmt.Println("Example:")
fmt.Println("")
fmt.Println(" $ " + os.Args[0] + " --capture ls -al /")
fmt.Println(" $ " + os.Args[0] + " ls -al /")
fmt.Println("")
fmt.Println("Options:")
fmt.Println("")
fmt.Println(" --export Export database to json")
fmt.Println(" --import [PATH] Import json to database")
fmt.Println(" --capture [COMMAND] [[ARGS]] Capture the output of running COMMAND [ARGS]")
}
func InCaptureMode() bool {
return os.Getenv("CAPTURE") == "1"
}
func main() {
if len(os.Args) == 1 {
Usage()
} else if os.Args[1] == "--export" {
Export()
} else if os.Args[1] == "--import" {
Import(os.Args[2])
} else if os.Args[1] == "--capture" {
p := Process{}
p.Command = os.Args[2:]
p.Capture()
} else {
p := Process{}
p.Command = os.Args[1:]
if InCaptureMode() {
p.Capture()
} else if err := p.Lookup() ; err == nil {
p.Playback()
} else {
fmt.Println(err.Error())
}
}
}