nitter/build/tools.go
Peter Kurfer 31243b2a5b
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
feat(release): prepare release routine
2023-03-13 08:57:47 +01:00

73 lines
1.6 KiB
Go

//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 fullPath, err := exec.LookPath(toolName); err != nil {
slog.Warn("tool is missing", slog.String("toolName", toolName))
return fallbackAction()
} else {
slog.Info("Found tool", slog.String("path", fullPath))
}
return nil
}