-
Notifications
You must be signed in to change notification settings - Fork 1
/
gowild.go
191 lines (165 loc) · 4.15 KB
/
gowild.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const gowildVersion = "0.0.1"
func check(e error) {
if e != nil {
panic(e)
}
}
// PrintVersion prints CLI Information
func PrintVersion() {
fmt.Printf("gowild - https://github.com/havenbarnes/gowild\nv%v\n", gowildVersion)
}
// PrintHelp prints an optional error message and usage information
func PrintHelp(cmd string, invalidCommand bool) {
fmt.Print("gowild - https://github.com/havenbarnes/gowild\n\n")
if invalidCommand {
fmt.Printf("Invalid command: %s\n\n", cmd)
}
fmt.Print(`Usage:
gowild record begin recording bash commands
gowild stop end recording session and create shell script
gowild help Print Help (this message) and exit
gowild version Print version information and exit`)
}
var cwd string
func getCwd() string {
if cwd == "" {
path, err := os.Getwd()
check(err)
cwd = path
}
return cwd
}
func getConfigPath() string {
return "/usr/local/bin/.gowild"
}
func deleteConfig() {
os.Remove(getConfigPath())
}
func getHistoryPath() string {
home := os.Getenv("HOME")
if _, err := os.Stat(filepath.Join(home, ".zsh_history")); os.IsNotExist(err) {
return filepath.Join(home, ".bash_history")
}
return filepath.Join(home, ".zsh_history")
}
func getBashHistory() string {
dat, err := ioutil.ReadFile(getHistoryPath())
check(err)
fullHistory := strings.Split(string(dat), "\n")
historyLen := len(fullHistory)
var lastIndex int
for i := range fullHistory {
index := historyLen - 1 - i
cmd := fullHistory[index]
fullHistory[index] = strings.Join(strings.Split(fullHistory[index], ";")[1:], "")
if strings.Contains(cmd, "gowild record") {
lastIndex = index
break
}
}
recordedHistory := fullHistory[lastIndex+1 : historyLen-2]
return strings.Join(recordedHistory, "\n")
}
func isRecording() bool {
dat, err := ioutil.ReadFile(getConfigPath())
if err != nil || dat == nil {
return false
}
return string(dat) == "1"
}
func setRecording(recording bool) {
var value string
if recording {
value = "1"
} else {
value = "0"
}
f, err := os.Create(filepath.Join(getConfigPath()))
check(err)
_, err = f.WriteString(value)
check(err)
defer f.Close()
}
// ExecRecord begins a recording session by writing a flag file.
func ExecRecord() {
if isRecording() {
fmt.Println("Existing recording session found. Starting new recording session from here. Run 'gowild stop' to end recording")
} else {
fmt.Println("Now recording commands... run 'gowild stop' to end recording.\nTo start over, just run 'gowild record' again.")
}
setRecording(true)
}
// ExecStop ends a recording session by accessing bash history, writing an output file, and deleting the flag file.
func ExecStop() {
if !isRecording() {
fmt.Println("No recording session found. Run 'gowild record' to begin recording commands.")
return
}
writeFile()
deleteConfig()
}
func writeFile() {
filename := getOutputFilename()
path := filepath.Join(getCwd(), filename)
f, err := os.Create(path)
check(err)
_, err = f.WriteString("#!/bin/bash\n" + getBashHistory())
check(err)
defer f.Close()
err = os.Chmod(path, 0777)
check(err)
fmt.Printf("Recorded commands written to shell script %s\n", filepath.Join(getCwd(), filename))
}
func getOutputFilename() string {
defaultName := "gowild.sh"
scanner := bufio.NewScanner(os.Stdin)
fmt.Printf("What should the output file be named? [%s]: ", defaultName)
scanner.Scan()
filename := scanner.Text()
check(scanner.Err())
if strings.TrimSpace(filename) == "" {
filename = defaultName
}
if filename[len(filename)-3:] == ".sh" {
return filename
}
filename = filename + ".sh"
return filename
}
func testToggle() string {
if isRecording() {
return "stop"
}
return "record"
}
func main() {
var cmd string
if len(os.Args) < 2 {
PrintHelp("No command specified.", true)
return
} else if len(os.Args) > 2 {
PrintHelp("Too many arguments provided.", true)
return
}
cmd = os.Args[1]
if cmd == "record" {
ExecRecord()
} else if cmd == "stop" {
ExecStop()
} else if cmd == "help" {
PrintHelp("", false)
} else if cmd == "version" {
PrintVersion()
} else {
PrintHelp(cmd, true)
}
}