From 0c1ffa1cd96b3a80d11767419863c43e4cc75ada Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 17:47:16 -0400 Subject: [PATCH 01/36] add rcfile flag --- linux/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/run.go b/linux/run.go index 8009d16..e2bb35d 100644 --- a/linux/run.go +++ b/linux/run.go @@ -29,7 +29,7 @@ func NewCommand(options CommandOptions) (*LinuxCommand, error) { func (cmd *LinuxCommand) Run() error { - command := exec.Command(cmd.Options.Shell, "-c", cmd.Options.Command) + command := exec.Command(cmd.Options.Shell, "--rcfile", "testrc", "-c", cmd.Options.Command) command.Dir = cmd.Options.Cwd command.Args = append(command.Args, cmd.Options.Args...) -- 2.45.2 From 76f276f69e425c2cc9b3045f741b6a0c1e52264e Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 18:08:45 -0400 Subject: [PATCH 02/36] add sources option --- linux/interface.go | 2 +- linux/run.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/linux/interface.go b/linux/interface.go index 384ef94..ed25b04 100644 --- a/linux/interface.go +++ b/linux/interface.go @@ -10,7 +10,7 @@ type LinuxCommand struct { type CommandOptions struct { Env map[string]string - Sources map[string]string + Sources []string Command string Args []string Cwd string diff --git a/linux/run.go b/linux/run.go index e2bb35d..7c612d7 100644 --- a/linux/run.go +++ b/linux/run.go @@ -29,7 +29,12 @@ func NewCommand(options CommandOptions) (*LinuxCommand, error) { func (cmd *LinuxCommand) Run() error { - command := exec.Command(cmd.Options.Shell, "--rcfile", "testrc", "-c", cmd.Options.Command) + var sourceCommand string + for _, value := range cmd.Options.Sources { + sourceCommand += fmt.Sprintf("source %s && ", value) + } + + command := exec.Command(cmd.Options.Shell, "-c", sourceCommand+cmd.Options.Command) command.Dir = cmd.Options.Cwd command.Args = append(command.Args, cmd.Options.Args...) -- 2.45.2 From d127424b9f06622520a29701e488e35943c66b47 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 19:40:33 -0400 Subject: [PATCH 03/36] add custom exit code errors --- linux/interface.go | 13 ++-- linux/run.go | 6 +- main.go | 177 --------------------------------------------- 3 files changed, 11 insertions(+), 185 deletions(-) delete mode 100644 main.go diff --git a/linux/interface.go b/linux/interface.go index ed25b04..d958111 100644 --- a/linux/interface.go +++ b/linux/interface.go @@ -9,12 +9,13 @@ type LinuxCommand struct { } type CommandOptions struct { - Env map[string]string - Sources []string - Command string - Args []string - Cwd string - Shell string + Env map[string]string + Sources []string + Command string + Args []string + CustomErrors map[int8]error + Cwd string + Shell string } // Errors diff --git a/linux/run.go b/linux/run.go index 7c612d7..b02cdef 100644 --- a/linux/run.go +++ b/linux/run.go @@ -55,9 +55,10 @@ func (cmd *LinuxCommand) Run() error { if err := command.Start(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { - fmt.Println(exitErr.ExitCode()) if exitErr.ExitCode() == 127 { return ErrCommandNotFound + } else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok { + return cmd.Options.CustomErrors[int8(exitErr.ExitCode())] } else { return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err) } @@ -67,9 +68,10 @@ func (cmd *LinuxCommand) Run() error { if err := command.Wait(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { - fmt.Println(exitErr.ExitCode()) if exitErr.ExitCode() == 127 { return ErrCommandNotFound + } else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok { + return cmd.Options.CustomErrors[int8(exitErr.ExitCode())] } else { return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err) } diff --git a/main.go b/main.go deleted file mode 100644 index e5b9d97..0000000 --- a/main.go +++ /dev/null @@ -1,177 +0,0 @@ -package main - -import ( - "bytes" - "errors" - "fmt" - "io/fs" - "os" - "os/exec" - "strings" - - "golang.org/x/sys/unix" -) - -var LinuxType string - -var ErrCommandDoesNotExist = errors.New("command Doesn't Exist") -var ErrSourceDoesNotExist = errors.New("source Doesn't Exist") - -func RunCommand(command string, args ...string) error { - - if !DoesCommandExist(command) { - return ErrCommandDoesNotExist - } - - var stderr bytes.Buffer - - var mainCommand string - - mainCommand = "/bin/bash" - - bashCommand := exec.Command(mainCommand, "-c", command) - bashCommand.Args = append(bashCommand.Args, args...) - bashCommand.Stderr = &stderr - - if err := bashCommand.Start(); err != nil { - return errors.New(stderr.String() + " " + err.Error()) - } - - if err := bashCommand.Wait(); err != nil { - return errors.New(stderr.String() + " " + err.Error()) - } - - return nil - -} - -func RunCommandWithSource(source string, command string, args ...string) error { - - if !DoesSourceExist(source) { - return ErrSourceDoesNotExist - } - - if !DoesCommandExistWithSource(source, command) { - return ErrCommandDoesNotExist - } - - var stderr bytes.Buffer - - var mainCommand string - - mainCommand = "/bin/bash" - - bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("source %s; %s", source, command)) - bashCommand.Args = append(bashCommand.Args, args...) - bashCommand.Stderr = &stderr - - if err := bashCommand.Start(); err != nil { - return errors.New(stderr.String()) - } - - if err := bashCommand.Wait(); err != nil { - return errors.New(stderr.String()) - } - - return nil - -} - -func DoesCommandExist(command string) bool { - - var stderr bytes.Buffer - - var mainCommand string - - mainCommand = "/bin/bash" - - bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("command -v %s", command)) - bashCommand.Stderr = &stderr - - if err := bashCommand.Start(); err != nil { - if strings.Contains(err.Error(), "1") { - return false - } - } - - if err := bashCommand.Wait(); err != nil { - if strings.Contains(err.Error(), "1") { - return false - } - } - - return true - -} - -func DoesSourceExist(source string) bool { - - var stderr bytes.Buffer - - var mainCommand string - - mainCommand = "/bin/bash" - - bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("source %s", source)) - bashCommand.Stderr = &stderr - - if err := bashCommand.Start(); err != nil { - if strings.Contains(err.Error(), "1") { - return false - } - } - - if err := bashCommand.Wait(); err != nil { - if strings.Contains(err.Error(), "1") { - return false - } - } - - return true - -} - -func DoesCommandExistWithSource(source string, command string) bool { - - if sourceCheck := DoesSourceExist(source); !sourceCheck { - return false - } - - var stderr bytes.Buffer - - var mainCommand string - - mainCommand = "/bin/bash" - - bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("source %s && command -v %s", source, command)) - bashCommand.Stderr = &stderr - - if err := bashCommand.Start(); err != nil { - if strings.Contains(err.Error(), "1") { - return false - } - } - - if err := bashCommand.Wait(); err != nil { - if strings.Contains(err.Error(), "1") { - return false - } - } - - return true - -} - -func CanExecute(script string) bool { - - if _, err := os.Stat(script); errors.Is(err, fs.ErrNotExist) { - return false - } - - if err := unix.Access(script, unix.X_OK); err != nil { - return err == unix.EACCES - } - - return true - -} -- 2.45.2 From 2cc3517f37cc901954329e85202cf3ab9311e3b5 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 20:37:59 -0400 Subject: [PATCH 04/36] fixed permission checking on commands, added "which" command. --- cmd/which.go | 30 ++++++++++++++++++++++++++++++ linux/utils.go | 16 ++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 cmd/which.go diff --git a/cmd/which.go b/cmd/which.go new file mode 100644 index 0000000..c7acd6a --- /dev/null +++ b/cmd/which.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "errors" + "fmt" + "os/exec" + "strings" +) + +var ErrNotFound = errors.New("which: command not found") + +func Which(cmd string) (string, error) { + + command := exec.Command("which", cmd) + + outputBytes, err := command.Output() + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + if exitErr.ExitCode() == 1 { + return "", ErrNotFound + } else { + return "", fmt.Errorf("command error: %w", err) + } + } + } + + return strings.Trim(string(outputBytes), "\n"), nil + +} diff --git a/linux/utils.go b/linux/utils.go index ba6a1d5..7146b29 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -3,6 +3,7 @@ package linux import ( "errors" "fmt" + cmd2 "git.shadowhosting.xyz/Eggactyl/shell/cmd" "golang.org/x/sys/unix" "io/fs" "os" @@ -11,8 +12,19 @@ import ( func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { - if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) { - return false, err + whichOut, err := cmd2.Which(command) + if err != nil { + if errors.Is(err, cmd2.ErrNotFound) { + if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) { + return false, err + } + } else { + return false, err + } + } + + if len(whichOut) > 0 { + command = whichOut } if err := unix.Access(command, unix.X_OK); err != nil { -- 2.45.2 From ef7c4f26f7b19b9471794d4a7b136836f1036cef Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 21:57:46 -0400 Subject: [PATCH 05/36] added 'command' shell command --- cmd/command.go | 28 ++++++++++++++++++++++++++++ tes.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 cmd/command.go create mode 100644 tes.go diff --git a/cmd/command.go b/cmd/command.go new file mode 100644 index 0000000..24c2920 --- /dev/null +++ b/cmd/command.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "errors" + "fmt" + "os/exec" + "strings" +) + +func Command(cmd string) (string, error) { + + command := exec.Command("command", "-v", cmd) + + outputBytes, err := command.Output() + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + if exitErr.ExitCode() == 1 { + return "", ErrNotFound + } else { + return "", fmt.Errorf("command error: %w", err) + } + } + } + + return strings.Trim(string(outputBytes), "\n"), nil + +} diff --git a/tes.go b/tes.go new file mode 100644 index 0000000..7f0fcf4 --- /dev/null +++ b/tes.go @@ -0,0 +1,30 @@ +package main + +import ( + "errors" + "git.shadowhosting.xyz/Eggactyl/shell/linux" + "log" +) + +func main() { + + cmd, err := linux.NewCommand(linux.CommandOptions{ + Command: "./package", + //Sources: []string{"./nvm.sh"}, + Args: []string{"install"}, + CustomErrors: map[int8]error{ + 1: errors.New("failed to download package"), + }, + //Cwd: "/home//", + //Shell: "/bin/bash", + }) + + if err != nil { + log.Fatal(err) + } + + if err := cmd.Run(); err != nil { + log.Fatal(err) + } + +} -- 2.45.2 From c7b6273166511066a5820424490f36625c046efe Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 21:58:04 -0400 Subject: [PATCH 06/36] remove garbage file --- tes.go | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 tes.go diff --git a/tes.go b/tes.go deleted file mode 100644 index 7f0fcf4..0000000 --- a/tes.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "errors" - "git.shadowhosting.xyz/Eggactyl/shell/linux" - "log" -) - -func main() { - - cmd, err := linux.NewCommand(linux.CommandOptions{ - Command: "./package", - //Sources: []string{"./nvm.sh"}, - Args: []string{"install"}, - CustomErrors: map[int8]error{ - 1: errors.New("failed to download package"), - }, - //Cwd: "/home//", - //Shell: "/bin/bash", - }) - - if err != nil { - log.Fatal(err) - } - - if err := cmd.Run(); err != nil { - log.Fatal(err) - } - -} -- 2.45.2 From cafa0dcca17d2b02b86ee565b619f28a8a5e29c8 Mon Sep 17 00:00:00 2001 From: Shane C Date: Fri, 5 Jul 2024 11:28:33 -0400 Subject: [PATCH 07/36] add node command --- cmd/node.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/which.go | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 cmd/node.go diff --git a/cmd/node.go b/cmd/node.go new file mode 100644 index 0000000..2e57784 --- /dev/null +++ b/cmd/node.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "errors" + "fmt" + "os/exec" + "strings" +) + +var NodeNotFound = errors.New("nodejs not found") + +func Node(args ...string) (output string, err error) { + + if _, err := Which("node"); err != nil { + if errors.Is(err, ErrNotFound) { + return "", NodeNotFound + } else { + return "", err + } + } + + command := exec.Command("node", args...) + + outputBytes, err := command.Output() + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + if exitErr.ExitCode() == 1 { + return "", ErrNotFound + } else { + return "", fmt.Errorf("command error: %w", err) + } + } + } + + return strings.Trim(string(outputBytes), "\n"), nil + +} diff --git a/cmd/which.go b/cmd/which.go index c7acd6a..b66eafc 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -9,7 +9,7 @@ import ( var ErrNotFound = errors.New("which: command not found") -func Which(cmd string) (string, error) { +func Which(cmd string) (dir string, err error) { command := exec.Command("which", cmd) -- 2.45.2 From e6ff94c45fb692d7081a5a1710d164d845941b00 Mon Sep 17 00:00:00 2001 From: Shane C Date: Fri, 5 Jul 2024 11:33:12 -0400 Subject: [PATCH 08/36] add python command --- cmd/python.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 cmd/python.go diff --git a/cmd/python.go b/cmd/python.go new file mode 100644 index 0000000..b1ddc22 --- /dev/null +++ b/cmd/python.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "errors" + "fmt" + "os/exec" + "strings" +) + +var PythonNotFound = errors.New("python not found") + +func Python(args ...string) (output string, err error) { + + if _, err := Which("python3"); err != nil { + if errors.Is(err, ErrNotFound) { + return "", PythonNotFound + } else { + return "", err + } + } + + command := exec.Command("python3", args...) + + outputBytes, err := command.Output() + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + if exitErr.ExitCode() == 1 { + return "", ErrNotFound + } else { + return "", fmt.Errorf("command error: %w", err) + } + } + } + + return strings.Trim(string(outputBytes), "\n"), nil + +} -- 2.45.2 From 4e2c314c2c95b44f2b695646b301bcf8476f1adf Mon Sep 17 00:00:00 2001 From: Shane C Date: Fri, 5 Jul 2024 12:05:22 -0400 Subject: [PATCH 09/36] add pip command and fix error codes on all commands --- cmd/node.go | 6 +----- cmd/pip.go | 33 +++++++++++++++++++++++++++++++++ cmd/python.go | 6 +----- 3 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 cmd/pip.go diff --git a/cmd/node.go b/cmd/node.go index 2e57784..dc2d099 100644 --- a/cmd/node.go +++ b/cmd/node.go @@ -25,11 +25,7 @@ func Node(args ...string) (output string, err error) { if err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { - if exitErr.ExitCode() == 1 { - return "", ErrNotFound - } else { - return "", fmt.Errorf("command error: %w", err) - } + return "", fmt.Errorf("command error: %w", err) } } diff --git a/cmd/pip.go b/cmd/pip.go new file mode 100644 index 0000000..1dd3a01 --- /dev/null +++ b/cmd/pip.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "errors" + "fmt" + "os/exec" + "strings" +) + +func Pip(args ...string) (output string, err error) { + + if _, err := Which("python3"); err != nil { + if errors.Is(err, ErrNotFound) { + return "", PythonNotFound + } else { + return "", err + } + } + + command := exec.Command("python3", "-m", "pip") + command.Args = append(command.Args, args...) + + outputBytes, err := command.Output() + if err != nil { + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + return "", fmt.Errorf("command error: %w", err) + } + } + + return strings.Trim(string(outputBytes), "\n"), nil + +} diff --git a/cmd/python.go b/cmd/python.go index b1ddc22..9592a67 100644 --- a/cmd/python.go +++ b/cmd/python.go @@ -25,11 +25,7 @@ func Python(args ...string) (output string, err error) { if err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { - if exitErr.ExitCode() == 1 { - return "", ErrNotFound - } else { - return "", fmt.Errorf("command error: %w", err) - } + return "", fmt.Errorf("command error: %w", err) } } -- 2.45.2 From 750686fd1ffbc8ed4f9192ae07079e3bb0958e8c Mon Sep 17 00:00:00 2001 From: Shane C Date: Sat, 6 Jul 2024 11:48:45 -0400 Subject: [PATCH 10/36] add ability for live output, add events system --- linux/events.go | 10 ++++ linux/interface.go | 10 +++- linux/run.go | 134 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 138 insertions(+), 16 deletions(-) create mode 100644 linux/events.go diff --git a/linux/events.go b/linux/events.go new file mode 100644 index 0000000..df2c37c --- /dev/null +++ b/linux/events.go @@ -0,0 +1,10 @@ +package linux + +const ( + EventOutput = iota + EventStderr +) + +type EventOutputData struct { + Output string +} diff --git a/linux/interface.go b/linux/interface.go index d958111..b0267d0 100644 --- a/linux/interface.go +++ b/linux/interface.go @@ -2,10 +2,17 @@ package linux import ( "errors" + "io" + "sync" ) type LinuxCommand struct { - Options CommandOptions + Options CommandOptions + handlers map[int]interface{} + wg sync.WaitGroup + stdout io.ReadCloser + stderr io.ReadCloser + stdin io.WriteCloser } type CommandOptions struct { @@ -24,4 +31,5 @@ var ( ErrRunningCmd = errors.New("error running command") ErrCommandNotFound = errors.New("error command not found") ErrCommandNotExecutable = errors.New("error command not executable") + ErrInvalidHandler = errors.New("invalid handler") ) diff --git a/linux/run.go b/linux/run.go index b02cdef..68dc1b7 100644 --- a/linux/run.go +++ b/linux/run.go @@ -1,10 +1,15 @@ package linux import ( + "bufio" "errors" "fmt" + "golang.org/x/sys/unix" "os" "os/exec" + "os/signal" + "strings" + "syscall" ) func NewCommand(options CommandOptions) (*LinuxCommand, error) { @@ -22,34 +27,68 @@ func NewCommand(options CommandOptions) (*LinuxCommand, error) { } return &LinuxCommand{ - Options: options, + Options: options, + handlers: make(map[int]interface{}), }, nil } +func (cmd *LinuxCommand) AddHandler(handler interface{}) error { + + switch h := handler.(type) { + case func(data EventOutputData) error: + cmd.handlers[EventOutput] = h + break + default: + return ErrInvalidHandler + } + + return nil + +} + func (cmd *LinuxCommand) Run() error { - var sourceCommand string + var sourceCommand strings.Builder for _, value := range cmd.Options.Sources { - sourceCommand += fmt.Sprintf("source %s && ", value) + sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) } - command := exec.Command(cmd.Options.Shell, "-c", sourceCommand+cmd.Options.Command) + var commandOptions strings.Builder + commandOptions.WriteString(" ") + for index, arg := range cmd.Options.Args { + if len(cmd.Options.Args)-1 == index { + commandOptions.WriteString(fmt.Sprintf("%s", arg)) + } else { + commandOptions.WriteString(fmt.Sprintf("%s ", arg)) + } + } + + command := exec.Command(cmd.Options.Shell, "-c", sourceCommand.String()+cmd.Options.Command+commandOptions.String()) command.Dir = cmd.Options.Cwd - command.Args = append(command.Args, cmd.Options.Args...) - // Loop through env to format and add them to the command. - for key, value := range cmd.Options.Env { - command.Env = append(command.Env, fmt.Sprintf("%s=%s", key, value)) - } + var signalChannel chan os.Signal - isCommandExecutable, err := cmd.isCommandExecutable(cmd.Options.Command) - if err != nil { - return err - } + if len(cmd.handlers) != 0 { + command.SysProcAttr = &unix.SysProcAttr{Setsid: true} + signalChannel = make(chan os.Signal, 1) + signal.Notify(signalChannel, unix.SIGINT, unix.SIGTERM) - if !isCommandExecutable { - return ErrCommandNotExecutable + var err error + cmd.stdout, err = command.StdoutPipe() + if err != nil { + return err + } + + cmd.stdin, err = command.StdinPipe() + if err != nil { + return err + } + + cmd.stderr, err = command.StderrPipe() + if err != nil { + return err + } } if err := command.Start(); err != nil { @@ -65,6 +104,67 @@ func (cmd *LinuxCommand) Run() error { } } + cmd.wg.Add(3) + + go func() { + defer cmd.wg.Done() + scanner := bufio.NewScanner(cmd.stderr) + for scanner.Scan() { + line := scanner.Text() + if h, ok := cmd.handlers[EventOutput]; ok { + if err := h.(func(data EventOutputData) error)(EventOutputData{ + Output: line, + }); err != nil { + return + } + } + } + }() + + go func() { + defer cmd.wg.Done() + + scanner := bufio.NewScanner(cmd.stdout) + for scanner.Scan() { + line := scanner.Text() + if h, ok := cmd.handlers[EventOutput]; ok { + if err := h.(func(data EventOutputData) error)(EventOutputData{ + Output: line, + }); err != nil { + return + } + } + } + }() + + go func() { + defer cmd.wg.Done() + + select { + case _, ok := <-signalChannel: + if !ok { + return + } + if err := unix.Kill(-command.Process.Pid, syscall.SIGINT); err != nil { + return + } + } + }() + + // Loop through env to format and add them to the command. + for key, value := range cmd.Options.Env { + command.Env = append(command.Env, fmt.Sprintf("%s=%s", key, value)) + } + + isCommandExecutable, err := cmd.isCommandExecutable(cmd.Options.Command) + if err != nil { + return err + } + + if !isCommandExecutable { + return ErrCommandNotExecutable + } + if err := command.Wait(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { @@ -78,6 +178,10 @@ func (cmd *LinuxCommand) Run() error { } } + close(signalChannel) + signal.Stop(signalChannel) + cmd.wg.Wait() + return nil } -- 2.45.2 From bde6ae681e0f1b2a03d1db732ac0e562e99d0302 Mon Sep 17 00:00:00 2001 From: Shane C Date: Sat, 6 Jul 2024 15:09:17 -0400 Subject: [PATCH 11/36] add exit event --- linux/events.go | 11 ++++++++-- linux/interface.go | 1 + linux/run.go | 53 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/linux/events.go b/linux/events.go index df2c37c..f8168b5 100644 --- a/linux/events.go +++ b/linux/events.go @@ -2,9 +2,16 @@ package linux const ( EventOutput = iota - EventStderr + EventExit ) type EventOutputData struct { - Output string + Output string + CmdOptions CommandOptions +} + +type EventExitData struct { + HasSucceeded bool + ExitCode int + CmdOptions CommandOptions } diff --git a/linux/interface.go b/linux/interface.go index b0267d0..2e7950c 100644 --- a/linux/interface.go +++ b/linux/interface.go @@ -32,4 +32,5 @@ var ( ErrCommandNotFound = errors.New("error command not found") ErrCommandNotExecutable = errors.New("error command not executable") ErrInvalidHandler = errors.New("invalid handler") + ErrRunningEvt = errors.New("error running event") ) diff --git a/linux/run.go b/linux/run.go index 68dc1b7..308d28e 100644 --- a/linux/run.go +++ b/linux/run.go @@ -39,6 +39,8 @@ func (cmd *LinuxCommand) AddHandler(handler interface{}) error { case func(data EventOutputData) error: cmd.handlers[EventOutput] = h break + case func(data EventExitData) error: + cmd.handlers[EventExit] = h default: return ErrInvalidHandler } @@ -94,6 +96,7 @@ func (cmd *LinuxCommand) Run() error { if err := command.Start(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { + fmt.Println(exitErr.String()) if exitErr.ExitCode() == 127 { return ErrCommandNotFound } else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok { @@ -113,7 +116,8 @@ func (cmd *LinuxCommand) Run() error { line := scanner.Text() if h, ok := cmd.handlers[EventOutput]; ok { if err := h.(func(data EventOutputData) error)(EventOutputData{ - Output: line, + Output: line, + CmdOptions: cmd.Options, }); err != nil { return } @@ -129,7 +133,8 @@ func (cmd *LinuxCommand) Run() error { line := scanner.Text() if h, ok := cmd.handlers[EventOutput]; ok { if err := h.(func(data EventOutputData) error)(EventOutputData{ - Output: line, + Output: line, + CmdOptions: cmd.Options, }); err != nil { return } @@ -165,19 +170,61 @@ func (cmd *LinuxCommand) Run() error { return ErrCommandNotExecutable } + var exitInfo *EventExitData + + if _, ok := cmd.handlers[EventExit]; ok { + exitInfo = &EventExitData{ + HasSucceeded: true, + CmdOptions: cmd.Options, + } + } + if err := command.Wait(); err != nil { var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if errors.As(err, &exitErr) && exitErr.String() != "signal: interrupt" { if exitErr.ExitCode() == 127 { return ErrCommandNotFound } else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok { + if h, ok := cmd.handlers[EventExit]; ok { + if exitInfo == nil { + return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err) + } + exitInfo.HasSucceeded = false + exitInfo.ExitCode = exitErr.ExitCode() + err := h.(func(data EventExitData) error)(*exitInfo) + if err != nil { + return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err) + } + } return cmd.Options.CustomErrors[int8(exitErr.ExitCode())] } else { + if h, ok := cmd.handlers[EventExit]; ok { + if exitInfo == nil { + return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err) + } + exitInfo.HasSucceeded = false + exitInfo.ExitCode = exitErr.ExitCode() + err := h.(func(data EventExitData) error)(*exitInfo) + if err != nil { + return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err) + } + } return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err) } } } + if h, ok := cmd.handlers[EventExit]; ok { + if exitInfo == nil { + return nil + } + exitInfo.ExitCode = 0 + err := h.(func(data EventExitData) error)(*exitInfo) + if err != nil { + return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err) + } + } + close(signalChannel) signal.Stop(signalChannel) cmd.wg.Wait() -- 2.45.2 From 992dd1427282b81b3da5fa58aa730b8f25843292 Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 9 Jul 2024 17:45:33 -0400 Subject: [PATCH 12/36] add env options --- linux/run.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/linux/run.go b/linux/run.go index 308d28e..e890eb7 100644 --- a/linux/run.go +++ b/linux/run.go @@ -69,6 +69,10 @@ func (cmd *LinuxCommand) Run() error { command := exec.Command(cmd.Options.Shell, "-c", sourceCommand.String()+cmd.Options.Command+commandOptions.String()) command.Dir = cmd.Options.Cwd + for key, value := range cmd.Options.Env { + command.Env = append(command.Env, fmt.Sprintf("%s=%s", key, value)) + } + var signalChannel chan os.Signal if len(cmd.handlers) != 0 { -- 2.45.2 From 5d646427bdd962a4d02a18da36f5693cb0a1d095 Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 9 Jul 2024 18:44:28 -0400 Subject: [PATCH 13/36] add env and source options to commands --- cmd/which.go | 23 +++++++++++++++++++++-- linux/run.go | 5 ----- linux/utils.go | 6 +++++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cmd/which.go b/cmd/which.go index b66eafc..647d809 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -9,9 +9,28 @@ import ( var ErrNotFound = errors.New("which: command not found") -func Which(cmd string) (dir string, err error) { +type WhichOptions struct { + Env map[string]string + Sources []string + Cwd string +} - command := exec.Command("which", cmd) +func Which(cmd string, options WhichOptions) (dir string, err error) { + + var sourceCommand strings.Builder + for _, value := range options.Sources { + sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) + } + + command := exec.Command(sourceCommand.String(), "which", cmd) + + if options.Cwd != "" { + command.Dir = options.Cwd + } + + for k, v := range options.Env { + command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v)) + } outputBytes, err := command.Output() if err != nil { diff --git a/linux/run.go b/linux/run.go index e890eb7..f83aa10 100644 --- a/linux/run.go +++ b/linux/run.go @@ -160,11 +160,6 @@ func (cmd *LinuxCommand) Run() error { } }() - // Loop through env to format and add them to the command. - for key, value := range cmd.Options.Env { - command.Env = append(command.Env, fmt.Sprintf("%s=%s", key, value)) - } - isCommandExecutable, err := cmd.isCommandExecutable(cmd.Options.Command) if err != nil { return err diff --git a/linux/utils.go b/linux/utils.go index 7146b29..db2a126 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -12,7 +12,11 @@ import ( func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { - whichOut, err := cmd2.Which(command) + whichOut, err := cmd2.Which(command, cmd2.WhichOptions{ + Env: cmd.Options.Env, + Sources: cmd.Options.Sources, + Cwd: cmd.Options.Cwd, + }) if err != nil { if errors.Is(err, cmd2.ErrNotFound) { if _, err := os.Stat(command); errors.Is(err, fs.ErrNotExist) { -- 2.45.2 From 991429a290ac85acdc3977c380a957d0db4c65de Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 9 Jul 2024 18:53:22 -0400 Subject: [PATCH 14/36] update which options --- cmd/node.go | 8 ++++++-- cmd/pip.go | 8 ++++++-- cmd/python.go | 8 ++++++-- cmd/which.go | 4 ++-- linux/utils.go | 2 +- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/cmd/node.go b/cmd/node.go index dc2d099..32bc6f2 100644 --- a/cmd/node.go +++ b/cmd/node.go @@ -9,9 +9,13 @@ import ( var NodeNotFound = errors.New("nodejs not found") -func Node(args ...string) (output string, err error) { +func Node(options BasicOptions, args ...string) (output string, err error) { - if _, err := Which("node"); err != nil { + if _, err := Which("node", BasicOptions{ + Env: options.Env, + Sources: options.Sources, + Cwd: options.Cwd, + }); err != nil { if errors.Is(err, ErrNotFound) { return "", NodeNotFound } else { diff --git a/cmd/pip.go b/cmd/pip.go index 1dd3a01..974b3ee 100644 --- a/cmd/pip.go +++ b/cmd/pip.go @@ -7,9 +7,13 @@ import ( "strings" ) -func Pip(args ...string) (output string, err error) { +func Pip(options BasicOptions, args ...string) (output string, err error) { - if _, err := Which("python3"); err != nil { + if _, err := Which("python3", BasicOptions{ + Env: options.Env, + Sources: options.Sources, + Cwd: options.Cwd, + }); err != nil { if errors.Is(err, ErrNotFound) { return "", PythonNotFound } else { diff --git a/cmd/python.go b/cmd/python.go index 9592a67..2945854 100644 --- a/cmd/python.go +++ b/cmd/python.go @@ -9,9 +9,13 @@ import ( var PythonNotFound = errors.New("python not found") -func Python(args ...string) (output string, err error) { +func Python(options BasicOptions, args ...string) (output string, err error) { - if _, err := Which("python3"); err != nil { + if _, err := Which("python3", BasicOptions{ + Env: options.Env, + Sources: options.Sources, + Cwd: options.Cwd, + }); err != nil { if errors.Is(err, ErrNotFound) { return "", PythonNotFound } else { diff --git a/cmd/which.go b/cmd/which.go index 647d809..80b5fae 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -9,13 +9,13 @@ import ( var ErrNotFound = errors.New("which: command not found") -type WhichOptions struct { +type BasicOptions struct { Env map[string]string Sources []string Cwd string } -func Which(cmd string, options WhichOptions) (dir string, err error) { +func Which(cmd string, options BasicOptions) (dir string, err error) { var sourceCommand strings.Builder for _, value := range options.Sources { diff --git a/linux/utils.go b/linux/utils.go index db2a126..2f18318 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -12,7 +12,7 @@ import ( func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { - whichOut, err := cmd2.Which(command, cmd2.WhichOptions{ + whichOut, err := cmd2.Which(command, cmd2.BasicOptions{ Env: cmd.Options.Env, Sources: cmd.Options.Sources, Cwd: cmd.Options.Cwd, -- 2.45.2 From 182f75e37a6f361ad28457f2a576f87f85e112f3 Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 9 Jul 2024 19:13:10 -0400 Subject: [PATCH 15/36] put code in right palce --- linux/run.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/linux/run.go b/linux/run.go index f83aa10..960fd37 100644 --- a/linux/run.go +++ b/linux/run.go @@ -51,6 +51,15 @@ func (cmd *LinuxCommand) AddHandler(handler interface{}) error { func (cmd *LinuxCommand) Run() error { + isCommandExecutable, err := cmd.isCommandExecutable(cmd.Options.Command) + if err != nil { + return err + } + + if !isCommandExecutable { + return ErrCommandNotExecutable + } + var sourceCommand strings.Builder for _, value := range cmd.Options.Sources { sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) @@ -100,7 +109,6 @@ func (cmd *LinuxCommand) Run() error { if err := command.Start(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { - fmt.Println(exitErr.String()) if exitErr.ExitCode() == 127 { return ErrCommandNotFound } else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok { @@ -160,15 +168,6 @@ func (cmd *LinuxCommand) Run() error { } }() - isCommandExecutable, err := cmd.isCommandExecutable(cmd.Options.Command) - if err != nil { - return err - } - - if !isCommandExecutable { - return ErrCommandNotExecutable - } - var exitInfo *EventExitData if _, ok := cmd.handlers[EventExit]; ok { -- 2.45.2 From 0fb6fcc14aee71262fe0ae95f4420e202e7a85d6 Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 9 Jul 2024 19:21:07 -0400 Subject: [PATCH 16/36] panic thingy --- linux/run.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linux/run.go b/linux/run.go index 960fd37..cd564d5 100644 --- a/linux/run.go +++ b/linux/run.go @@ -106,6 +106,8 @@ func (cmd *LinuxCommand) Run() error { } } + fmt.Println(command.String()) + if err := command.Start(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { -- 2.45.2 From c7db79fe13d94c0d3592021dac129b0bb612fa7b Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 20:16:13 -0400 Subject: [PATCH 17/36] remove sources from which command --- cmd/which.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/which.go b/cmd/which.go index 80b5fae..168abdd 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -22,7 +22,7 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) } - command := exec.Command(sourceCommand.String(), "which", cmd) + command := exec.Command("which", cmd) if options.Cwd != "" { command.Dir = options.Cwd -- 2.45.2 From c0fb972b5fb9efb5527356e1b188a0f3be945e27 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 20:20:18 -0400 Subject: [PATCH 18/36] debug thing --- linux/utils.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linux/utils.go b/linux/utils.go index 2f18318..ca76814 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -31,6 +31,8 @@ func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { command = whichOut } + fmt.Println("cmd: ", command) + if err := unix.Access(command, unix.X_OK); err != nil { if err == unix.EACCES { return false, nil -- 2.45.2 From 8390e16616e8e3cbbaf3e8b17cc2ef6d62f817a8 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 20:22:32 -0400 Subject: [PATCH 19/36] switch to whichOut var for executable check --- linux/utils.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/linux/utils.go b/linux/utils.go index ca76814..21d38fb 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -27,13 +27,11 @@ func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { } } - if len(whichOut) > 0 { - command = whichOut + if len(whichOut) == 0 { + return false, nil } - fmt.Println("cmd: ", command) - - if err := unix.Access(command, unix.X_OK); err != nil { + if err := unix.Access(whichOut, unix.X_OK); err != nil { if err == unix.EACCES { return false, nil } else { -- 2.45.2 From def7d6082596280436f0c31df0907d05db7f1b90 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 20:23:27 -0400 Subject: [PATCH 20/36] debug --- linux/utils.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linux/utils.go b/linux/utils.go index 21d38fb..25b3205 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -31,6 +31,8 @@ func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { return false, nil } + fmt.Println("which:", whichOut) + if err := unix.Access(whichOut, unix.X_OK); err != nil { if err == unix.EACCES { return false, nil -- 2.45.2 From 69b891d598f912219a484de4f9c7b9bc4d2276b7 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 20:24:17 -0400 Subject: [PATCH 21/36] debug --- linux/utils.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linux/utils.go b/linux/utils.go index 25b3205..8abe103 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -27,6 +27,8 @@ func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { } } + fmt.Println(whichOut) + if len(whichOut) == 0 { return false, nil } -- 2.45.2 From 751d90c8405397b50ff3e537969c2c606708cc39 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:36:57 -0400 Subject: [PATCH 22/36] debug --- cmd/which.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/which.go b/cmd/which.go index 168abdd..c13f1b5 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -17,6 +17,9 @@ type BasicOptions struct { func Which(cmd string, options BasicOptions) (dir string, err error) { + fmt.Println(options.Env) + fmt.Println(options.Sources) + var sourceCommand strings.Builder for _, value := range options.Sources { sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) -- 2.45.2 From 6cbbe692236f9a4aa3d708ef9a92ad0a9a3bf2e6 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:37:45 -0400 Subject: [PATCH 23/36] debug --- cmd/which.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/which.go b/cmd/which.go index c13f1b5..7a65145 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -47,6 +47,8 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { } } + fmt.Println(string(outputBytes)) + return strings.Trim(string(outputBytes), "\n"), nil } -- 2.45.2 From db8c33a5263c2a990fb645333cda23177e91f06a Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:41:36 -0400 Subject: [PATCH 24/36] debug --- cmd/which.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/which.go b/cmd/which.go index 7a65145..755bf6e 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -25,7 +25,7 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) } - command := exec.Command("which", cmd) + command := exec.Command("/bin/bash", "-c", "which", cmd) if options.Cwd != "" { command.Dir = options.Cwd -- 2.45.2 From 060a6a49ebe1d0c28bb5a8a93baaeac4ddcdf6a7 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:42:20 -0400 Subject: [PATCH 25/36] debug --- cmd/which.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/which.go b/cmd/which.go index 755bf6e..f7afd16 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -31,9 +31,9 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { command.Dir = options.Cwd } - for k, v := range options.Env { - command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v)) - } + //for k, v := range options.Env { + // command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v)) + //} outputBytes, err := command.Output() if err != nil { -- 2.45.2 From 8a7fdf0f4324cf95e76a9dfef4f73da630b2fcb2 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:43:11 -0400 Subject: [PATCH 26/36] debug --- cmd/which.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/which.go b/cmd/which.go index f7afd16..a06f08a 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -25,6 +25,8 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) } + fmt.Println(cmd) + command := exec.Command("/bin/bash", "-c", "which", cmd) if options.Cwd != "" { -- 2.45.2 From f627fde1866bf00057bce938f1ebdea3bbe86501 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:45:07 -0400 Subject: [PATCH 27/36] debug --- cmd/which.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/cmd/which.go b/cmd/which.go index a06f08a..7578692 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -17,25 +17,20 @@ type BasicOptions struct { func Which(cmd string, options BasicOptions) (dir string, err error) { - fmt.Println(options.Env) - fmt.Println(options.Sources) - var sourceCommand strings.Builder for _, value := range options.Sources { sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) } - fmt.Println(cmd) - command := exec.Command("/bin/bash", "-c", "which", cmd) if options.Cwd != "" { command.Dir = options.Cwd } - //for k, v := range options.Env { - // command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v)) - //} + for k, v := range options.Env { + command.Env = append(command.Env, fmt.Sprintf("%s=%s", k, v)) + } outputBytes, err := command.Output() if err != nil { @@ -49,8 +44,6 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { } } - fmt.Println(string(outputBytes)) - return strings.Trim(string(outputBytes), "\n"), nil } -- 2.45.2 From 3e5b7f99fc687542349cf673e39a5966aabbfc9d Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:47:50 -0400 Subject: [PATCH 28/36] debug --- linux/utils.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/linux/utils.go b/linux/utils.go index 8abe103..d033379 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -12,6 +12,8 @@ import ( func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { + fmt.Println(command) + whichOut, err := cmd2.Which(command, cmd2.BasicOptions{ Env: cmd.Options.Env, Sources: cmd.Options.Sources, -- 2.45.2 From f9e5426b1b1eb50240b4def3b7972706e66d80d3 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:48:37 -0400 Subject: [PATCH 29/36] debug --- cmd/which.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/which.go b/cmd/which.go index 7578692..523d5c5 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -44,6 +44,8 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { } } + fmt.Println(string(outputBytes)) + return strings.Trim(string(outputBytes), "\n"), nil } -- 2.45.2 From ad63e155642957368eaf3e95d571174bc855d2ff Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 10 Jul 2024 21:49:07 -0400 Subject: [PATCH 30/36] debug --- cmd/which.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/which.go b/cmd/which.go index 523d5c5..25c2cdb 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -22,7 +22,7 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { sourceCommand.WriteString(fmt.Sprintf("source %s && ", value)) } - command := exec.Command("/bin/bash", "-c", "which", cmd) + command := exec.Command("which", cmd) if options.Cwd != "" { command.Dir = options.Cwd -- 2.45.2 From 2a59a80b11250c162c3dcf2d61edfd9aa0bdf72c Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 11 Jul 2024 15:54:59 -0400 Subject: [PATCH 31/36] remove debug stuff --- cmd/which.go | 2 -- linux/run.go | 2 -- linux/utils.go | 8 +------- 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/cmd/which.go b/cmd/which.go index 25c2cdb..168abdd 100644 --- a/cmd/which.go +++ b/cmd/which.go @@ -44,8 +44,6 @@ func Which(cmd string, options BasicOptions) (dir string, err error) { } } - fmt.Println(string(outputBytes)) - return strings.Trim(string(outputBytes), "\n"), nil } diff --git a/linux/run.go b/linux/run.go index cd564d5..960fd37 100644 --- a/linux/run.go +++ b/linux/run.go @@ -106,8 +106,6 @@ func (cmd *LinuxCommand) Run() error { } } - fmt.Println(command.String()) - if err := command.Start(); err != nil { var exitErr *exec.ExitError if errors.As(err, &exitErr) { diff --git a/linux/utils.go b/linux/utils.go index d033379..5d21199 100644 --- a/linux/utils.go +++ b/linux/utils.go @@ -12,8 +12,6 @@ import ( func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { - fmt.Println(command) - whichOut, err := cmd2.Which(command, cmd2.BasicOptions{ Env: cmd.Options.Env, Sources: cmd.Options.Sources, @@ -29,14 +27,10 @@ func (cmd *LinuxCommand) isCommandExecutable(command string) (bool, error) { } } - fmt.Println(whichOut) - if len(whichOut) == 0 { return false, nil } - - fmt.Println("which:", whichOut) - + if err := unix.Access(whichOut, unix.X_OK); err != nil { if err == unix.EACCES { return false, nil -- 2.45.2 From 2b190eccb73ec5abdd252e2604c59b9ab5997965 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 11 Jul 2024 16:34:08 -0400 Subject: [PATCH 32/36] fix goroutine call for commands with no handlers --- linux/run.go | 58 ++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/linux/run.go b/linux/run.go index 960fd37..52b0469 100644 --- a/linux/run.go +++ b/linux/run.go @@ -119,40 +119,44 @@ func (cmd *LinuxCommand) Run() error { } } - cmd.wg.Add(3) + cmd.wg.Add(1) - go func() { - defer cmd.wg.Done() - scanner := bufio.NewScanner(cmd.stderr) - for scanner.Scan() { - line := scanner.Text() - if h, ok := cmd.handlers[EventOutput]; ok { - if err := h.(func(data EventOutputData) error)(EventOutputData{ - Output: line, - CmdOptions: cmd.Options, - }); err != nil { - return + if len(cmd.handlers) != 0 { + cmd.wg.Add(2) + + go func() { + defer cmd.wg.Done() + scanner := bufio.NewScanner(cmd.stderr) + for scanner.Scan() { + line := scanner.Text() + if h, ok := cmd.handlers[EventOutput]; ok { + if err := h.(func(data EventOutputData) error)(EventOutputData{ + Output: line, + CmdOptions: cmd.Options, + }); err != nil { + return + } } } - } - }() + }() - go func() { - defer cmd.wg.Done() + go func() { + defer cmd.wg.Done() - scanner := bufio.NewScanner(cmd.stdout) - for scanner.Scan() { - line := scanner.Text() - if h, ok := cmd.handlers[EventOutput]; ok { - if err := h.(func(data EventOutputData) error)(EventOutputData{ - Output: line, - CmdOptions: cmd.Options, - }); err != nil { - return + scanner := bufio.NewScanner(cmd.stdout) + for scanner.Scan() { + line := scanner.Text() + if h, ok := cmd.handlers[EventOutput]; ok { + if err := h.(func(data EventOutputData) error)(EventOutputData{ + Output: line, + CmdOptions: cmd.Options, + }); err != nil { + return + } } } - } - }() + }() + } go func() { defer cmd.wg.Done() -- 2.45.2 From a693fb5cd6a34c808710444bdf7859ce988600b5 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 11 Jul 2024 16:45:10 -0400 Subject: [PATCH 33/36] fix sysprocattr --- linux/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/run.go b/linux/run.go index 52b0469..d2f761e 100644 --- a/linux/run.go +++ b/linux/run.go @@ -76,6 +76,7 @@ func (cmd *LinuxCommand) Run() error { } command := exec.Command(cmd.Options.Shell, "-c", sourceCommand.String()+cmd.Options.Command+commandOptions.String()) + command.SysProcAttr = &unix.SysProcAttr{Setsid: true} command.Dir = cmd.Options.Cwd for key, value := range cmd.Options.Env { @@ -85,7 +86,6 @@ func (cmd *LinuxCommand) Run() error { var signalChannel chan os.Signal if len(cmd.handlers) != 0 { - command.SysProcAttr = &unix.SysProcAttr{Setsid: true} signalChannel = make(chan os.Signal, 1) signal.Notify(signalChannel, unix.SIGINT, unix.SIGTERM) -- 2.45.2 From c395f09556f44b9d6a682018380f67e30488428b Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 11 Jul 2024 16:57:03 -0400 Subject: [PATCH 34/36] fix sysprocattr --- linux/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/run.go b/linux/run.go index d2f761e..572b426 100644 --- a/linux/run.go +++ b/linux/run.go @@ -227,7 +227,7 @@ func (cmd *LinuxCommand) Run() error { } } - close(signalChannel) + //close(signalChannel) signal.Stop(signalChannel) cmd.wg.Wait() -- 2.45.2 From 8959b13a0df411ebc844033350eee3b02933f5a3 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 11 Jul 2024 17:13:58 -0400 Subject: [PATCH 35/36] test stuff --- linux/run.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/linux/run.go b/linux/run.go index 572b426..cf31040 100644 --- a/linux/run.go +++ b/linux/run.go @@ -119,8 +119,6 @@ func (cmd *LinuxCommand) Run() error { } } - cmd.wg.Add(1) - if len(cmd.handlers) != 0 { cmd.wg.Add(2) @@ -158,6 +156,8 @@ func (cmd *LinuxCommand) Run() error { }() } + cmd.wg.Add(1) + go func() { defer cmd.wg.Done() @@ -227,8 +227,8 @@ func (cmd *LinuxCommand) Run() error { } } - //close(signalChannel) signal.Stop(signalChannel) + close(signalChannel) cmd.wg.Wait() return nil -- 2.45.2 From b14e2ccfb1a15e4aa828e6d43744776b248ca383 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 11 Jul 2024 17:27:44 -0400 Subject: [PATCH 36/36] fix signals --- linux/run.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/linux/run.go b/linux/run.go index cf31040..ad1c76d 100644 --- a/linux/run.go +++ b/linux/run.go @@ -84,11 +84,10 @@ func (cmd *LinuxCommand) Run() error { } var signalChannel chan os.Signal + signalChannel = make(chan os.Signal, 1) + signal.Notify(signalChannel, unix.SIGINT, unix.SIGTERM) if len(cmd.handlers) != 0 { - signalChannel = make(chan os.Signal, 1) - signal.Notify(signalChannel, unix.SIGINT, unix.SIGTERM) - var err error cmd.stdout, err = command.StdoutPipe() if err != nil { @@ -227,8 +226,8 @@ func (cmd *LinuxCommand) Run() error { } } - signal.Stop(signalChannel) close(signalChannel) + signal.Stop(signalChannel) cmd.wg.Wait() return nil -- 2.45.2