diff --git a/internal/cli/root.go b/internal/cli/root.go index a95b097..246cf2c 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -42,6 +42,7 @@ schema patch in order, and generates Go structs from the result.`, pf.BoolVarP(&opts.quiet, "quiet", "q", false, "don't print notes and warnings") root.AddCommand( + newVersionCmd(), newGenerateCmd(opts), newResolveCmd(opts), newSchemaCmd(opts), diff --git a/internal/cli/version.go b/internal/cli/version.go new file mode 100644 index 0000000..0d30d51 --- /dev/null +++ b/internal/cli/version.go @@ -0,0 +1,26 @@ +package cli + +import ( + "fmt" + + "git.avsdev.uk/Go/go-ocds/internal/version" + "github.com/spf13/cobra" +) + +func newVersionCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Print version information", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("ocdsgen - %s\n", version.Version) + if version.BuildTime != "unknown" { + fmt.Printf("Built: %s\n", version.BuildTime) + } + if version.GitCommit != "unknown" { + fmt.Printf("Commit: %s\n", version.GitCommit) + } + }, + } + + return cmd +} diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..c0180e9 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,8 @@ +package version + +// Version information (set at build time with -ldflags) +var ( + Version = "dev" + BuildTime = "unknown" + GitCommit = "unknown" +) diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..c9add07 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +# Build script for ocdsgen release + +set -euo pipefail + +BUILD_DIR="build" +RELEASE_DIR="release" +DO_RELEASE=0 + +if [[ $* == *--release* ]]; then + DO_RELEASE=1 +fi + +BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S') +GIT_COMMIT= +VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo 'unknown') +VERSION_TAG=$(git describe --tags 2>/dev/null || echo 'unknown') + +if [ "$VERSION" != "$VERSION_TAG" ]; then + echo "Dirty commits detected, marking as development build" + GIT_COMMIT=$(git rev-parse HEAD 2>/dev/null || echo 'unknown') + GIT_COMMIT_SHORT=$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown') + VERSION="${VERSION}.${GIT_COMMIT_SHORT}" + + DO_RELEASE=0 + if [[ $* == *--release* && $* == *--force* ]]; then + echo "Forcing release build anyway" + DO_RELEASE=1 + fi +fi + +LD_FLAGS=${LD_FLAGS:-""} +LD_FLAGS="$LD_FLAGS -X git.avsdev.uk/Go/go-ocds/internal/version.Version=${VERSION}" +if [[ $DO_RELEASE -ne 1 ]]; then + LD_FLAGS="$LD_FLAGS -X git.avsdev.uk/Go/go-ocds/internal/version.BuildTime=${BUILD_TIME}" +fi +if [ "$GIT_COMMIT" != "" ]; then + LD_FLAGS="$LD_FLAGS -X git.avsdev.uk/Go/go-ocds/internal/version.GitCommit=${GIT_COMMIT}" +fi + +# Build binary with version info +go build \ + -ldflags="$LD_FLAGS" \ + -trimpath \ + -o $BUILD_DIR/ocdsgen-$VERSION \ + ./cmd/ocdsgen + +echo "Built $BUILD_DIR/ocdsgen-$VERSION" + +# If proper release, copy to output directory for packaging +if [[ $DO_RELEASE -eq 1 ]]; then + install -DT $BUILD_DIR/ocdsgen-$VERSION $RELEASE_DIR/ocdsgen + ARCH=`dpkg --print-architecture` + tar -czf $RELEASE_DIR/ocdsgen-$VERSION-$ARCH.tar.gz \ + --transform "s,^$RELEASE_DIR/,ocdsgen-$VERSION-$ARCH/," \ + $RELEASE_DIR/ocdsgen + echo "Installed $RELEASE_DIR/ocdsgen" +fi