|
|
@@ -7,10 +7,17 @@ import "strings"
|
|
7
|
7
|
import "github.com/codecrafters-io/shell-starter-go/app/core"
|
|
8
|
8
|
|
|
9
|
9
|
type Builtin interface {
|
|
10
|
|
- Run(ctx *core.Context) core.ExitStatus
|
|
|
10
|
+ Run(ctx *core.Context) core.ActionResult
|
|
11
|
11
|
ParseArgs(ctx *core.Context, args []string) error
|
|
12
|
12
|
}
|
|
13
|
13
|
|
|
|
14
|
+func wrapEs(es int) core.ActionResult {
|
|
|
15
|
+ return core.ActionResult{
|
|
|
16
|
+ Code: core.ActionResultCodeBuiltinCommand,
|
|
|
17
|
+ ExitStatus: core.ExitStatus(es),
|
|
|
18
|
+ }
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
14
|
21
|
func Parse(ctx *core.Context, name string, args []string) (Builtin, error) {
|
|
15
|
22
|
selected, err := selectBuiltin(name)
|
|
16
|
23
|
if err != nil {
|
|
|
@@ -91,20 +98,20 @@ func (self *CdBuiltin) ParseArgs(ctx *core.Context, args []string) error {
|
|
91
|
98
|
}
|
|
92
|
99
|
}
|
|
93
|
100
|
|
|
94
|
|
-func (self *CdBuiltin) Run(ctx *core.Context) core.ExitStatus {
|
|
95
|
|
- chdir := func(path string) core.ExitStatus {
|
|
|
101
|
+func (self *CdBuiltin) Run(ctx *core.Context) core.ActionResult {
|
|
|
102
|
+ chdir := func(path string) core.ActionResult {
|
|
96
|
103
|
err := os.Chdir(path)
|
|
97
|
104
|
if err != nil {
|
|
98
|
105
|
fmt.Fprintf(ctx.Stderr, "cd: %s: No such file or directory\n", path)
|
|
99
|
|
- return 4
|
|
|
106
|
+ return wrapEs(4)
|
|
100
|
107
|
}
|
|
101
|
|
- return 0
|
|
|
108
|
+ return wrapEs(0)
|
|
102
|
109
|
}
|
|
103
|
110
|
if self.path == "~" || self.path == "" {
|
|
104
|
111
|
home_path := os.Getenv("HOME")
|
|
105
|
112
|
if len(home_path) == 0 {
|
|
106
|
113
|
fmt.Fprintln(ctx.Stderr, "error: $HOME environment variable is empty or unset")
|
|
107
|
|
- return 4
|
|
|
114
|
+ return wrapEs(4)
|
|
108
|
115
|
}
|
|
109
|
116
|
return chdir(home_path)
|
|
110
|
117
|
}
|
|
|
@@ -124,9 +131,9 @@ func (self *EchoBuiltin) ParseArgs(ctx *core.Context, args []string) error {
|
|
124
|
131
|
return nil
|
|
125
|
132
|
}
|
|
126
|
133
|
|
|
127
|
|
-func (self *EchoBuiltin) Run(ctx *core.Context) core.ExitStatus {
|
|
|
134
|
+func (self *EchoBuiltin) Run(ctx *core.Context) core.ActionResult {
|
|
128
|
135
|
fmt.Fprintln(ctx.Stdout, strings.Join(self.args, " "))
|
|
129
|
|
- return 0
|
|
|
136
|
+ return wrapEs(0)
|
|
130
|
137
|
}
|
|
131
|
138
|
|
|
132
|
139
|
//
|
|
|
@@ -143,9 +150,9 @@ func (self *ExitBuiltin) ParseArgs(ctx *core.Context, args []string) error {
|
|
143
|
150
|
return nil
|
|
144
|
151
|
}
|
|
145
|
152
|
|
|
146
|
|
-func (self *ExitBuiltin) Run(ctx *core.Context) core.ExitStatus {
|
|
|
153
|
+func (self *ExitBuiltin) Run(ctx *core.Context) core.ActionResult {
|
|
147
|
154
|
os.Exit(0)
|
|
148
|
|
- return 0
|
|
|
155
|
+ return wrapEs(0)
|
|
149
|
156
|
}
|
|
150
|
157
|
|
|
151
|
158
|
//
|
|
|
@@ -162,13 +169,13 @@ func (self *PwdBuiltin) ParseArgs(ctx *core.Context, args []string) error {
|
|
162
|
169
|
return nil
|
|
163
|
170
|
}
|
|
164
|
171
|
|
|
165
|
|
-func (self *PwdBuiltin) Run(ctx *core.Context) core.ExitStatus {
|
|
|
172
|
+func (self *PwdBuiltin) Run(ctx *core.Context) core.ActionResult {
|
|
166
|
173
|
cwd_path, err := os.Getwd()
|
|
167
|
174
|
if err != nil {
|
|
168
|
175
|
panic("error getting current working directory")
|
|
169
|
176
|
}
|
|
170
|
177
|
fmt.Fprintln(ctx.Stdout, cwd_path)
|
|
171
|
|
- return 0
|
|
|
178
|
+ return wrapEs(0)
|
|
172
|
179
|
}
|
|
173
|
180
|
|
|
174
|
181
|
//
|
|
|
@@ -187,17 +194,17 @@ func (self *TypeBuiltin) ParseArgs(ctx *core.Context, args []string) error {
|
|
187
|
194
|
return nil
|
|
188
|
195
|
}
|
|
189
|
196
|
|
|
190
|
|
-func (self *TypeBuiltin) Run(ctx *core.Context) core.ExitStatus {
|
|
|
197
|
+func (self *TypeBuiltin) Run(ctx *core.Context) core.ActionResult {
|
|
191
|
198
|
_, err := selectBuiltin(self.query)
|
|
192
|
199
|
if err == nil {
|
|
193
|
200
|
fmt.Fprintf(ctx.Stdout, "%s is a shell builtin\n", self.query)
|
|
194
|
|
- return 0
|
|
|
201
|
+ return wrapEs(0)
|
|
195
|
202
|
}
|
|
196
|
203
|
exec_path, err := exec.LookPath(self.query)
|
|
197
|
204
|
if err == nil {
|
|
198
|
205
|
fmt.Fprintf(ctx.Stdout, "%s is %s\n", self.query, exec_path)
|
|
199
|
|
- return 0
|
|
|
206
|
+ return wrapEs(0)
|
|
200
|
207
|
}
|
|
201
|
208
|
fmt.Fprintf(ctx.Stderr, "%s: not found\n", self.query)
|
|
202
|
|
- return 0
|
|
|
209
|
+ return wrapEs(0)
|
|
203
|
210
|
}
|