Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -version flag support #175

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v4

- name: build assets
run: make release
run: make release VERSION=${{ github.event.release.tag_name }}

- name: gh login
run: echo ${{ secrets.GITHUB_TOKEN }} | gh auth login --with-token
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
SHELL=/bin/bash
VERSION=$(shell git describe --tags --always --dirty)

.PHONY: deps
deps:
go mod vendor

.PHONY: build
build: $(GOFILES)
go build
go build -ldflags "-X main.Version=${VERSION}"

.PHONY: unit
unit:
go test -v -race $$(go list ./... | grep -v /vendor/)
go vet $$(go list ./... | grep -v /vendor/)

.PHONY: integration
integration: VERSION=v1337
integration: build
bats integration

Expand All @@ -32,6 +34,6 @@ fmt:

.PHONY: release
release:
./build.sh
./build.sh ${VERSION}

.DEFAULT_GOAL := test
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ set -x
mkdir -p dist
export GOOS="linux"
export CGO_ENABLED=0
for arch in amd64 386 arm arm64; do GOARCH="$arch" go build && file supercronic | grep 'statically linked' && mv supercronic "dist/supercronic-${GOOS}-${arch}"; done
VERSION=${1:-$(git describe --tags --always --dirty)}
for arch in amd64 386 arm arm64; do GOARCH="$arch" go build -ldflags="-X 'main.Version=$VERSION'" && file supercronic | grep 'statically linked' && mv supercronic "dist/supercronic-${GOOS}-${arch}"; done
pushd dist
ls -lah *
file *
Expand Down
5 changes: 5 additions & 0 deletions integration/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ wait_for() {
return 1
}

@test "it prints the version" {
run "${BATS_TEST_DIRNAME}/../supercronic" -version
[[ "$output" =~ ^v1337$ ]]
}

@test "it starts" {
run_supercronic "${BATS_TEST_DIRNAME}/noop.crontab"
}
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/sirupsen/logrus"
)

var (
Version = "<unset>"
)

var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] CRONTAB\n\nAvailable options:\n", os.Args[0])
flag.PrintDefaults()
Expand All @@ -29,6 +33,7 @@ func main() {
debug := flag.Bool("debug", false, "enable debug logging")
quiet := flag.Bool("quiet", false, "do not log informational messages (takes precedence over debug)")
json := flag.Bool("json", false, "enable JSON logging")
printVersion := flag.Bool("version", false, "print version and exit")
test := flag.Bool("test", false, "test crontab (does not run jobs)")
inotify := flag.Bool("inotify", false, "use inotify to detect crontab file changes")
prometheusListen := flag.String(
Expand Down Expand Up @@ -96,6 +101,12 @@ func main() {
)
}

if *printVersion {
fmt.Println(Version)
os.Exit(0)
return
}

if flag.NArg() != 1 {
Usage()
os.Exit(2)
Expand Down
Loading