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 }