control agent can now uboot commands
This commit is contained in:
89
clitools/pkg/cmd/internal/fwprintenv.go
Normal file
89
clitools/pkg/cmd/internal/fwprintenv.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
"example.com/monok8s/pkg/system"
|
||||
)
|
||||
|
||||
func newInternalFWPrintEnvCmd() *cobra.Command {
|
||||
var key string
|
||||
var configPath string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "fw-printenv",
|
||||
Short: "Run fw_printenv",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
key = strings.TrimSpace(key)
|
||||
configPath = strings.TrimSpace(configPath)
|
||||
|
||||
if configPath == "" {
|
||||
configPath = defaultFWEnvConfigPath
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); err != nil {
|
||||
return fmt.Errorf("stat fw env config %q: %w", configPath, err)
|
||||
}
|
||||
|
||||
runner := system.NewRunner(system.RunnerConfig{
|
||||
DefaultTimeout: 15 * time.Second,
|
||||
StreamOutput: false,
|
||||
Logger: &system.StdLogger{},
|
||||
})
|
||||
|
||||
runArgs := []string{"-c", configPath}
|
||||
if key != "" {
|
||||
runArgs = append(runArgs, key)
|
||||
}
|
||||
|
||||
res, err := runner.RunWithOptions(
|
||||
ctx,
|
||||
"/fw_printenv",
|
||||
runArgs,
|
||||
system.RunOptions{
|
||||
Quiet: true,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if res != nil {
|
||||
klog.ErrorS(err, "fw_printenv failed",
|
||||
"key", key,
|
||||
"stdout", strings.TrimSpace(res.Stdout),
|
||||
"stderr", strings.TrimSpace(res.Stderr),
|
||||
)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
stdout := strings.TrimSpace(res.Stdout)
|
||||
stderr := strings.TrimSpace(res.Stderr)
|
||||
|
||||
if stdout != "" {
|
||||
fmt.Println(stdout)
|
||||
}
|
||||
if stderr != "" {
|
||||
klog.InfoS("fw_printenv stderr", "output", stderr)
|
||||
}
|
||||
|
||||
klog.InfoS("fw_printenv succeeded",
|
||||
"key", key,
|
||||
"configPath", configPath,
|
||||
)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVar(&key, "key", "", "U-Boot environment variable name to print")
|
||||
cmd.Flags().StringVar(&configPath, "config", defaultFWEnvConfigPath, "Path to fw_env.config")
|
||||
|
||||
return cmd
|
||||
}
|
||||
Reference in New Issue
Block a user