start deprecating and modernizing this code
This commit is contained in:
parent
98730aed8a
commit
c9df5a7ace
1
Makefile
1
Makefile
|
@ -1,4 +1,5 @@
|
||||||
all:
|
all:
|
||||||
|
GO111MODULE=off go build
|
||||||
|
|
||||||
goimports:
|
goimports:
|
||||||
goimports -w *.go
|
goimports -w *.go
|
||||||
|
|
|
@ -3,8 +3,6 @@ package shell
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"go.wit.com/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// openBrowser opens the specified URL in the default browser of the user.
|
// openBrowser opens the specified URL in the default browser of the user.
|
||||||
|
@ -24,19 +22,3 @@ func OpenBrowser(url string) error {
|
||||||
args = append(args, url)
|
args = append(args, url)
|
||||||
return exec.Command(cmd, args...).Start()
|
return exec.Command(cmd, args...).Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Xterm(cmd string) {
|
|
||||||
var tmp []string
|
|
||||||
var argsXterm = []string{"nohup", "xterm", "-geometry", "120x40"}
|
|
||||||
/*
|
|
||||||
if xtermHold.Checked() {
|
|
||||||
log.Println("hold = true")
|
|
||||||
argsXterm = append(argsXterm, "-hold")
|
|
||||||
} else {
|
|
||||||
log.Println("hold = false")
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
tmp = append(argsXterm, "-e", cmd)
|
|
||||||
log.Info("xterm cmd=", tmp)
|
|
||||||
go Run(tmp)
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
package shell
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// get your parent PID
|
||||||
|
func GetPPID(pid int) (int, error) {
|
||||||
|
data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Fields(string(data))
|
||||||
|
if len(parts) < 4 {
|
||||||
|
return 0, fmt.Errorf("unexpected format of /proc/%d/stat", pid)
|
||||||
|
}
|
||||||
|
|
||||||
|
ppid, err := strconv.Atoi(parts[3])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ppid, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// get comm from proc
|
||||||
|
func GetComm(pid int) (string, error) {
|
||||||
|
data, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/comm", pid))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(string(data)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func main() {
|
||||||
|
pid := os.Getpid()
|
||||||
|
ppid, err := getPPID(pid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting PPID:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
comm, err := getComm(ppid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting comm:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(comm)
|
||||||
|
}
|
||||||
|
*/
|
58
run.go
58
run.go
|
@ -30,7 +30,7 @@ func RunCapture(cmdline string) string {
|
||||||
return Chomp(test.Buffer)
|
return Chomp(test.Buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunWait(args []string) *Shell {
|
func RunWait(args []string) *OldShell {
|
||||||
test := New()
|
test := New()
|
||||||
cmdline := strings.Join(args, " ")
|
cmdline := strings.Join(args, " ")
|
||||||
test.Exec(cmdline)
|
test.Exec(cmdline)
|
||||||
|
@ -86,7 +86,7 @@ func RunPath(path string, args []string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *Shell) Run(cmdline string) string {
|
func (cmd *OldShell) Run(cmdline string) string {
|
||||||
cmd.InitProcess(cmdline)
|
cmd.InitProcess(cmdline)
|
||||||
if cmd.Error != nil {
|
if cmd.Error != nil {
|
||||||
return ""
|
return ""
|
||||||
|
@ -95,7 +95,7 @@ func (cmd *Shell) Run(cmdline string) string {
|
||||||
return Chomp(cmd.Buffer)
|
return Chomp(cmd.Buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *Shell) InitProcess(cmdline string) {
|
func (cmd *OldShell) InitProcess(cmdline string) {
|
||||||
log.Log(RUN, "shell.InitProcess() START "+cmdline)
|
log.Log(RUN, "shell.InitProcess() START "+cmdline)
|
||||||
|
|
||||||
cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl
|
cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl
|
||||||
|
@ -118,7 +118,7 @@ func (cmd *Shell) InitProcess(cmdline string) {
|
||||||
cmd.Process = exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
|
cmd.Process = exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *Shell) FileCreate(out string) {
|
func (cmd *OldShell) FileCreate(out string) {
|
||||||
var newfile File
|
var newfile File
|
||||||
|
|
||||||
var iof io.ReadCloser
|
var iof io.ReadCloser
|
||||||
|
@ -141,7 +141,7 @@ func (cmd *Shell) FileCreate(out string) {
|
||||||
|
|
||||||
// NOTE: this might cause problems:
|
// NOTE: this might cause problems:
|
||||||
// always remove the newlines at the end ?
|
// always remove the newlines at the end ?
|
||||||
func (cmd *Shell) Exec(cmdline string) {
|
func (cmd *OldShell) Exec(cmdline string) {
|
||||||
log.Log(RUN, "shell.Run() START "+cmdline)
|
log.Log(RUN, "shell.Run() START "+cmdline)
|
||||||
|
|
||||||
cmd.InitProcess(cmdline)
|
cmd.InitProcess(cmdline)
|
||||||
|
@ -179,7 +179,7 @@ func (cmd *Shell) Exec(cmdline string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// nonblocking read until file errors
|
// nonblocking read until file errors
|
||||||
func (cmd *Shell) Capture(f *File) {
|
func (cmd *OldShell) Capture(f *File) {
|
||||||
log.Log(RUN, "nbrREADER() START")
|
log.Log(RUN, "nbrREADER() START")
|
||||||
|
|
||||||
if cmd.Buffer == nil {
|
if cmd.Buffer == nil {
|
||||||
|
@ -211,7 +211,7 @@ func (cmd *Shell) Capture(f *File) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns true if filehandle buffer is empty
|
// returns true if filehandle buffer is empty
|
||||||
func (cmd *Shell) ReadToBuffer(f *File) bool {
|
func (cmd *OldShell) ReadToBuffer(f *File) bool {
|
||||||
log.Log(RUN, "ReadToBuffer() START")
|
log.Log(RUN, "ReadToBuffer() START")
|
||||||
nbr := f.Fnbreader
|
nbr := f.Fnbreader
|
||||||
oneByte := make([]byte, 1024)
|
oneByte := make([]byte, 1024)
|
||||||
|
@ -282,7 +282,6 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) {
|
||||||
log.Warn("output was", string(output))
|
log.Warn("output was", string(output))
|
||||||
log.Warn("cmd exited with error", err)
|
log.Warn("cmd exited with error", err)
|
||||||
// panic("fucknuts")
|
// panic("fucknuts")
|
||||||
return err, false, string(output)
|
|
||||||
|
|
||||||
// The command failed (non-zero exit status)
|
// The command failed (non-zero exit status)
|
||||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||||
|
@ -293,6 +292,7 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) {
|
||||||
} else {
|
} else {
|
||||||
log.Warn("cmd.Run() failed with %s\n", err)
|
log.Warn("cmd.Run() failed with %s\n", err)
|
||||||
}
|
}
|
||||||
|
return err, false, string(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := string(output)
|
tmp := string(output)
|
||||||
|
@ -301,3 +301,45 @@ func RunCmd(workingpath string, parts []string) (error, bool, string) {
|
||||||
// Print the output
|
// Print the output
|
||||||
return nil, true, tmp
|
return nil, true, tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send the path and the command
|
||||||
|
func RunCmdRun(workingpath string, parts []string) error {
|
||||||
|
if len(parts) == 0 {
|
||||||
|
log.Warn("command line was empty")
|
||||||
|
return errors.New("empty")
|
||||||
|
}
|
||||||
|
if parts[0] == "" {
|
||||||
|
log.Warn("command line was empty")
|
||||||
|
return errors.New("empty")
|
||||||
|
}
|
||||||
|
thing := parts[0]
|
||||||
|
parts = parts[1:]
|
||||||
|
log.Log(INFO, "working path =", workingpath, "thing =", thing, "cmdline =", parts)
|
||||||
|
|
||||||
|
// Create the command
|
||||||
|
cmd := exec.Command(thing, parts...)
|
||||||
|
|
||||||
|
// Set the working directory
|
||||||
|
cmd.Dir = workingpath
|
||||||
|
|
||||||
|
// Execute the command
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("ERROR working path =", workingpath, "thing =", thing, "cmdline =", parts)
|
||||||
|
log.Error(err)
|
||||||
|
log.Warn("cmd exited with error", err)
|
||||||
|
// panic("fucknuts")
|
||||||
|
|
||||||
|
// The command failed (non-zero exit status)
|
||||||
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||||
|
// Assert that it is an exec.ExitError and get the exit code
|
||||||
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
||||||
|
log.Warn("Exit Status: %d\n", status.ExitStatus())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Warn("cmd.Run() failed with %s\n", err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -27,7 +27,8 @@ type File struct {
|
||||||
Fnbreader *nbreader.NBReader // := nbreader.NewNBReader(readOUT, 1024)
|
Fnbreader *nbreader.NBReader // := nbreader.NewNBReader(readOUT, 1024)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Shell struct {
|
// early code playground
|
||||||
|
type OldShell struct {
|
||||||
Cmdline string
|
Cmdline string
|
||||||
Process *exec.Cmd
|
Process *exec.Cmd
|
||||||
Done bool
|
Done bool
|
||||||
|
@ -45,9 +46,9 @@ type Shell struct {
|
||||||
Stderr *File
|
Stderr *File
|
||||||
}
|
}
|
||||||
|
|
||||||
// default values for Shell
|
// default values for OldShell
|
||||||
func New() *Shell {
|
func New() *OldShell {
|
||||||
var tmp Shell
|
var tmp OldShell
|
||||||
|
|
||||||
tmp.Done = false
|
tmp.Done = false
|
||||||
tmp.Fail = false
|
tmp.Fail = false
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
package shell
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"go.wit.com/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func scanToParent(pid int) (bool, string) {
|
||||||
|
ppid, err := GetPPID(pid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting PPID:", err)
|
||||||
|
return true, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
comm, err := GetComm(ppid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting comm:", err)
|
||||||
|
return true, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("scanToParent() found:", comm)
|
||||||
|
|
||||||
|
// if this is an xterm
|
||||||
|
switch comm {
|
||||||
|
case "xterm":
|
||||||
|
return true, "xterm"
|
||||||
|
case "mate-terminal":
|
||||||
|
return true, "mate-terminal"
|
||||||
|
case "bash":
|
||||||
|
// keep digging for the parent xterm
|
||||||
|
return scanToParent(ppid)
|
||||||
|
case "make":
|
||||||
|
// keep digging for the parent xterm
|
||||||
|
return scanToParent(ppid)
|
||||||
|
default:
|
||||||
|
return false, comm
|
||||||
|
}
|
||||||
|
if comm == "bash" {
|
||||||
|
}
|
||||||
|
|
||||||
|
ok, better := scanToParent(ppid)
|
||||||
|
if ok {
|
||||||
|
return ok, better
|
||||||
|
}
|
||||||
|
if better == "" {
|
||||||
|
// if the parent was blank, at least return the something that we have
|
||||||
|
// since something is better than nothing
|
||||||
|
return false, comm
|
||||||
|
}
|
||||||
|
return ok, better
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns a string of the xterm the user is using
|
||||||
|
// for example, "xterm" or "mate-terminal"
|
||||||
|
func Terminal() string {
|
||||||
|
pid := os.Getpid()
|
||||||
|
_, comm := scanToParent(pid)
|
||||||
|
/*
|
||||||
|
ppid, err := GetPPID(pid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting PPID:", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
comm, err := GetComm(ppid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting comm:", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
return comm
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns a string of the shell the user is using
|
||||||
|
func Shell() string {
|
||||||
|
envsh := os.Getenv("SHELL")
|
||||||
|
switch envsh {
|
||||||
|
case "/bin/bash":
|
||||||
|
return "bash"
|
||||||
|
default:
|
||||||
|
return envsh
|
||||||
|
}
|
||||||
|
return envsh
|
||||||
|
}
|
||||||
|
|
||||||
|
func getXtermCmd(cmd []string) []string {
|
||||||
|
var term string
|
||||||
|
// doesn't work yet
|
||||||
|
// term = Terminal()
|
||||||
|
term = "xterm"
|
||||||
|
switch term {
|
||||||
|
case "mate-terminal":
|
||||||
|
newcmd := []string{"mate-terminal", "-e"}
|
||||||
|
newcmd = append(newcmd, cmd...)
|
||||||
|
return newcmd
|
||||||
|
default:
|
||||||
|
// unknown terminal. use xterm
|
||||||
|
newcmd := []string{"xterm", "-geometry", "140x32", "-e", "bash", "-c"}
|
||||||
|
// fix/shell escape sequence this for quote chars, etc
|
||||||
|
// tmp := "\"" + strings.Join(cmd, " ") + ";bash\""
|
||||||
|
newcmd = append(newcmd, cmd...)
|
||||||
|
// newcmd = append(newcmd, cmd...)
|
||||||
|
return newcmd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// spawns an xterm with something you can run at a command line
|
||||||
|
func XtermCmd(path string, cmd []string) {
|
||||||
|
go XtermCmdWait(path, cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runs an xterm
|
||||||
|
// waits until xterm exits
|
||||||
|
func XtermCmdWait(path string, cmd []string) {
|
||||||
|
var argsXterm = getXtermCmd(cmd)
|
||||||
|
|
||||||
|
log.Info("XtermCmd() path =", path, "cmd =", argsXterm)
|
||||||
|
|
||||||
|
// keeps git diff from exiting on small diffs
|
||||||
|
os.Setenv("LESS", "-+F -+X -R")
|
||||||
|
|
||||||
|
RunCmdRun(path, argsXterm)
|
||||||
|
}
|
||||||
|
|
||||||
|
// spawns an xterm with something you can run at a command line
|
||||||
|
// then executes bash
|
||||||
|
func XtermCmdBash(path string, cmd []string) {
|
||||||
|
var tmp []string
|
||||||
|
var argsXterm = getXtermCmd(cmd)
|
||||||
|
bash := "\"-c '"
|
||||||
|
bash += strings.Join(cmd, " ")
|
||||||
|
bash += "'; bash\""
|
||||||
|
tmp = append(argsXterm, "bash", bash)
|
||||||
|
log.Info("XtermCmd() path =", path, "cmd =", tmp)
|
||||||
|
go RunCmd(path, tmp)
|
||||||
|
}
|
Loading…
Reference in New Issue