/
command.go
107 lines (90 loc) · 2.36 KB
/
command.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
// SPDX-License-Identifier: GPL-2.0-or-later
package clients
import (
"fmt"
"regexp"
log "github.com/sirupsen/logrus"
)
type Cmder interface {
GetCommand() string
ExtractResult(string) (map[string]string, error)
}
type Cmd struct {
key string
prefix string
suffix string
cmd string
outputProcessor func(string) (string, error)
regex *regexp.Regexp
fullCmd string
}
func NewCmd(key, cmd string) (*Cmd, error) {
cmdInstance := Cmd{
key: key,
cmd: cmd,
prefix: fmt.Sprintf("echo '<%s>'", key),
suffix: fmt.Sprintf("echo '</%s>'", key),
}
cmdInstance.fullCmd = fmt.Sprintf("%s;", cmdInstance.prefix)
cmdInstance.fullCmd += cmdInstance.cmd
if string(cmd[len(cmd)-1]) != ";" {
cmdInstance.fullCmd += ";"
}
cmdInstance.fullCmd += fmt.Sprintf("%s;", cmdInstance.suffix)
compiledRegex, err := regexp.Compile(`(?s)<` + key + `>\n` + `(.*)` + `\n</` + key + `>`)
if err != nil {
return nil, fmt.Errorf("failed to compile regex for key %s: %w", key, err)
}
cmdInstance.regex = compiledRegex
return &cmdInstance, nil
}
func (c *Cmd) SetOutputProcessor(f func(string) (string, error)) {
c.outputProcessor = f
}
func (c *Cmd) GetCommand() string {
return c.fullCmd
}
func (c *Cmd) ExtractResult(s string) (map[string]string, error) {
result := make(map[string]string)
log.Debugf("extract %s from %s", c.key, s)
match := c.regex.FindStringSubmatch(s)
log.Debugf("match %#v", match)
if len(match) > 0 {
value := match[1]
if c.outputProcessor != nil {
cleanValue, err := c.outputProcessor(match[1])
if err != nil {
return result, fmt.Errorf("failed to cleanup value %s of key %s", match[1], c.key)
}
value = cleanValue
}
log.Debugf("r %s", value)
result[c.key] = value
return result, nil
}
return result, fmt.Errorf("failed to find result for key: %s", c.key)
}
type CmdGroup struct {
cmds []*Cmd
}
func (cgrp *CmdGroup) AddCommand(c *Cmd) {
cgrp.cmds = append(cgrp.cmds, c)
}
func (cgrp *CmdGroup) GetCommand() string {
res := ""
for _, c := range cgrp.cmds {
res += c.GetCommand()
}
return res
}
func (cgrp *CmdGroup) ExtractResult(s string) (map[string]string, error) {
results := make(map[string]string)
for _, c := range cgrp.cmds {
res, err := c.ExtractResult(s)
if err != nil {
return results, err
}
results[c.key] = res[c.key]
}
return results, nil
}