-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
395 lines (341 loc) · 9.39 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/urfave/cli/v2"
)
type script struct {
name string
executable string
editable string
}
/*
==========
Utillities
==========
*/
// Returns the users home directory
func getHomeDir() string {
homeDir, err := os.UserHomeDir()
check(err)
return filepath.Join(homeDir)
}
var configScriptsFilePath string = filepath.Join(getHomeDir(), "/.config/scripthub/scripts")
var configScripthubPath string = filepath.Join(getHomeDir(), "/.config/scripthub")
// Error checker
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
// Returns a list of all the scripts in the config file
func getScripts() []script {
scriptPaths, err := os.ReadFile(configScriptsFilePath)
check(err)
scriptLines := strings.Split(string(scriptPaths), "\n")
var scriptsStruct []script
for i := 0; i < len(scriptLines)-1; i += 1 {
split := strings.Split(scriptLines[i], " : ")
scriptsStruct = append(
scriptsStruct,
script{
name: split[0],
executable: split[1],
editable: split[2],
},
)
}
return scriptsStruct
}
// Returns the script struct of a given script
func getScriptStruct(name string) (res script, err error) {
scripts := getScripts()
// Find the specified script
for i := 0; i < len(scripts); i += 1 {
if scripts[i].name == name {
res = scripts[i]
break
}
}
if res.name == "" {
err = fmt.Errorf("Error: Could not find script \"%s\" in scripts", name)
}
return
}
/*
================
Action functions
================
*/
// Appends a script to the script file
func addScript(newScript script) (err error) {
cwd, err := os.Getwd()
if err != nil {
return
}
// Make the executable path
newScript.executable = filepath.Join(cwd, newScript.executable)
// Check if editable is set, else use executable
if newScript.editable == "" {
newScript.editable = newScript.executable
} else {
newScript.editable = filepath.Join(cwd, newScript.editable)
}
// Write new script entry to the scripts file
file, err := os.OpenFile(configScriptsFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return
}
defer file.Close()
// Put togheter the script entry string and write to file
scriptString := newScript.name + " : " + newScript.executable + " : " + newScript.editable + "\n"
if _, err = file.WriteString(scriptString); err != nil {
return
}
file.Close()
return
}
// Opens the editable script file in the EDITOR env variable
func editScript(name string) (err error) {
// Get all the scripts
scripts := getScripts()
var scriptPath string
// Find the editable path for specified script
for i := 0; i < len(scripts); i += 1 {
if scripts[i].name == name {
scriptPath = scripts[i].editable
break
}
}
if scriptPath == "" {
return fmt.Errorf("Error: Could not find script \"%s\" in scripts", name)
}
// Open script in $EDITOR env variable (Defaults to `vim`)
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
cmd := exec.Command(editor, scriptPath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err = cmd.Run()
check(err)
return
}
// Removes script from the script file
func removeScript(name string) (err error) {
scriptsFile, err := os.ReadFile(configScriptsFilePath)
scriptLines := string(scriptsFile)
// Remove line starting with {name}
re := regexp.MustCompile("(?m)[\r\n]+^" + name + ".*$")
res := re.ReplaceAllString(scriptLines, "")
// Overwrite the scripts file
os.WriteFile(configScriptsFilePath, []byte(res), 0777)
return
}
// Runs the script with the same name as passed in as argument
func runScript(name string) (err error) {
var executablePath string
res, err := getScriptStruct(name)
check(err)
executablePath = res.executable
if executablePath == "" {
err = fmt.Errorf("Error: Could not find script \"%s\" in scripts", name)
}
cmd := exec.Command(executablePath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err = cmd.Run()
check(err)
return
}
// Returns the path of the given script, can be specified using the option param
func getPath(name string, option string) (res string, err error) {
temp, err := getScriptStruct(name)
check(err)
if option == "executable" || option == "x" {
res = temp.executable
} else if option == "editable" || option == "e" {
res = temp.editable
} else {
res += fmt.Sprintf("Name : %s\n", temp.name)
res += fmt.Sprintf("Editable : %s\n", temp.editable)
res += fmt.Sprintf("Executable : %s\n", temp.executable)
}
return
}
// Sets up scripthub files and folders is needed
func setup() {
// If scripts file does not exist is scripthub config folder
if _, err := os.Stat(configScriptsFilePath); os.IsNotExist(err) {
fmt.Println("Could not find scripts file. Generating one.")
fmt.Println("")
if _, err := os.Stat(configScripthubPath); os.IsNotExist(err) {
// If scripthub config folder does not exist
err := os.Mkdir(configScripthubPath, 0666)
check(err)
err = os.WriteFile(configScriptsFilePath, []byte(""), 0777)
check(err)
fmt.Printf("Generated scripts file at: %s\n", configScriptsFilePath)
} else {
// If scripthub config folder does exist, but scripts file doesn't
err = os.WriteFile(configScriptsFilePath, []byte(""), 0777)
check(err)
fmt.Printf("Generated scripts file at: %s\n", configScriptsFilePath)
}
} else {
// Scriphub is already set up
fmt.Println("Scripts file found. No setup needed")
fmt.Println("Here are the scripts in your library: ")
scriptStructs := getScripts()
fmt.Println("")
for i := 0; i < len(scriptStructs); i += 1 {
fmt.Printf("Name : %s\n", scriptStructs[i].name)
fmt.Printf("Executable : %s\n", scriptStructs[i].executable)
fmt.Printf("Editable : %s\n", scriptStructs[i].editable)
fmt.Println("============")
}
}
}
func main() {
var newScriptStruct script
var specifier string
app := &cli.App{
Name: "scripthub",
Usage: "Keep track of all your scripts",
Commands: []*cli.Command{
// ================================================================
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List all available scripts",
Action: func(*cli.Context) (err error) {
scriptStructs := getScripts()
for i := 0; i < len(scriptStructs); i += 1 {
fmt.Println("Name : " + scriptStructs[i].name)
fmt.Println("Executable : " + scriptStructs[i].executable)
fmt.Println("Editable : " + scriptStructs[i].editable)
fmt.Println("============")
}
return
},
},
// ================================================================
{
Name: "edit",
Aliases: []string{"e"},
Usage: "Edit the given script",
Action: func(c *cli.Context) (err error) {
editScriptName := c.Args().First()
err = editScript(editScriptName)
check(err)
return
},
ArgsUsage: "scriptName",
},
// ================================================================
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a script to your library",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Name of script to add",
Required: true,
DefaultText: "",
Destination: &newScriptStruct.name,
},
&cli.StringFlag{
Name: "editable",
Aliases: []string{"e"},
Usage: "Path to editable",
Value: "",
DefaultText: "Same as --executable",
Destination: &newScriptStruct.editable,
},
&cli.StringFlag{
Name: "executable",
Aliases: []string{"x"},
Usage: "Path to executable",
Required: true,
DefaultText: "",
Destination: &newScriptStruct.executable,
},
},
Action: func(c *cli.Context) (err error) {
err = addScript(newScriptStruct)
check(err)
return
},
},
// ================================================================
{
Name: "remove",
Aliases: []string{"rm"},
Usage: "Remove a script from your library",
ArgsUsage: "{script name}",
Action: func(c *cli.Context) (err error) {
name := c.Args().First()
err = removeScript(name)
check(err)
return
},
},
// ================================================================
{
Name: "run",
Aliases: []string{"r"},
Usage: "Run a script from your library",
ArgsUsage: "{script name}",
Action: func(c *cli.Context) (err error) {
name := c.Args().First()
err = runScript(name)
check(err)
return
},
},
// ================================================================
{
Name: "path",
Aliases: []string{"p"},
Usage: "Get paths of a script",
ArgsUsage: "{script name}",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "specifier",
Aliases: []string{"s"},
Destination: &specifier,
},
},
Action: func(c *cli.Context) (err error) {
name := c.Args().First()
res, err := getPath(name, specifier)
fmt.Println(res)
check(err)
return
},
},
// ================================================================
{
Name: "setup",
Usage: "Set up scripthub",
Action: func(c *cli.Context) (err error) {
setup()
return
},
},
// ================================================================
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}