add python command
This commit is contained in:
parent
cafa0dcca1
commit
e6ff94c45f
1 changed files with 38 additions and 0 deletions
38
cmd/python.go
Normal file
38
cmd/python.go
Normal file
|
@ -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
|
||||
|
||||
}
|
Loading…
Reference in a new issue