From 6bd72190ccdf0056f52ae3a99ea076164a74e39b Mon Sep 17 00:00:00 2001 From: Neil O'Toole Date: Fri, 26 May 2023 22:16:52 -0600 Subject: [PATCH] Switch from mage to make (#242) * Switched to Makefile --- Makefile | 28 ++++++++ go.mod | 1 - go.sum | 2 - magefile.go | 193 ---------------------------------------------------- 4 files changed, 28 insertions(+), 196 deletions(-) create mode 100644 Makefile delete mode 100644 magefile.go diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..758883aa --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +PKG := github.com/neilotoole/sq +VERSION_PKG := $(PKG)/cli/buildinfo +BUILD_VERSION := $(shell git describe --tags --always --dirty) +BUILD_COMMIT := $(shell git rev-parse HEAD) +BUILD_TIMESTAMP := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') +LDFLAGS ?= -s -w -X $(VERSION_PKG).Version=$(BUILD_VERSION) -X $(VERSION_PKG).Commit=$(BUILD_COMMIT) -X $(VERSION_PKG).Timestamp=$(BUILD_TIMESTAMP) + + +.PHONY: test +test: + @go test ./... + +.PHONY: install +install: + @go install -ldflags "$(LDFLAGS)" + +.PHONY: lint +lint: + @golangci-lint run --out-format tab --sort-results + +.PHONY: gen +gen: + @go generate ./... + +.PHONY: fmt +fmt: + @# Use gofumpt instead of "go fmt" + @gofumpt -w . diff --git a/go.mod b/go.mod index 49e9fbda..f74af9c4 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ require ( github.com/go-sql-driver/mysql v1.7.1 github.com/google/uuid v1.3.0 github.com/h2non/filetype v1.1.3 - github.com/magefile/mage v1.15.0 github.com/mattn/go-colorable v0.1.13 github.com/mattn/go-isatty v0.0.19 github.com/mattn/go-runewidth v0.0.14 diff --git a/go.sum b/go.sum index 38cea0e1..1eb3444b 100644 --- a/go.sum +++ b/go.sum @@ -76,8 +76,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= -github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= -github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= diff --git a/magefile.go b/magefile.go deleted file mode 100644 index 177e9bcd..00000000 --- a/magefile.go +++ /dev/null @@ -1,193 +0,0 @@ -//go:build mage - -// Magefile for building/test sq. This magefile was originally copied from -// the Hugo magefile, and may contain functionality that can be ditched. -package main - -// See https://magefile.org -// This file originally derived from the Hugo magefile, see https://github.com/gohugoio/hugo - -import ( - "fmt" - "os" - "path/filepath" - "time" - - "github.com/magefile/mage/sh" -) - -const ( - packageName = "github.com/neilotoole/sq" - ldflags = "-X $PACKAGE/cli/buildinfo.Version=$BUILD_VERSION -X $PACKAGE/cli/buildinfo.Timestamp=$BUILD_TIMESTAMP -X $PACKAGE/cli/buildinfo.Commit=$BUILD_COMMIT" -) - -var Default = Install - -func ldflagsEnv() map[string]string { - hash := gitHeadCommit(true) - - return map[string]string{ - "PACKAGE": packageName, - "BUILD_COMMIT": hash, - "BUILD_TIMESTAMP": time.Now().Format("2006-01-02T15:04:05Z0700"), - "BUILD_VERSION": generateBuildVersion(), - "BUILD_BRANCH": gitCurrentBranch(), - } -} - -// Clean cleans the dist dirs, and binaries. -func Clean() error { - if err := sh.Run("rm", "-rf", "./dist*"); err != nil { - return err - } - - // Delete the sq binary that "go build" might produce - if err := sh.Rm("./sq"); err != nil { - return err - } - - if gopath, ok := os.LookupEnv("GOPATH"); ok { - if err := sh.Rm(filepath.Join(gopath, "bin", "sq")); err != nil { - return err - } - } - - return nil -} - -// Build builds sq. -func Build() error { - return sh.RunWith(ldflagsEnv(), "go", "build", "-ldflags", ldflags, packageName) -} - -// Install installs the sq binary. -func Install() error { - return sh.RunWith(ldflagsEnv(), "go", "install", "-ldflags", ldflags, packageName) -} - -// Test runs go test. -func Test() error { - return sh.RunV("go", "test", "-v", "./...") -} - -// Lint runs the golangci-lint linters. -// The golangci-lint binary must be installed: -// -// $ brew install golangci-lint -// -// See .golangci.yml for configuration. -func Lint() error { - return sh.RunV("golangci-lint", "run", "./...") -} - -// Fmt runs gofumpt on the source. -func Fmt() error { - return sh.RunV("gofumpt", "-l", "-w", ".") -} - -// Generate generates SLQ parser Go files from the -// antlr grammar. Note that the antlr generator tool is Java-based; you -// must have Java installed. -func Generate() error { - return sh.Run("go", "generate", "./...") -} - -func panicIf(err error) { - if err != nil { - panic(err) - } -} - -// CheckDocker verifies that docker is running by executing echo -// on alpine. -func CheckDocker() error { - return execDocker("run", "-it", "alpine", "echo", "docker is working") -} - -// execDocker executes a docker command with args, returning -// any error. Example: -// -// execDocker("run", "-it", "alpine", "echo", "hello world") -func execDocker(cmd string, args ...string) error { - args = append([]string{cmd}, args...) - return sh.RunV("docker", args...) -} - -func gitHeadCommit(short bool) string { - args := []string{"rev-parse", "HEAD"} - if short { - args = []string{"rev-parse", "--short", "HEAD"} - } - - hash, err := sh.Output("git", args...) - panicIf(err) - return hash -} - -func gitCommitForLatestTag() string { - hash, err := sh.Output("git", "rev-list", "--tags", "--max-count=1") - panicIf(err) - return hash -} - -func gitTagForCommit(commit string) string { - tag, err := sh.Output("git", "describe", "--tags", commit) - panicIf(err) - return tag -} - -func gitLatestTag() string { - commitForLatestTag := gitCommitForLatestTag() - tag := gitTagForCommit(commitForLatestTag) - return tag -} - -func gitCurrentBranch() string { - branch, err := sh.Output("git", "rev-parse", "--abbrev-ref", "HEAD") - panicIf(err) - return branch -} - -func gitIsDirtyWorkingDir() bool { - diff, err := sh.Output("git", "diff", "HEAD") - panicIf(err) - - // If no diff, then diff's output will be empty. - return diff != "" -} - -// BuildVersion prints the build version that would be -// incorporated into the sq binary. The build version is of -// the form TAG[-SUFFIX], for example "v0.5.9-dev". -// - If working dir is dirty, or if the HEAD commit does not -// match the latest tag commit, the suffix is "-wip" (Work In -// Progress), e.g. "v0.5.9-wip". -// - Else, if the branch is not master, the branch name is -// used as the suffix, e.g. "v0.5.9-dev". -// - Else, we're on master and the HEAD commit is the latest -// tag, so the suffix is omitted, e.g. "v0.5.9". -func BuildVersion() { - fmt.Println(generateBuildVersion()) -} - -func generateBuildVersion() string { - commitForLatestTag := gitCommitForLatestTag() - headCommit := gitHeadCommit(false) - latestTag := gitTagForCommit(commitForLatestTag) - currentBranch := gitCurrentBranch() - - // If working dis is dirty or we're not on the latest tag, - // then add a "-wip" suffix (Work In Progress). - isDirty := gitIsDirtyWorkingDir() - if isDirty || headCommit != commitForLatestTag { - return latestTag + "-wip" - } - - // Else, if the current branch is not master, append the - // branch. - if currentBranch != "master" { - return latestTag + "-" + currentBranch - } - - return latestTag -}