Skip to content

Commit

Permalink
add support for -version flag (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxihafer authored Oct 10, 2024
1 parent 8b4edf5 commit cca6b3a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
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 @@ -19,6 +19,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 @@ -28,6 +32,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")
// If this flag changes, update forkExec to disable reaping in the child process
Expand Down Expand Up @@ -97,6 +102,12 @@ func main() {
)
}

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

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

0 comments on commit cca6b3a

Please sign in to comment.