-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
149 lines (134 loc) · 3.62 KB
/
type.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package command
import (
"encoding/json"
"fmt"
"log"
)
type NullArgs struct {
}
type BaseArgs struct {
ENV struct {
HostName string `json:"host_name"`
System string `json:"system"`
Arch string `json:"arch"`
GoVersion string `json:"go_version"`
Timezone string `json:"timezone"`
CpuNums int `json:"cpu_nums"`
CpuThreadNum int `json:"cpu_thread_num"`
MemorySize int `json:"memory_size"`
PWD string `json:"pwd"`
OsArgs []string `json:"os_args"`
CurrentCommand string `json:"current_command"`
NextStepArgs []string `json:"next_step_args"`
} `json:"env"`
}
type Type int
const (
TypeOfSystem Type = iota
TypeOfExport
TypeOfFix
)
type commandParams struct {
jsonBytes []byte
}
type ArgRelation struct {
Index int
Name string
}
type Command struct {
Command string
Type Type
Desc string
MinArgs int
//ArgsRelations []ArgRelation
ArgsRelations []string
NextCommand *Command
SubCommands []*Command
commandExecute func(params commandParams, command *Command)
env map[string]interface{}
}
func (c *Command) Next(args BaseArgs) {
if c.NextCommand != nil {
c.executeNext(args.ENV.NextStepArgs)
} else {
log.Println("NextCommand is nil", args)
}
}
func (c *Command) ExecuteSub(args []string) {
c.executeSub(args)
}
func (c *Command) executeNext(args []string) {
commandInstance := c.NextCommand
if len(args) < commandInstance.MinArgs {
//display help
log.Println("at least "+fmt.Sprintf("%d", commandInstance.MinArgs)+" params", commandInstance)
return
}
var argsRelation = make(map[string]interface{})
c.env["current_command"] = args[0]
c.env["next_step_args"] = args
argsRelation["env"] = c.env
for index, arg := range args[1:] {
for paramIndex, paramName := range commandInstance.ArgsRelations {
if paramIndex == index {
argsRelation[paramName] = arg
}
}
}
argParamsBytes, _ := json.Marshal(argsRelation)
argParams := commandParams{
jsonBytes: argParamsBytes,
}
commandInstance.env = c.env
commandInstance.commandExecute(argParams, commandInstance)
if commandInstance.NextCommand != nil {
var baseArgs = BaseArgs{}
json.Unmarshal(argParamsBytes, &baseArgs)
baseArgs.ENV.NextStepArgs = baseArgs.ENV.NextStepArgs[1:]
commandInstance.Next(baseArgs)
}
}
func (c *Command) executeSub(args []string) {
var command = args[0]
var commandInstance *Command
for i, child := range c.SubCommands {
if child.Command == command {
commandInstance = c.SubCommands[i]
break
}
}
if commandInstance != nil {
if len(args)-1 < commandInstance.MinArgs {
//display help
log.Println("at least "+fmt.Sprintf("%d", commandInstance.MinArgs)+" params", commandInstance)
return
}
var argsRelation = make(map[string]interface{})
c.env["next_step_args"] = args[1:]
c.env["current_command"] = args[0]
argsRelation["env"] = c.env
for index, arg := range args[1:] {
for paramIndex, paramName := range commandInstance.ArgsRelations {
if paramIndex == index {
argsRelation[paramName] = arg
}
}
}
argParamsBytes, _ := json.Marshal(argsRelation)
argParams := commandParams{
jsonBytes: argParamsBytes,
}
//json.Unmarshal(argParams, argParams)
//commandInstance. = argParams
commandInstance.env = c.env
commandInstance.commandExecute(argParams, commandInstance)
if commandInstance.NextCommand != nil {
var baseArgs = BaseArgs{}
json.Unmarshal(argParamsBytes, &baseArgs)
baseArgs.ENV.NextStepArgs = baseArgs.ENV.NextStepArgs[1:]
commandInstance.Next(baseArgs)
}
} else {
log.Printf("cant found the command %s\n", command)
}
}