-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcommand.go
More file actions
71 lines (56 loc) · 1.67 KB
/
command.go
File metadata and controls
71 lines (56 loc) · 1.67 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
package command
import (
"errors"
"fmt"
"github.com/openware/pkg/nats/protocol"
)
const (
RESTART_SERVICE_COMMAND = "service_restart"
LOAD_CONFIG_COMMAND = "load_config"
SET_CONFIG_VALUE_COMMAND = "set_config"
)
var (
InvalidParamError = errors.New("invalid param in payload")
)
type (
ServiceRestartCommand protocol.RequestMessage
LoadConfigCommand protocol.RequestMessage
SetConfigValueCommand protocol.RequestMessage
)
func NewServiceRestartCommand(msgId uint32) *ServiceRestartCommand {
return (*ServiceRestartCommand)(protocol.NewRequestMessage(msgId, RESTART_SERVICE_COMMAND, nil))
}
func NewLoadConfigCommand(msgId uint32, param string) *LoadConfigCommand {
return (*LoadConfigCommand)(protocol.NewRequestMessage(msgId, LOAD_CONFIG_COMMAND, []interface{}{param}))
}
func NewSetConfigValueCommand(msgId uint32, config []string) *SetConfigValueCommand {
return (*SetConfigValueCommand)(protocol.NewRequestMessage(msgId, SET_CONFIG_VALUE_COMMAND, []interface{}{config}))
}
func (command *LoadConfigCommand) ReadParam() (string, error) {
if len(command.Params) != 1 {
return "", InvalidParamError
}
val, ok := command.Params[0].(string)
if !ok {
return "", InvalidParamError
}
return val, nil
}
func (command *SetConfigValueCommand) ReadConfig() ([]string, error) {
if len(command.Params) != 1 {
return nil, InvalidParamError
}
switch command.Params[0].(type) {
case []string:
return command.Params[0].([]string), nil
case []interface{}:
val := command.Params[0].([]interface{})
params := make([]string, 2)
for i, param := range val {
params[i] = param.(string)
}
return params, nil
default:
return nil, fmt.Errorf("invalid type")
}
}