Skip to content

Instantly share code, notes, and snippets.

@AlexJReid
Last active February 18, 2023 14:26
Show Gist options
  • Save AlexJReid/31bef4a83303380a7fb5c36bb08b9f55 to your computer and use it in GitHub Desktop.
Save AlexJReid/31bef4a83303380a7fb5c36bb08b9f55 to your computer and use it in GitHub Desktop.
skeleton
on: ["push"]
name: Test
jobs:
test:
name: Run tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: ">=1.20"
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: go version
- run: make test docker
FROM gcr.io/distroless/base
COPY ./dist/linux/amd64/changeme /changeme
ENTRYPOINT ["/changeme"]
package main
import (
"os"
"github.com/changeme/v2/version"
"github.com/joho/godotenv"
"github.com/mitchellh/cli"
log "github.com/sirupsen/logrus"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Warn("no .env file found")
}
c := cli.NewCLI("changeme", version.GetHumanVersion())
c.Args = os.Args[1:]
c.Commands = Commands
c.HelpFunc = helpFunc()
exitStatus, err := c.Run()
if err != nil {
log.Error(err)
}
os.Exit(exitStatus)
}
SHELL = /usr/bin/env bash -euo pipefail -c
BIN_NAME = CHANGEME
ARCH = $(shell A=$$(uname -m); [ $$A = x86_64 ] && A=amd64; echo $$A)
OS = $(shell uname | tr [[:upper:]] [[:lower:]])
PLATFORM = $(OS)/$(ARCH)
DIST = dist/$(PLATFORM)
BIN = $(DIST)/$(BIN_NAME)
VERSION ?= $(shell ./build-scripts/version.sh version/version.go)
GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
GIT_DIRTY ?= $(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)
PROJECT = $(shell go list -m)
LD_FLAGS ?= -X "$(PROJECT)/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)"
version:
@echo $(VERSION)
.PHONY: version
test:
go test -v ./...
.PHONY: test
dist:
mkdir -p $(DIST)
dev: version dist
GOARCH=$(ARCH) GOOS=$(OS) go build -ldflags "$(LD_FLAGS)" -o $(BIN)
.PHONY: dev
linux: OS = linux
linux: ARCH = amd64
linux: dev
.PHONY: linux
docker: linux
docker build -f Dockerfile -t $(BIN_NAME):$(VERSION) .
.PHONY: docker
package version
import (
"fmt"
"strings"
)
var (
// The git commit that was compiled. These will be filled in by the compiler.
GitCommit string
// The main version number that is being run at the moment.
//
// Version must conform to the format expected by
// github.com/hashicorp/go-version for tests to work.
Version = "0.1.0"
// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease = ""
)
// GetHumanVersion composes the parts of the version in a way that's suitable
// for displaying to humans.
func GetHumanVersion() string {
version := Version
if VersionPrerelease != "" {
version += fmt.Sprintf("-%s", VersionPrerelease)
}
if GitCommit != "" {
version += fmt.Sprintf(" (%s)", GitCommit)
}
// Strip off any single quotes added by the git information.
return "v" + strings.Replace(version, "'", "", -1)
}
#!/usr/bin/env bash
version_file=$1
version=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < "${version_file}")
prerelease=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < "${version_file}")
if [ -n "$prerelease" ]; then
echo "${version}-${prerelease}"
else
echo "${version}"
fi
package version_command
import (
"fmt"
"github.com/mitchellh/cli"
)
type Command struct {
UI cli.Ui
Version string
}
func (c *Command) Run(_ []string) int {
c.UI.Output(fmt.Sprintf("%s", c.Version))
return 0
}
func (c *Command) Synopsis() string {
return "Prints the version of this binary"
}
func (c *Command) Help() string {
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment