fleet/orbit/pkg/table/firmwarepasswd/parser.go
Jahziel Villasana-Espinoza 87f4a28419
fix: use zerolog for orbit osquery table logging (#20028)
> Related issue: #19886 

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [x] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [x] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).
2024-06-27 13:26:20 -04:00

74 lines
1.5 KiB
Go

// based on github.com/kolide/launcher/pkg/osquery/tables
package firmwarepasswd
import (
"bufio"
"bytes"
"github.com/rs/zerolog"
)
type Matcher struct {
Match func(string) bool
KeyFunc func(string) (string, error)
ValFunc func(string) (string, error)
}
type OutputParser struct {
matchers []Matcher
logger zerolog.Logger
}
func NewParser(logger zerolog.Logger, matchers []Matcher) *OutputParser {
p := &OutputParser{
matchers: matchers,
logger: logger,
}
return p
}
// Parse looks at command output, line by line. It uses the defined Matchers to set any appropriate values
func (p *OutputParser) Parse(input *bytes.Buffer) []map[string]string {
var results []map[string]string
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
row := make(map[string]string)
// check each possible key match
for _, m := range p.matchers {
if m.Match(line) {
key, err := m.KeyFunc(line)
if err != nil {
p.logger.Debug().Err(err).Str("line", line).Msg("key match failed")
continue
}
val, err := m.ValFunc(line)
if err != nil {
p.logger.Debug().Err(err).Str("line", line).Msg("value match failed")
continue
}
row[key] = val
continue
}
}
if len(row) == 0 {
p.logger.Debug().Str("line", line).Msg("No matched keys")
continue
}
results = append(results, row)
}
if err := scanner.Err(); err != nil {
p.logger.Debug().Err(err).Msg("scanner error")
}
return results
}