add pip command and fix error codes on all commands

This commit is contained in:
Shane C 2024-07-05 12:05:22 -04:00
parent e6ff94c45f
commit 4e2c314c2c
Signed by: shane
GPG key ID: E46B5FEA35B22FF9
3 changed files with 35 additions and 10 deletions

View file

@ -25,11 +25,7 @@ func Node(args ...string) (output string, err error) {
if err != nil { if err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 1 { return "", fmt.Errorf("command error: %w", err)
return "", ErrNotFound
} else {
return "", fmt.Errorf("command error: %w", err)
}
} }
} }

33
cmd/pip.go Normal file
View file

@ -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
}

View file

@ -25,11 +25,7 @@ func Python(args ...string) (output string, err error) {
if err != nil { if err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 1 { return "", fmt.Errorf("command error: %w", err)
return "", ErrNotFound
} else {
return "", fmt.Errorf("command error: %w", err)
}
} }
} }