Skip to content

Commit

Permalink
Add --version support (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion authored Jun 20, 2023
1 parent dc381f5 commit 7a28b96
Show file tree
Hide file tree
Showing 8 changed files with 33 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 @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: build
run: bazel build //target-determinator:all //driver:all && mkdir .release-artifacts && for f in $(bazel cquery --output=files 'let bins = kind(go_binary, //target-determinator:all + //driver:all) in $bins - attr(tags, "\bmanual\b", $bins)'); do cp "$(bazel info execution_root)/${f}" .release-artifacts/; done
run: bazel build --stamp --workspace_status_command=./scripts/workspace-status.sh //target-determinator:all //driver:all && mkdir .release-artifacts && for f in $(bazel cquery --output=files 'let bins = kind(go_binary, //target-determinator:all + //driver:all) in $bins - attr(tags, "\bmanual\b", $bins)'); do cp "$(bazel info execution_root)/${f}" .release-artifacts/; done
- name: release
uses: softprops/action-gh-release@v1
with:
Expand Down
1 change: 1 addition & 0 deletions cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ go_library(
deps = [
"//common",
"//pkg",
"//version",
],
)
12 changes: 11 additions & 1 deletion cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/bazel-contrib/target-determinator/common"
"github.com/bazel-contrib/target-determinator/pkg"
"github.com/bazel-contrib/target-determinator/version"
)

type IgnoreFileFlag []common.RelPath
Expand Down Expand Up @@ -65,6 +67,7 @@ func (e *EnforceCleanFlag) Set(value string) error {
}

type CommonFlags struct {
Version bool
WorkingDirectory *string
BazelPath *string
BazelStartupOpts *MultipleStrings
Expand All @@ -86,6 +89,7 @@ func StrPtr() *string {

func RegisterCommonFlags() *CommonFlags {
commonFlags := CommonFlags{
Version: false,
WorkingDirectory: StrPtr(),
BazelPath: StrPtr(),
BazelStartupOpts: &MultipleStrings{},
Expand All @@ -99,6 +103,7 @@ func RegisterCommonFlags() *CommonFlags {
CompareQueriesAroundAnalysisCacheClear: false,
FilterIncompatibleTargets: true,
}
flag.BoolVar(&commonFlags.Version, "version", false, "Print the version of the tool and exit.")
flag.StringVar(commonFlags.WorkingDirectory, "working-directory", ".", "Working directory to query.")
flag.StringVar(commonFlags.BazelPath, "bazel", "bazel",
"Bazel binary (basename on $PATH, or absolute or relative path) to run.")
Expand Down Expand Up @@ -127,7 +132,12 @@ type CommonConfig struct {
}

// ValidateCommonFlags ensures that the argument follow the right format
func ValidateCommonFlags() (targetPattern string, err error) {
func ValidateCommonFlags(commandName string, flags *CommonFlags) (targetPattern string, err error) {
if flags.Version {
fmt.Printf("%s %s\n", commandName, version.Version)
os.Exit(0)
}

positional := flag.Args()
if len(positional) != 1 {
return "", fmt.Errorf("expected one positional argument, <before-revision>, but got %d", len(positional))
Expand Down
2 changes: 1 addition & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func parseFlags() (*driverFlags, error) {
}

var err error
flags.revisionBefore, err = cli.ValidateCommonFlags()
flags.revisionBefore, err = cli.ValidateCommonFlags("driver", flags.commonFlags)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/workspace-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash -eu

echo "VERSION $(git describe --tags --exact-match)"
2 changes: 1 addition & 1 deletion target-determinator/target-determinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func parseFlags() (*targetDeterminatorFlags, error) {
flag.Parse()

var err error
flags.revisionBefore, err = cli.ValidateCommonFlags()
flags.revisionBefore, err = cli.ValidateCommonFlags("target-determinator", flags.commonFlags)
if err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions version/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "version",
srcs = ["version.go"],
importpath = "github.com/bazel-contrib/target-determinator/version",
visibility = ["//visibility:public"],
x_defs = {
"Version": "{VERSION}",
},
)
4 changes: 4 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package version

// Version of target-determinator. This is set by `x_defs` in the owning go_library rule.
var Version = "0.0.0-unstamped"

0 comments on commit 7a28b96

Please sign in to comment.