add custom exit code errors
This commit is contained in:
parent
76f276f69e
commit
d127424b9f
3 changed files with 11 additions and 185 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
177
main.go
177
main.go
|
@ -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
|
||||
|
||||
}
|
Loading…
Reference in a new issue