瀏覽代碼

Implement #EI0

Alois Mahdal 6 天之前
父節點
當前提交
3458e5744e
共有 1 個檔案被更改,包括 14 行新增0 行删除
  1. 14
    0
      app/main.go

+ 14
- 0
app/main.go 查看文件

@@ -76,6 +76,7 @@ const (
76 76
 	CommandTypeBuiltinExit
77 77
 	CommandTypeBuiltinEcho
78 78
 	CommandTypeBuiltinType
79
+	CommandTypeBuiltinPwd
79 80
 )
80 81
 
81 82
 func getCommand(ctx *Context) (Command, error) {
@@ -125,6 +126,9 @@ func parseCommand(command_line string) (Command, error) {
125 126
 	if tokens[0] == "exit" {
126 127
 		return makeCommandBuiltin(CommandTypeBuiltinExit, tokens), nil
127 128
 	}
129
+	if tokens[0] == "pwd" {
130
+		return makeCommandBuiltin(CommandTypeBuiltinPwd, tokens), nil
131
+	}
128 132
 	if tokens[0] == "echo" {
129 133
 		return makeCommandBuiltin(CommandTypeBuiltinEcho, tokens), nil
130 134
 	}
@@ -147,6 +151,16 @@ func handleCommand(ctx Context, command Command) ExitStatus {
147 151
 		os.Exit(0)
148 152
 	case CommandTypeBuiltinEcho:
149 153
 		fmt.Fprintln(ctx.stdout, strings.Join(command.tokens[1:], " "))
154
+	case CommandTypeBuiltinPwd:
155
+		if len(command.tokens) != 1 {
156
+			fmt.Fprintln(ctx.stderr, "usage: pwd")
157
+			return 2
158
+		}
159
+		cwd_path, err := os.Getwd()
160
+		if err != nil {
161
+			panic("error getting current working directory")
162
+		}
163
+		fmt.Fprintln(ctx.stdout, cwd_path)
150 164
 	case CommandTypeBuiltinType:
151 165
 		if len(command.tokens) != 2 {
152 166
 			fmt.Fprintln(ctx.stderr, "usage: type COMMAND")