Skip to content

Add version command #43

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

Merged
merged 5 commits into from
Apr 21, 2023
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
14 changes: 3 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@ jobs:
with:
go-version-file: 'src/git-bundle-server/go.mod'

# TODO: when the '-C' option is available, remove the 'cd ...'
# See https://github.com/golang/go/issues/50332 for more details
- name: Build
run: |
cd $GITHUB_WORKSPACE/src/git-bundle-server
go build -v ./...
run: make -C src/git-bundle-server build

- name: Check style
run: |
cd $GITHUB_WORKSPACE/src/git-bundle-server
go vet ./...
run: make -C src/git-bundle-server vet

- name: Run unit tests
run: |
cd $GITHUB_WORKSPACE/src/git-bundle-server
go test -v ./...
run: make -C src/git-bundle-server test
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Release
on:
push:
tags:
- 'v[0-9]*.[0-9]*.[0-9]*' # matches "v<number...>.<number...>.<number>..."
- 'v[0-9]+.[0-9]+.[0-9]+' # matches "v<number>.<number>.<number>"
- 'v[0-9]+.[0-9]+.[0-9]+-*' # matches "v<number>.<number>.<number>-<string>"

jobs:
# Check prerequisites for the workflow
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/git-bundle-server
/git-bundle-web-server
/VERSION-FILE
/bin/
/_dist/
/_docs/
Expand Down
32 changes: 28 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Default target
build:

# Project metadata (note: to package, VERSION *must* be set by the caller)
# Project metadata
# By default, the project version is automatically determined using
# 'git describe'. If you would like to set the version manually, set the
# 'VERSION' variable to the desired version string.
NAME := git-bundle-server
VERSION :=
PACKAGE_REVISION := 1

# Installation information
INSTALL_ROOT := /
Expand All @@ -22,6 +23,7 @@ GOARCH := $(shell go env GOARCH)
# Packaging information
SUPPORTED_PACKAGE_GOARCHES := amd64 arm64
PACKAGE_ARCH := $(GOARCH)
PACKAGE_REVISION := 1

# Guard against environment variables
APPLE_APP_IDENTITY =
Expand All @@ -30,18 +32,39 @@ APPLE_KEYCHAIN_PROFILE =
E2E_FLAGS=
INTEGRATION_FLAGS=

# General targets
.PHONY: FORCE

ifdef VERSION
# If the version is set by the user, don't bother with regenerating the version
# file.
.PHONY: VERSION-FILE
else
# If the version is not set by the user, we need to generate the version file
# and load it.
VERSION-FILE: FORCE
@scripts/generate-version.sh --version-file="$@"
-include VERSION-FILE
endif

# Build targets
LDFLAGS += -X '$(shell go list -m)/cmd/utils.Version=$(VERSION)'

.PHONY: build
build:
$(RM) -r $(BINDIR)
@mkdir -p $(BINDIR)
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) ./...
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) -ldflags "$(LDFLAGS)" ./...

.PHONY: doc
doc:
@scripts/make-docs.sh --docs="$(CURDIR)/docs/man" \
--output="$(DOCDIR)"

.PHONY: vet
vet:
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go vet ./...

# Testing targets
.PHONY: test
test: build
Expand Down Expand Up @@ -193,6 +216,7 @@ endif
.PHONY: clean
clean:
go clean ./...
$(RM) -r VERSION-FILE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is the -r needed, given this is a file?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a -f instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might seem silly, but I went with -r in case someone really messes up their local build environment and creates a VERSION-FILE directory.

The $(RM) built-in variable already has the -f flag included, though.

$(RM) -r $(BINDIR)
$(RM) -r $(DISTDIR)
$(RM) -r $(DOCDIR)
Expand Down
1 change: 1 addition & 0 deletions cmd/git-bundle-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func all(logger log.TraceLogger) []argparse.Subcommand {
NewUpdateCommand(logger, container),
NewUpdateAllCommand(logger, container),
NewListCommand(logger, container),
NewVersionCommand(logger, container),
NewWebServerCommand(logger, container),
}
}
Expand Down
45 changes: 45 additions & 0 deletions cmd/git-bundle-server/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"fmt"

"github.com/github/git-bundle-server/cmd/utils"
"github.com/github/git-bundle-server/internal/argparse"
"github.com/github/git-bundle-server/internal/log"
)

type versionCmd struct {
logger log.TraceLogger
container *utils.DependencyContainer
}

func NewVersionCommand(logger log.TraceLogger, container *utils.DependencyContainer) argparse.Subcommand {
return &versionCmd{
logger: logger,
container: container,
}
}

func (versionCmd) Name() string {
return "version"
}

func (versionCmd) Description() string {
return `
Display the version information for the bundle server CLI.`
}

func (v *versionCmd) Run(ctx context.Context, args []string) error {
parser := argparse.NewArgParser(v.logger, "git-bundle-server version")
parser.Parse(ctx, args)

versionStr := utils.Version
if versionStr == "" {
versionStr = "<no version>"
}

fmt.Printf("git-bundle-server version %s\n", versionStr)

return nil
}
9 changes: 9 additions & 0 deletions cmd/utils/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

// The purpose of this file is to contain globally-accessible variables that
// specify version information for the bundle server. The values of these
// variables are set during the build process when using 'make'.

// The executable's version string with no leading 'v' (e.g. "1.0.0" or
// "1.0.1-g1a2b3c4d").
var Version string
3 changes: 3 additions & 0 deletions docs/man/git-bundle-server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ the server.

== COMMANDS

*version*::
Display the version information for the bundle server CLI

*init* _url_ [_route_]::
Initialize a repository for which bundles should be served. The repository is
cloned into a bare repo from _url_. A base bundle is created for the
Expand Down
55 changes: 55 additions & 0 deletions scripts/generate-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
die () {
echo "$*" >&2
exit 1
}

write_version () {
if [ -n "$1" ]; then
echo "Setting VERSION to $1"
else
echo "No version found!"
fi
echo "VERSION := $1" >"$2"
}

# Parse script arguments
for i in "$@"
do
case "$i" in
--version-file=*)
VERSION_FILE="${i#*=}"
shift # past argument=value
;;
*)
die "unknown option '$i'"
;;
esac
done

if [ -z "$VERSION_FILE" ]; then
die "error: missing version file path"
fi

set -e

# The pattern match used below is *less* restrictive than it should be (i.e.
# "v<number>.<number>.<number>[-<optional string>]"), but we're limited by the
# glob patterns used in 'git describe'.
#
# That said, we're explicitly specifying 'VERSION' for formal releases(based on
# the more precise pattern match in 'release.yml', so it's not super important
# to be strict here.
VERSION_RAW="$(git describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" 2>/dev/null || echo "")"
VERSION="${VERSION_RAW:1}"

if [ -r "$VERSION_FILE" ]
then
CACHE_VERSION=$(sed -e 's/^VERSION := //' <"$VERSION_FILE")
if [ "$VERSION" != "$CACHE_VERSION" ]; then
write_version "$VERSION" "$VERSION_FILE"
fi
else
# If the file doesn't exist, write unconditionally
write_version "$VERSION" "$VERSION_FILE"
fi