浏览代码

Implement #EI0

Alois Mahdal 6 天前
父节点
当前提交
3458e5744e
共有 1 个文件被更改,包括 14 次插入0 次删除
  1. 14
    0
      app/main.go

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

76
 	CommandTypeBuiltinExit
76
 	CommandTypeBuiltinExit
77
 	CommandTypeBuiltinEcho
77
 	CommandTypeBuiltinEcho
78
 	CommandTypeBuiltinType
78
 	CommandTypeBuiltinType
79
+	CommandTypeBuiltinPwd
79
 )
80
 )
80
 
81
 
81
 func getCommand(ctx *Context) (Command, error) {
82
 func getCommand(ctx *Context) (Command, error) {
125
 	if tokens[0] == "exit" {
126
 	if tokens[0] == "exit" {
126
 		return makeCommandBuiltin(CommandTypeBuiltinExit, tokens), nil
127
 		return makeCommandBuiltin(CommandTypeBuiltinExit, tokens), nil
127
 	}
128
 	}
129
+	if tokens[0] == "pwd" {
130
+		return makeCommandBuiltin(CommandTypeBuiltinPwd, tokens), nil
131
+	}
128
 	if tokens[0] == "echo" {
132
 	if tokens[0] == "echo" {
129
 		return makeCommandBuiltin(CommandTypeBuiltinEcho, tokens), nil
133
 		return makeCommandBuiltin(CommandTypeBuiltinEcho, tokens), nil
130
 	}
134
 	}
147
 		os.Exit(0)
151
 		os.Exit(0)
148
 	case CommandTypeBuiltinEcho:
152
 	case CommandTypeBuiltinEcho:
149
 		fmt.Fprintln(ctx.stdout, strings.Join(command.tokens[1:], " "))
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
 	case CommandTypeBuiltinType:
164
 	case CommandTypeBuiltinType:
151
 		if len(command.tokens) != 2 {
165
 		if len(command.tokens) != 2 {
152
 			fmt.Fprintln(ctx.stderr, "usage: type COMMAND")
166
 			fmt.Fprintln(ctx.stderr, "usage: type COMMAND")