-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.go
105 lines (91 loc) · 2.72 KB
/
fetcher.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
// SPDX-License-Identifier: GPL-2.0-or-later
package fetcher
import (
"bytes"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/nocturnalastro/collector-framework/pkg/clients"
)
type PostProcessFuncType func(map[string]string) (map[string]any, error)
type Fetcher struct {
cmdGrp *clients.CmdGroup
postProcessor PostProcessFuncType
}
func NewFetcher() *Fetcher {
return &Fetcher{
cmdGrp: &clients.CmdGroup{},
}
}
func TrimSpace(s string) (string, error) {
return strings.TrimSpace(s), nil
}
func (inst *Fetcher) SetPostProcessor(ppFunc PostProcessFuncType) {
inst.postProcessor = ppFunc
}
// AddNewCommand creates a new command from a string
// then adds it to the fetcher
func (inst *Fetcher) AddNewCommand(key, cmd string, trim bool) error {
cmdInst, err := clients.NewCmd(key, cmd)
if err != nil {
return fmt.Errorf("add fetcher cmd failed %w", err)
}
if trim {
cmdInst.SetOutputProcessor(TrimSpace)
}
inst.cmdGrp.AddCommand(cmdInst)
return nil
}
// AddCommand takes a command instance and adds it the fetcher
func (inst *Fetcher) AddCommand(cmdInst *clients.Cmd) {
inst.cmdGrp.AddCommand(cmdInst)
}
// Fetch executes the commands on the container passed as the ctx and
// use the results to populate pack
func (inst *Fetcher) Fetch(ctx clients.ExecContext, pack any) error {
runResult, err := runCommands(ctx, inst.cmdGrp)
if err != nil {
return err
}
result := make(map[string]any)
for key, value := range runResult {
result[key] = value
}
if inst.postProcessor != nil {
updatedResults, ppErr := inst.postProcessor(runResult)
if ppErr != nil {
return fmt.Errorf("feching failed post process the data %w", ppErr)
}
for key, value := range updatedResults {
result[key] = value
}
}
err = unmarshal(result, pack)
if err != nil {
return fmt.Errorf("feching failed to unpack data %w", err)
}
return nil
}
// runCommands executes the commands on the container passed as the ctx
// and extracts the results from the stdout
func runCommands(ctx clients.ExecContext, cmdGrp clients.Cmder) (result map[string]string, err error) { //nolint:lll // allow slightly long function definition
cmd := cmdGrp.GetCommand()
command := []string{"/usr/bin/sh"}
var buffIn bytes.Buffer
buffIn.WriteString(cmd)
stdout, _, err := ctx.ExecCommandStdIn(command, buffIn)
if err != nil {
log.Debugf(
"command in container failed unexpectedly:\n\tcontext: %v\n\tcommand: %v\n\terror: %v",
ctx, command, err,
)
return result, fmt.Errorf("runCommands failed %w", err)
}
result, err = cmdGrp.ExtractResult(stdout)
if err != nil {
log.Debugf("extraction failed %s", err.Error())
log.Debugf("output was %s", stdout)
return result, fmt.Errorf("runCommands failed %w", err)
}
return result, nil
}