2022-09-27 20:19:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-04 10:22:49 +00:00
|
|
|
"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)
|
2023-12-04 10:22:49 +00:00
|
|
|
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 {
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Warn("tool is missing", slog.String("toolName", toolName))
|
2022-09-27 20:19:27 +00:00
|
|
|
return fallbackAction()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|