-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds.go
170 lines (138 loc) · 3.89 KB
/
cmds.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path"
"strings"
"github.com/fatih/color"
)
func checkCustom(text string) string { // check for custom command
return strings.Split(text, " ")[0]
}
func loadConfig(homedir string, reader *bufio.Reader) {
file, err := os.Open(path.Join(homedir, ".goshrc")) // open config file
if err != nil {
color.Red("failed to open ~/.goshrc")
color.Red(err.Error())
color.Red("if config isn't initialized, click enter to create default ~/.goshrc")
_, _ = reader.ReadString('\n')
err := file.Close()
if err != nil {
return
}
file, err := os.Create(path.Join(homedir, ".goshrc"))
if err != nil {
color.Red("failed to create ~/.goshrc")
color.Red(err.Error())
}
defer func() {
_ = file.Close()
}()
_, err = file.Write(defaultGoshrc)
if err != nil {
color.Red("failed to write to ~/.goshrc")
color.Red(err.Error())
}
}
defer func() {
_ = file.Close()
}()
data, _ := os.ReadFile(path.Join(homedir, ".goshrc"))
dbgPrint("Loading: \n", string(data))
scanner := bufio.NewScanner(file) // scan file
for scanner.Scan() {
if scanner.Text()[0] == '#' { // comments start with #
continue
}
splitText := strings.Split(scanner.Text(), ">>>") // alias keyword: >>>
aliases[splitText[0]] = splitText[1]
}
}
func initCmd(command string, args []string) *exec.Cmd {
var cmd *exec.Cmd
if len(args) == 0 || (len(args) == 1 && args[0] == "") { // create command without args if len(args) is 0 or the only arg is empty
cmd = exec.Command(command)
} else { // otherwise init with args
cmd = exec.Command(command, args...)
}
cmd.Dir = currentDir // current dir is command's dir
cmd.Env = os.Environ() // environmental variables are command's variables
cmd.Env = append(cmd.Env, "TERM=xterm-256color")
dbgPrint("Cmd: ", command)
dbgPrint("Args amount: ", len(args))
dbgPrint("Args: ", args)
return cmd
}
func alias(command string) string { // replace aliases + internal quickfixes
commandSplit := strings.Fields(command)
for key, val := range aliases {
if commandSplit[0] == key {
command = strings.Replace(command, key, val, 1)
}
}
for key, val := range aliasesInt {
if commandSplit[0] == key {
command = strings.Replace(command, key, val, 1)
}
}
return command
}
func cmdSplit(command string, splitKey string) []string {
var split []string
element := ""
for _, c := range command {
element += string(c)
if checkFor(element, splitKey) {
elementSplit := strings.Split(element, splitKey)
element = strings.Join(elementSplit[:len(elementSplit)-1], splitKey)
split = append(split, element)
element = ""
}
}
split = append(split, element)
return split
}
func checkFor(command string, keyword string) bool {
quotes := false
cmd := ""
for _, c := range command { // ignoring everything in quotes, it is not command
if c == '"' {
quotes = !quotes
}
if !quotes {
cmd += string(c)
}
}
return strings.Contains(cmd, keyword)
}
func parseCmd(command string) (string, []string) { // just works
cmd := "" // command to be outputted
var args []string // arguments
commandSplit := strings.Fields(command) // split with spaces
cmd = commandSplit[0] // get command
argsNP := strings.Join(commandSplit[1:], " ") // other parts will be arguments
quotes := false
arg := ""
for _, c := range argsNP {
if c == '"' { // quote toggle, it means 'hello" is valid quote, to be fixed
quotes = !quotes
continue
}
if !quotes && c == ' ' { // if not in quotes and space, new argument is created
args = append(args, arg)
arg = ""
continue
}
arg += string(c) // add character to argument
}
args = append(args, arg) // append last argument
return cmd, args
}
func dbgPrint(msg string, vars ...any) {
if *debug {
fmt.Print(color.RedString(msg))
fmt.Println(vars...)
}
}