-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.go
More file actions
134 lines (125 loc) · 2.27 KB
/
commands.go
File metadata and controls
134 lines (125 loc) · 2.27 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
127
128
129
130
131
132
133
134
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
//Public
//Cversion バージョンを表示する
func Cversion(v string) {
fmt.Printf("This version is %s\n", v)
}
//Chelp ヘルプを表示
func Chelp(p []string) {
switch len(p) {
case 1:
fmt.Println(helplist(""))
case 2:
if s := helplist(p[1]); s != "" {
fmt.Println(s)
} else {
error("help", "unknown command "+p[1], "Run 'run help'")
}
default:
error("help", "many parameter", "try again 'run help'")
}
}
//Chello Hello Worldと表示する
func Chello(p []string) {
if len(p) == 0 {
fmt.Println("Hello World")
return
}
for i := range p {
fmt.Printf("Hello %s\n", p[i])
}
}
//Cbuild コマンドを実行する
func Cbuild(p []string) {
for i := range p {
for _, s := range searchfile(p[i]) {
cmd := strings.Split(strings.Replace(s, "[name]", p[i], -1), " ")
result, err := exec.Command(cmd[0], cmd[1:]...).Output()
if err != nil {
error("build", "Hwlloo")
return
}
if 0 < len(result) {
fmt.Print(string(result))
continue
}
}
// コマンドが見つからなかった場合
}
}
//Private
func searchfile(s string) []string {
file, err := os.Open(s)
if err != nil {
error("build", "Can`t Open file")
}
defer file.Close()
scanner := bufio.NewScanner(file)
result := []string{}
for scanner.Scan() {
text := scanner.Text()
for i := 0; i < len(text); i++ {
if text[i] == ' ' {
continue
}
if text[i] == '/' && text[i+1] == '/' {
for t := i + 2; t < len(text); t++ {
if text[t] != ' ' {
i = t
break
}
}
result = append(result, string(text[i:]))
break
}
return result
}
}
return nil
}
func helplist(t string) string {
s := ""
switch t {
case "":
s = `
Usage:
run command [option] [...parameter]
Command:
version:
バージョンを表示する
help:
ヘルプを表示
hello:
Hello WorldまたはHello [...parameter]を出力
build:
設定ファイルまたはファイルの先頭からコマンドを取得し実行する
Options:
`
case "version":
s = `
Usage:
run version [option]
Option:
`
case "hello":
s = `
Usage:
run hello [option] [...parameter]
Option:
`
case "build":
s = `
Usage:
run build [option] [...file]
Option:
`
}
return s
}