37 lines
761 B
Go
37 lines
761 B
Go
package installer
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
type installer struct {
|
|
workdir string
|
|
}
|
|
|
|
// ExecuteCommand implements syscheck.CommandExecutor interface
|
|
func (i *installer) ExecuteCommand(ctx context.Context, cmds ...string) (string, error) {
|
|
fcs := lo.Filter(cmds, func(item string, _ int) bool { return item != "" })
|
|
|
|
if len(fcs) == 0 {
|
|
return "", fmt.Errorf("empty commands")
|
|
}
|
|
|
|
cmd := exec.CommandContext(ctx, "sh", "-c", strings.Join(fcs, " "))
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return "", fmt.Errorf("command failed: %w, output: %s", err, string(output))
|
|
}
|
|
|
|
return string(output), nil
|
|
}
|
|
|
|
func NewInstaller(workdir string) *installer {
|
|
return &installer{workdir: workdir}
|
|
}
|