func (c *uBootEnvCommand) command(params ...string) (uBootVars, error) { cmd := runner.run(c.EnvCmd, params...) cmdReader, err := cmd.StdoutPipe() if err != nil { log.Errorln("Error creating StdoutPipe:", err) return nil, err } scanner := bufio.NewScanner(cmdReader) err = cmd.Start() if err != nil { log.Errorln("There was an error getting or setting U-Boot env") return nil, err } var env_variables = make(uBootVars) for scanner.Scan() { log.Debugln("Have U-Boot variable:", scanner.Text()) splited_line := strings.Split(scanner.Text(), "=") //we are having empty line (usually at the end of output) if scanner.Text() == "" { continue } //we have some malformed data or Warning/Error if len(splited_line) != 2 { log.Errorln("U-Boot variable malformed or error occured") return nil, errors.New("Invalid U-Boot variable or error: " + scanner.Text()) } env_variables[splited_line[0]] = splited_line[1] } err = cmd.Wait() if err != nil { log.Errorln("U-Boot env command returned non zero status") return nil, err } if len(env_variables) > 0 { log.Debugln("List of U-Boot variables:", env_variables) } return env_variables, err }
func setBootEnv(var_name string, value string) error { set_env := uBootEnvCommand{"fw_setenv"} if _, err := set_env.command(var_name, value); err != nil { log.Errorln("Error setting U-Boot variable:", err) return err } return nil }
func getOrSetEnvironmentVariable(cmd *exec.Cmd) (BootVars, error) { cmdReader, err := cmd.StdoutPipe() if err != nil { log.Errorln("Error creating StdoutPipe: ", err) return nil, err } scanner := bufio.NewScanner(cmdReader) err = cmd.Start() if err != nil { return nil, err } var env_variables = make(BootVars) for scanner.Scan() { log.Debug("Have U-Boot variable: ", scanner.Text()) splited_line := strings.Split(scanner.Text(), "=") //we are having empty line (usually at the end of output) if scanner.Text() == "" { continue } //we have some malformed data or Warning/Error if len(splited_line) != 2 { log.Error("U-Boot variable malformed or error occured") return nil, errors.New("Invalid U-Boot variable or error: " + scanner.Text()) } env_variables[splited_line[0]] = splited_line[1] } err = cmd.Wait() if err != nil { return nil, err } if len(env_variables) > 0 { log.Debug("List of U-Boot variables:", env_variables) } return env_variables, err }
func TestLoggingOptions(t *testing.T) { err := doMain([]string{"-commit", "-log-level", "crap"}) assert.Error(t, err, "'crap' log level should have given error") // Should have a reference to log level. assert.Contains(t, err.Error(), "Level") err = doMain([]string{"-info", "-log-level", "debug"}) assert.Error(t, err, "Incompatible log levels should have given error") assert.Contains(t, err.Error(), errMsgIncompatibleLogOptions.Error()) var buf bytes.Buffer oldOutput := log.Log.Out log.SetOutput(&buf) defer log.SetOutput(oldOutput) // Ignore errors for now, we just want to know if the logging level was // applied. log.SetLevel(log.DebugLevel) doMain([]string{"-log-level", "panic"}) log.Debugln("Should not show") doMain([]string{"-debug"}) log.Debugln("Should show") doMain([]string{"-info"}) log.Debugln("Should also not show") logdata := buf.String() assert.Contains(t, logdata, "Should show") assert.NotContains(t, logdata, "Should not show") assert.NotContains(t, logdata, "Should also not show") doMain([]string{"-log-modules", "main_test,MyModule"}) log.Errorln("Module filter should show main_test") log.PushModule("MyModule") log.Errorln("Module filter should show MyModule") log.PushModule("MyOtherModule") log.Errorln("Module filter should not show MyOtherModule") log.PopModule() log.PopModule() assert.True(t, strings.Index(buf.String(), "Module filter should show main_test") >= 0) assert.True(t, strings.Index(buf.String(), "Module filter should show MyModule") >= 0) assert.True(t, strings.Index(buf.String(), "Module filter should not show MyOtherModule") < 0) defer os.Remove("test.log") doMain([]string{"-log-file", "test.log"}) log.Errorln("Should be in log file") fd, err := os.Open("test.log") assert.NoError(t, err) var bytebuf [4096]byte n, err := fd.Read(bytebuf[:]) assert.True(t, err == nil) assert.True(t, strings.Index(string(bytebuf[0:n]), "Should be in log file") >= 0) err = doMain([]string{"-no-syslog"}) // Just check that the flag can be specified. assert.True(t, err != nil) assert.True(t, strings.Index(err.Error(), "syslog") < 0) }
func main() { if err := doMain(os.Args[1:]); err != nil && err != flag.ErrHelp { log.Errorln(err.Error()) os.Exit(1) } }