-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflags.go
141 lines (121 loc) · 3.93 KB
/
flags.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
const (
Reset = "\033[0m"
Green = "\033[32m"
Cyan = "\033[36m"
Magenta = "\033[35m"
Yellow = "\033[33m"
Grey = "\033[90m"
Red = "\033[31m"
)
const helpText = `
Usage:
lab <extension> Create and open a new file (e.g., lab js)
lab <number> Open file by number
lab 0 Open config file
lab List all lab files
lab -v, --version Show version
lab -h, --help Show this help
lab -d, --delete <number> Delete file by number
lab -p, --path Show file path
lab -r, --run <number> <command> Run any command on the specified file (e.g., python, node, cat)
lab -r, --run <number> Run the file if it is executable
Examples:
lab js Create a JavaScript file
lab 1 Open most recent file
lab 0 Edit config
lab List all lab files
lab -d 2 Delete file #2
lab -p 1 Show the path of file #1
lab -r 1 Run file #1 if it is executable binary
lab -r 1 node Run the file #1 with Node.js
lab -r 2 vim Open in different editor
lab -r 3 cat View file contents
Configuration (.lab):
editor=nvim Your preferred editor
lifedays=7 Days to keep files
prefix=lab Prefix for filenames
Files are stored in ~/lab/ (or custom location via LABPATH)
Files expire after configured days (default 7)
`
func handleFlags(labVersion string, organizedFiles []os.DirEntry, labdir string) {
if len(os.Args) < 2 {
fmt.Println("Error: No flag provided. Use -v or -d with an index.")
return
}
flag := os.Args[1]
var file os.DirEntry
var fileDir string
if len(os.Args) > 2 {
index, err := strconv.Atoi(os.Args[2])
if err != nil || index < 1 || index > len(organizedFiles) {
fmt.Println("Invalid or missing file index.")
return
}
file = organizedFiles[index-1]
fileDir = filepath.Join(labdir, file.Name())
}
switch flag {
case "-v", "--version":
fmt.Printf("lab version %v\n", labVersion)
os.Exit(0)
case "-h", "--help":
fmt.Println(helpText)
case "-d", "--delete":
if fileDir == "" || file == nil {
fmt.Printf("\n " + Yellow + "No file specified for deletion. Use 'lab -d <number>'\n\n" + Reset)
return
}
err := os.Remove(fileDir)
if err != nil {
fmt.Printf(Red+"Error: Failed to delete '%s': %v\n"+Reset, file.Name(), err)
return
}
fmt.Printf("\n "+Red+"%v "+Reset+"has been deleted from the lab!\n\n", file.Name())
case "-p", "--path":
if fileDir == "" || file == nil {
fmt.Printf("\n " + Yellow + "No file specified for retrieving the path. Use 'lab -p <number>'\n\n" + Reset)
return
}
fmt.Println(fileDir)
case "-r", "--run":
if (len(os.Args)) < 3 {
fmt.Println("\n " + Yellow + "Missing arguments. Use 'lab -r <number> <command>' or simply 'lab -r <number>' if the file is executable\n" + Reset)
return
}
var cmd *exec.Cmd
var runner string
if len(os.Args) == 3 {
cmd = exec.Command(fileDir)
} else {
runner = strings.Join(os.Args[3:], " ")
if fileDir == "" || file == nil {
fmt.Println(Red + "No file specified to run. Provide a valid file number." + Reset)
return
}
if strings.Contains(runner, "'") || strings.Contains(runner, "\"") {
parts := parseCommand(runner)
cmd = exec.Command(parts[0], append(parts[1:], fileDir)...)
} else {
parts := strings.Fields(runner)
cmd = exec.Command(parts[0], append(parts[1:], fileDir)...)
}
}
cmd.Dir = labdir
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf(Red+"Error: Failed to execute '%s' on file '%s': %v\n"+Reset, runner, fileDir, err)
}
}
}