nurse/magefiles/tools.go

33 lines
740 B
Go
Raw Permalink Normal View History

2022-09-27 20:19:27 +00:00
package main
import (
"fmt"
"log/slog"
2022-09-27 20:19:27 +00:00
"os/exec"
"github.com/magefile/mage/sh"
)
var (
GoReleaser = sh.RunCmd("goreleaser")
GoInstall = sh.RunCmd("go", "install")
GoBuild = sh.RunCmd("go", "build")
)
func ensureGoTool(toolName, importPath, version string) error {
return checkForTool(toolName, func() error {
toolToInstall := fmt.Sprintf("%s@%s", importPath, version)
slog.Info("Installing Go tool", slog.String("toolToInstall", toolToInstall))
2022-09-27 20:19:27 +00:00
return GoInstall(toolToInstall)
})
}
func checkForTool(toolName string, fallbackAction func() error) error {
if _, err := exec.LookPath(toolName); err != nil {
slog.Warn("tool is missing", slog.String("toolName", toolName))
2022-09-27 20:19:27 +00:00
return fallbackAction()
}
return nil
}