-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
126 lines (94 loc) · 1.81 KB
/
Copy pathexec.go
File metadata and controls
126 lines (94 loc) · 1.81 KB
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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/eiannone/keyboard"
)
const maxAliasFileSize = 1 << 20
var alias = map[string][]string{}
func init() {
setupAlias()
}
// Executes the given inputs
func execInput(input string) {
input = strings.TrimSuffix(input, "\n")
input = strings.TrimSpace(input)
args := strings.Split(input, " ")
program := strings.TrimSpace(args[0])
switch program {
case "cd":
if len(args) < 2 {
fmt.Fprintln(os.Stderr, ErrNoPath.Error())
return
}
os.Chdir(args[1])
return
case "exit":
keyboard.Close()
os.Exit(0)
}
split := 0
for _, v := range args[1:] {
v = strings.TrimSpace(v)
if v != "" {
args[split] = v
split++
}
}
if _, ok := alias[program]; ok {
key := program
program = alias[program][0]
args = append(alias[key][1:], args[:split]...)
} else {
args = args[:split]
}
cmd := exec.Command(program, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
fmt.Printf("%s: %v\n", program, ErrCommantNotFound)
}
}
}
// Populates alias map from alias file
func setupAlias() {
file, err := os.Open("alias")
if err != nil {
return
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return
}
if maxAliasFileSize < fi.Size() {
panic("alias file is too large!")
}
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err != nil {
return
}
keyValue := strings.Split(string(line), "=")
if len(keyValue) != 2 {
continue
}
key := strings.TrimSpace(keyValue[0])
values := strings.Split(keyValue[1], " ")
var args = []string{}
for _, v := range values {
v = strings.TrimSpace(v)
if v != "" {
args = append(args, v)
}
}
alias[key] = args
}
}