-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (83 loc) · 1.84 KB
/
main.go
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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
)
type event struct {
Id int64
Context string
Datetime string
Excutetime string
}
type Job struct {
newjob event
jobtimer *time.Timer
}
// type JobInfo struct {
// Id int64 `json:"id" validate:"required"`
// JobType int64 `json:"type" validate:"required,max=1"`
// Context string `json:"context" validate:"required"`
// Datetime string `json:"datatime" validate:"required"`
// }
func printls(abc string) {
println(abc)
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Simple Shell")
fmt.Println("---------------------")
for {
fmt.Print("add cmd-> ")
text, _ := reader.ReadString('\n')
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
strArray := strings.Split(text, " ")
if strings.Compare("exit", text) == 0 {
fmt.Println("Bye~have great time")
break
}
binary, lookErr := exec.LookPath(strArray[0])
if lookErr == nil {
args := strArray[1:]
cmd := exec.Command(binary, args...)
fmt.Print("add time-> ")
var time_input int64
fmt.Scan(&time_input)
f := func() {
stdout, err := cmd.StdoutPipe()
defer stdout.Close()
if err != nil {
log.Fatal(err)
}
// 保证关闭输出流
defer stdout.Close()
// 运行命令
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// 读取输出结果
opBytes, err := ioutil.ReadAll(stdout)
if err != nil {
log.Fatal(err)
}
log.Println(string(opBytes))
}
fmt.Println(time_input)
time.AfterFunc(time.Duration(time_input)*time.Second, f)
} else {
fmt.Print("input error")
reader.ReadString('\n')
}
}
println("GG")
// compeltech := make(chan int64)
// case1 := event{1, "GG1", "", ""}
// job1 := make{Job}
// case2 := event{2, "GG2", "", ""}
}