cloud world

To be A geek

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
package main

import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)

func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
// 读取键盘的输入.
input, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

// 执行并解析command.
err = execInput(input)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}

// 如果cd命令没有路径的话,就报下面的错误
var ErrNoPath = errors.New("path required")

func execInput(input string) error {
// 移除换行符.
input = strings.TrimSuffix(input, "\n")

// 将输入分割成参数.
args := strings.Split(input, " ")

// 对cd命令的情况进行区分.
switch args[0] {
case "cd":
// 暂时不支持cd加空格进入home目录.
if len(args) < 2 {
return ErrNoPath
}
err := os.Chdir(args[1])
if err != nil {
return err
}
// Stop further processing.
return nil
case "exit":
os.Exit(0)
}

// Prepare the command to execute.
cmd := exec.Command(args[0], args[1:]...)

// Set the correct output device.
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

// Execute the command and save it's output.
err := cmd.Run()
if err != nil {
return err
}
return nil
}
1
2
//执行并测试
go run main.go

暂时不支持tab键自动补全命令,只是提供一种简单的思路。


Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

0%