-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (102 loc) · 2.48 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
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
//yaml "gopkg.in/yaml.v2"
"github.com/manifoldco/promptui"
yaml "gopkg.in/yaml.v3"
)
func menuSelect(items []string) (string, error) {
prompt := promptui.Select{
Label: "Select Command",
Items: items,
}
_, result, err := prompt.Run()
if err != nil {
return "", err
}
return result, nil
}
func getConfig(str string) (Config, error) {
config := Config{}
configFile, err := ioutil.ReadFile(str)
yaml.Unmarshal(configFile, &config)
return config, err
}
func main() {
help := flag.Bool("h", false, "Show help")
config_file := flag.String("f", "fixtures.yaml", "the fixtures config")
isDaemon := flag.Bool("d", false, "No log, No prompt")
log_path := flag.String("log", "haniel.log", "the running log path")
socket_server := flag.String("l", "localhost:1234", "As socket Server Address default enable")
socket_client := flag.String("p", "", "As socket Client Address default disable '-p || -l'")
flag.Parse()
if *help {
flag.Usage()
return
}
fmt.Println("load fixtures config: ", *config_file)
config, err := getConfig(*config_file)
if err != nil {
log.Println(err)
}
if err = config.Verify(); err != nil {
fmt.Println("=== config error ===")
fmt.Println(err)
fmt.Println("=== config error ===")
}
logfile, err := os.OpenFile(*log_path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Panicln(err)
}
//logger := log.New(os.Stdout, "[DEV] ", log.LstdFlags)
logger := log.New(logfile, "[DEV] ", log.LstdFlags)
if *isDaemon {
logger = log.New(ioutil.Discard, "", log.LstdFlags)
}
ch_command := make(chan string)
ch_send := make(chan []byte, 128)
ch_recv := make(chan []byte, 128)
simulation := Simulation{
Input: ch_recv,
Output: ch_send,
Command: ch_command,
config: config,
logger: logger,
}
// Get Menu All Items
items := config.GetAllCommandName()
socketServer := &SocketServer{
logger: logger,
input: ch_send,
output: ch_recv,
}
if *socket_client == "" {
go socketServer.Listen(*socket_server)
} else {
go socketServer.Client(*socket_client)
}
// Core execute command
go simulation.Run()
// deal with socket input
go simulation.ipc()
// execute default need run command
simulation.RunDefault()
if *isDaemon {
select {}
} else {
for {
command, err := menuSelect(items)
if err != nil {
log.Println(err)
fmt.Println("Exit")
break
}
fmt.Printf("You choose %q\n", command)
simulation.Command <- command
}
}
}