Skip to content

Commit

Permalink
[DEVOPS-713] Add ignore-error to Command fact.
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufhm committed Feb 13, 2025
1 parent d7cd860 commit c37ff58
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
17 changes: 9 additions & 8 deletions docs/src/reference/collect/command.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ The `command` collect plugin executes a command and returns the output, error an

## Plugin fields

| Field | Description | Required | Default |
| ----- | ------------------------------------------- | :------: | :-----: |
| cmd | The main command to run. | Yes | "" |
| args | A list of arguments to pass to the command. | No | [] |
| Field | Description | Required | Default |
| ------------ | ----------------------------------------------------- | :------: | :-----: |
| cmd | The main command to run. | Yes | "" |
| args | A list of arguments to pass to the command. | No | [] |
| ignore-error | Whether to fail data collection if the command fails. | No | false |

<Content :page-key="$site.pages.find(p => p.path === '/reference/common/collect.html').key"/>

## Return format

A map with the following fields:

| Field | Description |
| ----- | ----------- |
| out | The command output. |
| err | The command error. |
| Field | Description |
| ----- | ---------------------- |
| out | The command output. |
| err | The command error. |
| code | The command exit code. |

## Example
Expand Down
5 changes: 3 additions & 2 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"io/fs"
"os/exec"
"strings"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -48,9 +49,9 @@ func GetMsgFromCommandError(err error) string {
if errors.As(err, &pathErr) {
errMsg = pathErr.Path + ": " + pathErr.Err.Error()
} else if errors.As(err, &exitErr) {
stderr := string(exitErr.Stderr)
stderr := strings.Trim(string(exitErr.Stderr), " \n")
if stderr != "" {
errMsg = string(exitErr.Error()) + ": " + string(exitErr.Stderr)
errMsg = string(exitErr.Error()) + ": " + stderr
} else {
errMsg = string(exitErr.Error())
}
Expand Down
24 changes: 18 additions & 6 deletions pkg/fact/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type Command struct {
data interface{}

// Plugin fields.
Cmd string `yaml:"cmd"`
Args []string `yaml:"args"`
Cmd string `yaml:"cmd"`
Args []string `yaml:"args"`
IgnoreError bool `yaml:"ignore-error"`
}

//go:generate go run ../../../cmd/gen.go fact-plugin --plugin=Command --package=command
Expand All @@ -54,11 +55,14 @@ func (p *Command) SupportedInputs() (fact.SupportLevel, []string) {
}

func (p *Command) Collect() {
log.WithFields(log.Fields{
contextLogger := log.WithFields(log.Fields{
"fact-plugin": p.PluginName(),
"fact": p.Name,
"cmd": p.Cmd,
"args": p.Args,
})

contextLogger.WithFields(log.Fields{
"cmd": p.Cmd,
"args": p.Args,
}).Debug("collecting data")

res := map[string]string{
Expand All @@ -68,7 +72,7 @@ func (p *Command) Collect() {
}

data, err := command.ShellCommander(p.Cmd, p.Args...).Output()
log.WithFields(log.Fields{
contextLogger.WithFields(log.Fields{
"stdout": string(data),
"stderr": fmt.Sprintf("%#v", err),
}).Debug("command output")
Expand All @@ -77,6 +81,14 @@ func (p *Command) Collect() {
if err != nil {
res["code"] = strconv.Itoa(command.GetExitCode(err))
res["stderr"] = command.GetMsgFromCommandError(err)

if !p.IgnoreError {
contextLogger.
WithField("stdout", res["stdout"]).
WithField("stderr", res["stderr"]).
WithError(err).Error("command failed")
p.errors = append(p.errors, err)
}
}

p.data = res
Expand Down
9 changes: 9 additions & 0 deletions pkg/fact/command/command_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command_test

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -48,6 +49,14 @@ func TestCommandCollect(t *testing.T) {
ExpectedData: map[string]string{
"code": "1", "stderr": "exec: no command", "stdout": "",
},
ExpectedErrors: []error{errors.New("exec: no command")},
},
{
Name: "emptyCommand/ignoreError",
Facter: &Command{Name: "TestCommand", IgnoreError: true},
ExpectedData: map[string]string{
"code": "1", "stderr": "exec: no command", "stdout": "",
},
},
{
Name: "echo",
Expand Down

0 comments on commit c37ff58

Please sign in to comment.