-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (178 loc) · 4.03 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
/*
TODOS:
2. support other editors
4. figure out how to paste to prompt instead of just executing
5. remove panics, handle errors
6. enforce opts before text
7. main() is getting bloated...
*/
const (
logPath = "/usr/local/var/f"
logFile = "flog"
)
type options struct {
DryRun bool
CmdLogName string
}
func main() {
setup()
opts := flags()
file, err := ioutil.TempFile(logPath, "temp.*.sh")
if err != nil {
panic(err)
}
defer func() {
// not working
err = os.Remove(file.Name())
if err != nil {
log.Println(fmt.Sprintf("error removing temp file: %v", err))
}
}()
if len(os.Args) > 1 {
skipFlags := ""
for _, v := range os.Args[1:] {
if v[0] == '-' {
continue
}
skipFlags += " " + v
}
skipFlags = strings.TrimLeft(skipFlags, " ")
_, err := file.WriteString(skipFlags)
if err != nil {
panic(err)
}
}
if err := runEditor(file.Name()); err != nil {
panic(err)
}
b, err := os.ReadFile(file.Name())
if err != nil {
panic(err)
}
name, args := prepare(string(b))
if len(name) == 0 {
fmt.Println("noop")
os.Exit(0)
}
err = writeLog(opts.CmdLogName, string(b))
if err != nil {
log.Fatal(err)
}
if opts.DryRun {
fmt.Println(string(b))
os.Exit(0)
}
execAndWait(name, args)
}
func writeLog(name, cmd string) error {
// If the file doesn't exist, create it, or append to the file
file, err := os.OpenFile(logPath+"/"+logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.WriteString("--- " + name + "\n" + cmd)
return err
}
func setup() {
if _, err := os.Stat(logPath); os.IsNotExist(err) {
if err := os.Mkdir(logPath, os.ModePerm); err != nil {
panic(err)
}
}
}
func flags() options {
// get flags
helpFlag := flag.Bool("help", false, "print usage information")
helpFlagShort := flag.Bool("h", false, "print usage information (shorthand)")
dryRunFlag := flag.Bool("dry-run", false, "print command without execution")
historyFlag := flag.Bool("history", false, "print command log")
cmdLogNameFlag := flag.String("log", "", "label command and save to f's history")
cmdLogNameFlagShort := flag.String("l", "", "label command and save to f's history (shorthand)")
flag.Parse()
// handle flags
if *helpFlag || *helpFlagShort {
fmt.Print(help)
os.Exit(0)
}
if *historyFlag {
// lazy...
// What? this is not enterprise software.
// This will error if logFile not exists.
cmd := exec.Command("cat", logPath+"/"+logFile)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
_ = cmd.Run()
os.Exit(0)
}
var opts options
max := func(a, b string) string {
if len(b) > len(a) {
return b
}
return a
}
if n := max(*cmdLogNameFlag, *cmdLogNameFlagShort); n != "" {
opts.CmdLogName = n
}
if *dryRunFlag {
opts.DryRun = true
opts.CmdLogName = strings.Join([]string{opts.CmdLogName, "(dry-run)"}, " ")
}
return opts
}
func prepare(s string) (string, []string) {
s = collapse(s)
s = clean(s)
slc := strings.Split(s, " ")
return slc[0], slc[1:]
}
// convert multiline commands (e.g. `\`) to single line
func collapse(s string) string {
var sb strings.Builder
for i, v := range s {
if (v == '\\' && s[i+1] == '\n') || v == '\n' {
continue
}
sb.WriteRune(v)
}
return sb.String()
}
func clean(s string) string {
return strings.TrimSpace(s)
}
func runEditor(path string) error {
cmd := exec.Command("vi", path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
return cmd.Run()
}
func execAndWait(name string, args []string) {
cmd := exec.Command(name, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
panic(err)
}
defer cmd.Wait()
}
const help = `
Usage: f
or: f [options] [text] Open editor with text provided e.g. f !! to open with last command
Options:
-l, --label Label command and write to log.
--history Print command history (only named commands are saved).
--dry-run Print command without execution.
`