47 lines
934 B
Go
47 lines
934 B
Go
package openwrt
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
)
|
|
|
|
type ShellFirmwareVersion struct {
|
|
command string
|
|
args []string
|
|
}
|
|
|
|
// FirmwareVersion implements FirmwareVersion
|
|
func (fv *ShellFirmwareVersion) FirmwareVersion(ctx context.Context) (string, error) {
|
|
command := exec.CommandContext(ctx, fv.command, fv.args...)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
command.Stdout = &buf
|
|
command.Stderr = os.Stderr
|
|
|
|
logger.Debug(ctx, "executing command", logger.F("command", fv.command), logger.F("args", fv.args))
|
|
|
|
if err := command.Run(); err != nil {
|
|
return "", errors.WithStack(err)
|
|
}
|
|
|
|
version := strings.TrimSpace(buf.String())
|
|
|
|
return version, nil
|
|
}
|
|
|
|
func NewShellFirmwareVersion(command string, args ...string) *ShellFirmwareVersion {
|
|
return &ShellFirmwareVersion{
|
|
command: command,
|
|
args: args,
|
|
}
|
|
}
|
|
|
|
var _ FirmwareVersion = &ShellFirmwareVersion{}
|