package main import ( "bufio" "errors" "io/ioutil" "os" "os/exec" "strings" "go.wit.com/log" "github.com/davecgh/go-spew/spew" ) func runSimpleCommand(s string) { cmd := strings.TrimSpace(s) // this is like 'chomp' in perl cmd = strings.TrimSuffix(cmd, "\n") // this is like 'chomp' in perl cmdArgs := strings.Fields(cmd) runLinuxCommand(cmdArgs) } func xterm(cmd string) { var tmp []string var argsXterm = []string{"nohup", "xterm", "-geometry", geom} tmp = append(argsXterm, "-hold", "-e", cmd) log.Println("xterm cmd=", cmd) go runCommand(tmp) } func runCommand(cmdArgs []string) { log.Println("runCommand() START", cmdArgs) process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) // process := exec.Command("xterm", "-e", "ping localhost") log.Println("runCommand() process.Start()") process.Start() log.Println("runCommand() process.Wait()") err := process.Wait() lookupError(err) log.Println("runCommand() NEED TO CHECK THE TIME HERE TO SEE IF THIS WORKED") log.Println("runCommand() OTHERWISE INFORM THE USER") log.Println("runCommand() END", cmdArgs) } func lookupError(err error) { var ( ee *exec.ExitError pe *os.PathError ) if errors.As(err, &ee) { log.Println("ran, but non-zero exit code =", ee.ExitCode()) // ran, but non-zero exit code } else if errors.As(err, &pe) { log.Printf("os.PathError = %v", pe) // "no such file ...", "permission denied" etc. } else if err != nil { log.Printf("something really bad happened general err = %v", err) // something really bad happened! if exitError, ok := err.(*exec.ExitError); ok { log.Printf("exitError.ExitCode() is %d\n", exitError.ExitCode()) } } else { log.Println("success! // ran without error (exit code zero)") } } func runLinuxCommand(cmdArgs []string) (string, error) { process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) process.Stdin = os.Stdin process.Stderr = os.Stderr stdOut, err := process.StdoutPipe() if err != nil { return "", err } if err := process.Start(); err != nil { return "", err } bytes, err := ioutil.ReadAll(stdOut) if err != nil { return "", err } err = process.Wait() lookupError(err) log.Println(string(bytes)) return string(bytes), err } func packrSaveFile(packrname string, filename string) error { log.Println("packrSaveFile() for", packrname) b, _ := packrBox.FindString(packrname) log.Println(packrname, "=\n\n", b) spew.Dump(b) f, err := os.Create(filename) if err != nil { return err } defer f.Close() w := bufio.NewWriter(f) n4, err := w.WriteString(b) log.Println("n4 =", n4) w.Flush() return nil } // run something and never return from it // TODO: pass STDOUT, STDERR, STDIN correctly // TODO: figure out how to nohup the process and exit func bash(cmdline string) error { log.Println("shell.Run() START " + cmdline) cmdArgs := strings.Fields(cmdline) process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) process.Stderr = os.Stderr process.Stdin = os.Stdin process.Stdout = os.Stdout process.Start() err := process.Wait() log.Println("shell.Exec() err =", err) return err // os.Exit(0) } func sudo(f func() error) error { uid := os.Getuid() e := "You must be root to do this." log.Println(e, "uid =", uid) if uid != 0 { log.Warn("Permissions Failed", e) return errors.New("not root") } err := f() return err }