Skip to content

Commit 71632a1

Browse files
feat: add version flag in the CLI (#43)
* feat: add CLI command description and usage for Globstar * feat: add version information to CLI and build process
1 parent 13af017 commit 71632a1

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ GOLANG_CROSS_VERSION ?= v1.23
44
SYSROOT_DIR ?= sysroots
55
SYSROOT_ARCHIVE ?= sysroots.tar.bz2
66

7+
CLI_BUILD_FLAGS := -X 'globstar.dev/pkg/cli.version=$$(git describe --tags 2>/dev/null || echo dev)'
8+
79
.PHONY: sysroot-pack
810
sysroot-pack:
911
@tar cf - $(SYSROOT_DIR) -P | pv -s $[$(du -sk $(SYSROOT_DIR) | awk '{print $1}') * 1024] | pbzip2 > $(SYSROOT_ARCHIVE)
@@ -48,7 +50,7 @@ release:
4850

4951
.PHONY: test
5052
test:
51-
@CGO_CFLAGS="-w" go test -coverprofile=coverage.out -covermode=atomic ./cmd/... ./pkg/...
53+
@CGO_CFLAGS="-w" go test -coverprofile=coverage.out -covermode=atomic ./cmd/... ./pkg/...
5254
@go tool cover -func=coverage.out | grep total: | awk '{print "Total coverage: " $$3}'
5355
@rm coverage.out
5456

@@ -59,8 +61,8 @@ fmt:
5961
@echo "Done."
6062

6163
build:
62-
CGO_ENABLED=1 go build -o bin/globstar ./cmd/globstar
64+
CGO_ENABLED=1 go build -ldflags "$(CLI_BUILD_FLAGS)" -o bin/globstar ./cmd/globstar
6365

6466
test-builtin-rules:
6567
echo "Testing built-in rules..."
66-
./bin/globstar test -d checkers/
68+
./bin/globstar test -d checkers/

goreleaser.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ builds:
1515
goarch:
1616
- amd64
1717
ldflags:
18-
- "-X 'main.version={{ .Version }}'"
18+
- "-X 'globstar.dev/pkg/cli.version={{ .Version }}'"
1919
# darwin-arm64
2020
- id: globstar-darwin-arm64
2121
main: ./cmd/globstar
@@ -29,7 +29,7 @@ builds:
2929
goarch:
3030
- arm64
3131
ldflags:
32-
- "-X 'main.version={{ .Version }}'"
32+
- "-X 'globstar.dev/pkg/cli.version={{ .Version }}'"
3333
# linux-amd64
3434
- id: globstar-linux-amd64
3535
main: ./cmd/globstar
@@ -43,7 +43,7 @@ builds:
4343
goarch:
4444
- amd64
4545
ldflags:
46-
- "-X 'main.version={{ .Version }}'"
46+
- "-X 'globstar.dev/pkg/cli.version={{ .Version }}'"
4747
# linux-arm64
4848
- id: globstar-linux-arm64
4949
main: ./cmd/globstar
@@ -57,7 +57,7 @@ builds:
5757
goarch:
5858
- arm64
5959
ldflags:
60-
- "-X 'main.version={{ .Version }}'"
60+
- "-X 'globstar.dev/pkg/cli.version={{ .Version }}'"
6161
# windows-amd64
6262
- id: globstar-windows-amd64
6363
main: ./cmd/globstar
@@ -72,7 +72,7 @@ builds:
7272
- amd64
7373
ldflags:
7474
- buildmode=exe
75-
- "-X 'main.version={{ .Version }}'"
75+
- "-X 'globstar.dev/pkg/cli.version={{ .Version }}'"
7676
# windows-arm64
7777
- id: globstar-windows-arm64
7878
main: ./cmd/globstar
@@ -87,7 +87,7 @@ builds:
8787
- arm64
8888
ldflags:
8989
- buildmode=exe
90-
- "-X 'main.version={{ .Version }}'"
90+
- "-X 'globstar.dev/pkg/cli.version={{ .Version }}'"
9191
archives:
9292
- id: arch_rename
9393
builds:

pkg/cli/cli.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"slices"
10+
"strings"
1011

1112
"github.com/rs/zerolog"
1213
"github.com/rs/zerolog/log"
@@ -49,7 +50,18 @@ func (c *Cli) Run() error {
4950
return err
5051
}
5152

53+
cli.VersionPrinter = func(cmd *cli.Command) {
54+
version := strings.TrimPrefix(cmd.Version, "v")
55+
fmt.Println(version)
56+
}
57+
5258
cmd := &cli.Command{
59+
Name: "globstar",
60+
Usage: "The open-source static analysis toolkit",
61+
Version: version,
62+
Description: `Globstar helps you write and run custom checkers for bad and insecure patterns and run them on
63+
your codebase with a simple command. It comes with built-in checkers that you can use out-of-the-box,\
64+
or you can write your own in the .globstar directory of any repository.`,
5365
Commands: []*cli.Command{
5466
{
5567
Name: "check",

pkg/cli/version.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cli
2+
3+
// current version of globstar
4+
// this will be overridden during the build process
5+
var version = "dev"

0 commit comments

Comments
 (0)