From d127424b9f06622520a29701e488e35943c66b47 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 19:40:33 -0400 Subject: [PATCH] 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 - -}