2021-11-02 23:44:28 -05:00
|
|
|
package main
|
|
|
|
|
2024-01-05 12:13:21 -06:00
|
|
|
import (
|
2024-01-24 18:24:38 -06:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
2024-01-05 12:13:21 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-01-24 18:24:38 -06:00
|
|
|
"strings"
|
2024-01-05 12:13:21 -06:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
2021-11-02 23:44:28 -05:00
|
|
|
|
|
|
|
func runSimpleCommand(s string) {
|
2024-01-24 18:24:38 -06:00
|
|
|
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)
|
2021-11-02 23:44:28 -05:00
|
|
|
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
|
|
|
|
}
|
2021-11-03 22:20:49 -05:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2024-01-24 18:24:38 -06:00
|
|
|
cmdArgs := strings.Fields(cmdline)
|
2021-11-03 22:20:49 -05:00
|
|
|
|
2024-01-24 18:24:38 -06:00
|
|
|
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
|
|
|
|
process.Stderr = os.Stderr
|
|
|
|
process.Stdin = os.Stdin
|
|
|
|
process.Stdout = os.Stdout
|
2021-11-03 22:20:49 -05:00
|
|
|
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()
|
2021-11-03 22:41:01 -05:00
|
|
|
e := "You must be root to do this."
|
|
|
|
log.Println(e, "uid =", uid)
|
2024-01-24 18:24:38 -06:00
|
|
|
if uid != 0 {
|
2024-01-05 12:13:21 -06:00
|
|
|
log.Warn("Permissions Failed", e)
|
2021-11-03 22:20:49 -05:00
|
|
|
return errors.New("not root")
|
|
|
|
}
|
|
|
|
err := f()
|
|
|
|
return err
|
|
|
|
}
|