//go:build mage package main import ( "context" "fmt" "os/exec" "path" "path/filepath" "github.com/carlmjohnson/requests" "github.com/magefile/mage/sh" "golang.org/x/exp/slog" ) var ( GoReleaser = sh.RunCmd("goreleaser") GoInstall = sh.RunCmd("go", "install") GoBuild = sh.RunCmd("go", "build") GoRun = sh.RunCmd("go", "run") ) func getLatestReleaseTag(ctx context.Context, repo string) (tag string, err error) { type release struct { TagName string `json:"tag_name"` } var releases []release err = requests. URL(fmt.Sprintf("https://%s", path.Join("api.github.com/repos", repo, "releases"))). ToJSON(&releases). Fetch(ctx) if err != nil { return "", err } if len(releases) < 1 { return "", fmt.Errorf("no release found for repo %s", repo) } return releases[0].TagName, nil } func ensureURLTool(ctx context.Context, toolName, downloadURL string) error { return checkForTool(toolName, func() error { return requests. URL(downloadURL). ToFile(filepath.Join("/", "usr", "local", "bin", toolName)). Fetch(ctx) }) } 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)) 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)) return fallbackAction() } return nil }