-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgod.go
347 lines (330 loc) · 9.73 KB
/
god.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"bytes"
"flag"
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"strings"
"time"
"github.com/carmark/pseudo-terminal-go/terminal"
"github.com/common-nighthawk/go-figure"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/olekukonko/tablewriter"
)
var version = "1.10"
var outb, errb bytes.Buffer // Buffer for command output
var cmdSlice, gitCmdSlice, allCmds []string // Slice for the shortened git commands
var gitShortcuts [][]string // 2-d slice for the git shortcuts
var whiteColor (*color.Color) = color.New(color.FgWhite, color.Bold)
var restartTerm bool = false // Handling the stdout issues.
var termChar = "#" // Character for non-git terminal commands.
var promptStr = "god > "
// Executes the terminal command and returns output.
// stdout parameter determines the output stream.
func execCmd(stdout bool, command string, args ...string) string {
// Remove the newline character.
command = strings.TrimSuffix(command, "\n")
// Prepare the command to execute.
cmd := exec.Command(command, args...)
// Set the correct output device.
if stdout {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
} else {
cmd.Stdout = &outb
cmd.Stderr = &errb
}
// Execute the command and return output
// depending on the stdout parameter.
cmd.Run()
if !stdout {
return outb.String()
}
return ""
}
// Search the given query in slice.
// Returns true if the element exists.
func searchInSlice(slice []string, query string) bool {
set := make(map[string]bool)
for _, v := range slice {
set[v] = true
}
return set[query]
}
// Returns a slice with given dimension parameter.
// Used for getting keys or values from a 2-d slice.
func getShortcutSlice(slice [][]string, d int) []string {
var shortcuts []string
for _, shortcut := range slice {
shortcuts = append(shortcuts, shortcut[d])
}
return shortcuts
}
// Prepare (shorten) the git commands.
func prepareCmds() []string {
// Show status if repository exists in directory.
execCmd(true, "git", "status")
// Trimming the string using sed.
// 's/^\s*//' -> Substitute the found expression (' ' with '').
// 's/ *[A-Z].*//' -> Remove the git command description
// && -> For the next command.
removeSpaces := "sed -e 's/^\\s*//' -e 's/ *[A-Z].*//' && "
// Parsing the git commands.
// grep '^ *[a-z]' -> Select the lines starting with indent.
// tr -d '*' -> Remove the '*' character.
parseGitCmd :=
"git help | grep '^ *[a-z]' | " + removeSpaces +
"git branch | tr -d '*' | " +
strings.Replace(removeSpaces, " -e 's/ *[A-Z].*//'", "", 1) +
"git remote"
cmdStr := execCmd(false, "sh", "-c", parseGitCmd)
gitCmdSlice = strings.Split(cmdStr, "\n")
for i, cmd := range gitCmdSlice {
if len(cmd) > 0 {
// Use the first character of git command
// for the new command if not exists in the
// commands slice. (cmdSlice)
// If first character is in the list, compose
// a two character abbreviation for it and
// add it to slice.
firstChar := string([]rune(cmd)[0])
if !searchInSlice(cmdSlice, firstChar) {
cmdSlice = append(cmdSlice, firstChar)
} else {
cmdSlice = append(cmdSlice, firstChar+
string([]rune(cmd)[len(cmd)/2]))
}
} else {
// Remove empty character
gitCmdSlice = append(gitCmdSlice[:i], gitCmdSlice[i+1:]...)
}
}
// Add git shortcuts.
gitShortcuts = append(gitShortcuts,
[]string{"add -A", "aa"},
[]string{"commit -m", "cmt"},
[]string{"remote -v", "rmt"},
[]string{"rm -r", "rr"},
[]string{"log --graph --decorate --all", "lo"},
[]string{"log --graph --decorate --oneline --all", "ll"},
[]string{"ls-files", "ls"},
[]string{"config credential.helper store", "crds"})
// Create a slice for storing all commands.
allCmds = append(gitCmdSlice, getShortcutSlice(gitShortcuts, 0)...)
return gitCmdSlice
}
// Create a git command from the given string.
// Returns changed/new command.
func buildCmd(line string) string {
line = " " + line + " "
// Run command without git if it starts
// with '#' character.
if strings.Contains(string([]rune(line)[:2]), termChar) {
return strings.Replace(line, " "+termChar, " ", 1)
}
// Support the commands starting with git.
line = strings.Replace(line, " git ", " ", -1)
// Use the first quoted part if command contains quote.
quotedPart := ""
if strings.Contains(line, "\"") {
quotedPart = strings.Join(strings.Split(line, "\"")[1:], "\"")
line = strings.Split(line, "\"")[0] + "\""
}
// Replace the shortened command with its original.
for index, cmd := range append(cmdSlice, getShortcutSlice(gitShortcuts, 1)...) {
cmd = " " + cmd + " "
if strings.Contains(line, cmd) {
line = strings.Replace(line, cmd, " "+allCmds[index]+" ", -1)
} else if strings.Contains(line, strings.ToUpper(cmd)) {
line = strings.Replace(line, strings.ToUpper(cmd), " "+allCmds[index]+" ", -1)
}
}
// Add quoted part back to the command.
if quotedPart != "" {
line += quotedPart
}
return "git" + line
}
// Start the interactive shell.
func startTerm(persistent bool) {
term, err := terminal.NewWithStdInOut()
if err != nil {
panic(err)
}
defer term.ReleaseFromStdInOut()
whiteColor.Println("Type '?' for help or 'git' for list of commands.")
term.SetPrompt(promptStr)
cmdLoop:
for {
// Read the keyboard input.
line, err := term.ReadLine()
// Exit on Ctrl-D and Ctrl-C (if -p not provided).
if (err == io.EOF) || (line == "^C" && !persistent) {
fmt.Println()
return
}
// Built-in commands.
switch line {
case "", " ":
break
case "clear":
execCmd(true, "clear")
case "exit":
break cmdLoop
case "?", "help":
showHelp()
case "version":
showVersion()
case "git":
showCommands()
case "god":
showWarning()
case "sc":
showShortcuts()
case "alias":
promptAlias()
default:
// Build the git command.
gitCmd := buildCmd(line)
// Release the std in/out for preventing the
// git username & password input issues.
if strings.Contains(gitCmd, " push") {
restartTerm = true
term.ReleaseFromStdInOut()
}
// Handle the execution of the input.
if strings.Contains(gitCmd, " god ") {
showWarning()
} else if retval := execCmd(true, "sh", "-c", gitCmd); len(retval) > 0 {
fmt.Fprintln(os.Stderr, retval)
}
// Restart the terminal for flushing the stdout.
// It is necessary for input required situations.
if restartTerm {
term, _ = terminal.NewWithStdInOut()
defer term.ReleaseFromStdInOut()
term.SetPrompt(promptStr)
restartTerm = false
}
}
}
}
// Takes 'table' parameter and returns colored.
func setTableColors(table *tablewriter.Table) *tablewriter.Table {
whiteTable := tablewriter.Colors{
tablewriter.Bold,
tablewriter.FgHiWhiteColor}
blackTable := tablewriter.Colors{
tablewriter.Bold,
tablewriter.FgHiBlackColor}
table.SetHeaderColor(whiteTable, whiteTable)
table.SetColumnColor(whiteTable, blackTable)
return table
}
// Display help message.
func showHelp() {
cliCmds := map[string]string{
"git": "List available git commands",
"sc": "List git shortcuts",
"alias": "Print shell or git aliases",
"help": "Show this help message",
"version": "Show version information",
"clear": "Clear the terminal",
"exit": "Exit shell"}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Command", "Description"})
table = setTableColors(table)
for k, v := range cliCmds {
table.Append([]string{k, v})
}
table.Render()
}
// Show git commands in table.
func showCommands() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Command", "git"})
table = setTableColors(table)
for index, cmd := range cmdSlice {
table.Append([]string{cmd, gitCmdSlice[index]})
}
table.Render()
}
// Show a warning from god.
func showWarning() {
messages := []string{
"Yes?",
"What?",
"Here.",
"What else do you want?",
"God hears you.",
"Hello, this is God. How may I be of assistance?",
"Repeating my name won't get you to the UNIX paradise.",
"You already have God's attention. Simply state your business.",
"Fetching God from a higher dimension... Please wait...",
}
rand.Seed(time.Now().Unix())
whiteColor.Println(messages[rand.Intn(len(messages))])
}
// Show commonly used git commands shortcuts.
func showShortcuts() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Shortcut", "Command"})
table = setTableColors(table)
for _, shortcut := range gitShortcuts {
table.Append([]string{shortcut[1], shortcut[0]})
}
table.Render()
}
// Save shortened commands as shell alias or Git alias.
func promptAlias() {
aliasOpts := []string{
"shell",
"git",
}
formats := map[string]string{
"shell": "alias %s='git %s'",
"git": "%s = %s",
}
aliasPrompt := promptui.Select{
Label: "Create alias for",
Items: aliasOpts,
}
_, selection, err := aliasPrompt.Run()
if err != nil {
fmt.Printf("Selection failed: %v\n", err)
}
if len(selection) > 0 {
whiteColor.Println("Alias list for " + selection + ":")
for index, cmd := range append(cmdSlice, getShortcutSlice(gitShortcuts, 1)...) {
alias := fmt.Sprintf(formats[selection], cmd, allCmds[index])
fmt.Println(alias)
}
}
}
// Show project information including version.
func showVersion() {
fmt.Println()
asciiFigure := figure.NewFigure("god", "cosmic", true)
asciiFigure.Print()
whiteColor.Println("\n ~ god:v" + version)
fmt.Println(" ~ utility for simplifying the git usage" +
"\n ~ github.com/orhun/god" +
"\n ~ licensed under gplv3\n")
}
func main() {
// If -v argument is given, show project information and exit.
// If not, start the god terminal.
versionFlag := flag.Bool("v", false, "Show version information")
persistentFlag := flag.Bool("p", false, "Don't exit on ^C")
flag.Parse()
if *versionFlag {
showVersion()
} else {
prepareCmds()
startTerm(*persistentFlag)
}
}