Marc Cataford
5bf3f36559
* refactor: extract shared logic determining which python version is used * feat: default to unshimmed python if no version specified docs: mention of how system-python is evaluated * refactor: extract python version determining funcs * wip: ensure that output from system python check is cleaned for stray spaces * test: coverage for system python fallback * refactor: unused constant * chore: update version to 0.0.5
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func StartFmtGroup(label string) func(string) {
|
|
fmt.Printf("\033[1m%s\033[0m\n", label)
|
|
|
|
return func(message string) {
|
|
fmt.Printf(" %s\n", message)
|
|
}
|
|
}
|
|
|
|
func VersionStringToStruct(version string) VersionTag {
|
|
splitVersion := strings.Split(version, ".")
|
|
|
|
return VersionTag{Major: splitVersion[0], Minor: splitVersion[1], Patch: splitVersion[2]}
|
|
}
|
|
|
|
func ValidateVersion(version string) error {
|
|
if splitVersion := strings.Split(version, "."); len(splitVersion) != 3 {
|
|
return errors.New("Invalid version string. Expected format 'a.b.c'.")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// RunCommand is a thin wrapper around running command-line calls
|
|
// programmatically. It abstracts common configuration like routing
|
|
// output and handling the directory the calls are made from.
|
|
func RunCommand(command []string, cwd string, quiet bool) (string, error) {
|
|
cmd := exec.Command(command[0], command[1:]...)
|
|
|
|
cmd.Dir = cwd
|
|
|
|
var out strings.Builder
|
|
var errOut strings.Builder
|
|
|
|
if !quiet {
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
} else {
|
|
cmd.Stdout = &out
|
|
cmd.Stderr = &errOut
|
|
}
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return errOut.String(), err
|
|
}
|
|
|
|
return out.String(), nil
|
|
}
|