-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmm.go
180 lines (153 loc) · 5.46 KB
/
cmm.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cmm
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
setValue = "/SetValue"
setValueIfNeeded = "/SetValueIfNeeded"
changeValue = "/ChangeValue"
switchValue = "/SwitchValue"
getValue = "/GetValue"
turnOff = "/TurnOff"
turnOn = "/TurnOn"
switchOffOn = "/SwitchOffOn"
saveConfig = "/SaveConfig"
loadConfig = "/LoadConfig"
sText = "/stext"
sTab = "/stab"
sComma = "/scomma"
sHtml = "/shtml"
sVerHtml = "/sverhtml"
sXml = "/sxml"
sJson = "/sjson"
sMonitors = "/smonitors"
)
// CommandExecutor interface defines the method for executing commands
type CommandExecutor interface {
Execute(args []string) (string, error)
}
// executor struct uses a CommandExecutor to execute commands
type Executor struct {
commandExecutor CommandExecutor
}
// SystemCommandExecutor is a concrete implementation of CommandExecutor
// that executes system commands using the executable name stored in it
type systemCommandExecutor struct {
executable string
}
// Execute runs a command using SystemCommandExecutor's executable
func (sce *systemCommandExecutor) Execute(args []string) (string, error) {
var out bytes.Buffer
cmd := exec.Command(sce.executable, args...)
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("error executing command: %w", err)
}
return out.String(), nil
}
// Constructor for executor
func ControlMyMonitor(executable string) (*Executor, error) {
if executable == "" {
return nil, errors.New("executable cannot be empty")
}
if !isExecutableInPath(executable) {
return nil, errors.New("executable missing from PATH")
}
// Instantiate SystemCommandExecutor with the executable name
ce := &systemCommandExecutor{executable: executable}
return &Executor{commandExecutor: ce}, nil
}
func isExecutableInPath(executable string) bool {
// Retrieve the PATH environment variable
path := os.Getenv("PATH")
paths := strings.Split(path, string(os.PathListSeparator))
// Check if the executable file exists in any of the directories in PATH
for _, dir := range paths {
fullPath := filepath.Join(dir, executable)
if _, err := os.Stat(fullPath); err == nil {
return true
}
}
return false
}
func (e Executor) SetValue(monitor, VCPCode, value string) error {
_, err := e.commandExecutor.Execute([]string{setValue, monitor, VCPCode, value})
return err
}
func (e Executor) SetValueIfNeeded(monitor, VCPCode, value string) error {
_, err := e.commandExecutor.Execute([]string{setValueIfNeeded, monitor, VCPCode, value})
return err
}
func (e Executor) GetValue(monitor, VCPCode string) (string, error) {
_, err := e.commandExecutor.Execute([]string{getValue, monitor, VCPCode})
if err != nil {
return "", err
}
return e.commandExecutor.Execute([]string{"echo", "$LASTEXITCODE"})
}
func (e Executor) TurnOff(monitor string) error {
_, err := e.commandExecutor.Execute([]string{turnOff, monitor})
return err
}
func (e Executor) TurnOn(monitor string) error {
_, err := e.commandExecutor.Execute([]string{turnOn, monitor})
return err
}
func (e Executor) SwitchOffOn(monitor string) error {
_, err := e.commandExecutor.Execute([]string{switchOffOn, monitor})
return err
}
func (e Executor) SwitchValue(monitor string, VCPCode string, values []string) error {
command := []string{switchValue, monitor, VCPCode}
command = append(command, values...)
_, err := e.commandExecutor.Execute(command)
return err
}
func (e Executor) SaveConfig(filename, monitor string) error {
_, err := e.commandExecutor.Execute([]string{saveConfig, filename, monitor})
return err
}
func (e Executor) LoadConfig(filename, monitor string) error {
_, err := e.commandExecutor.Execute([]string{loadConfig, filename, monitor})
return err
}
// Generalized Execute Function
func (e Executor) executeCommand(command []string, returnContents bool) (string, error) {
if returnContents {
// set filename to ""
command[1] = "\"\""
command = append(command, "|", "more")
}
return e.commandExecutor.Execute(command)
}
func (e Executor) SText(filename string, monitor string, returnContents bool) (string, error) {
return e.executeCommand([]string{sText, filename, monitor}, returnContents)
}
func (e Executor) STab(filename string, monitor string, returnContents bool) (string, error) {
return e.executeCommand([]string{sTab, filename, monitor}, returnContents)
}
func (e Executor) SComma(filename string, monitor string, returnContents bool) (string, error) {
return e.executeCommand([]string{sComma, filename, monitor}, returnContents)
}
func (e Executor) SHtml(filename string, monitor string, returnContents bool) (string, error) {
return e.executeCommand([]string{sHtml, filename, monitor}, returnContents)
}
func (e Executor) SVerHtml(filename string, monitor string, returnContents bool) (string, error) {
return e.executeCommand([]string{sVerHtml, filename, monitor}, returnContents)
}
func (e Executor) SXml(filename string, monitor string, returnContents bool) (string, error) {
return e.executeCommand([]string{sXml, filename, monitor}, returnContents)
}
func (e Executor) SJson(filename string, monitor string, returnContents bool) (string, error) {
return e.executeCommand([]string{sJson, filename, monitor}, returnContents)
}
func (e Executor) SMonitors(filename string, returnContents bool) (string, error) {
return e.executeCommand([]string{sMonitors, filename}, returnContents)
}