emissary/internal/agent/metadata/collector/shell/collector.go

47 lines
831 B
Go
Raw Permalink Normal View History

package shell
import (
"bytes"
"context"
"os"
"os/exec"
"strings"
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata"
"github.com/pkg/errors"
)
type Collector struct {
name string
command string
args []string
}
// Collect implements agent.MetadataCollector
func (c *Collector) Collect(ctx context.Context) (string, string, error) {
cmd := exec.CommandContext(ctx, c.command, c.args...)
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", "", errors.WithStack(err)
}
value := strings.TrimSpace(buf.String())
return c.name, value, nil
}
func NewCollector(name string, command string, args ...string) *Collector {
return &Collector{
name: name,
command: command,
args: args,
}
}
var _ metadata.Collector = &Collector{}