From da1e82c43bde003c3e11f858e4d23f6c21b61d7c Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Mon, 29 Apr 2024 10:46:26 +0200 Subject: [PATCH 1/3] Add --version command. --- justfile | 10 ++++++++-- main.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index af52fa3..377cc80 100644 --- a/justfile +++ b/justfile @@ -1,5 +1,11 @@ # https://github.com/casey/just +build: + goreleaser build --clean + +build version commitHash: + go build -ldflags "-X main.version={{version}} -X main.commit={{commitHash}}" . + release version: @[[ "{{version}}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] # ensure semantic versioning @echo "Creating a release for version {{version}}:" @@ -7,5 +13,5 @@ release version: git push origin {{version}} goreleaser release --clean - sign-last-commit: - git commit --amend --no-edit -S +sign-last-commit: + git commit --amend --no-edit -S diff --git a/main.go b/main.go index 0f87a0c..3b54e08 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,12 @@ const ( defaultLineLengthLimit = 100 * 1024 * 1024 // 100 MiB ) +var ( + // These variables are set at build time by GoReleaser. See https://goreleaser.com/cookbooks/using-main.version/ + version = "dev" + commit = "none" +) + // readFile opens the given file for reading and returns a reader and a closing function. func readFile(path string, lineLimit int) (r *bufio.Reader, closeFn func() error, err error) { if path == "" { @@ -104,11 +110,19 @@ func main() { var filePath string var trim bool var lineLengthLimit int + var versionFlag bool flag.StringVar(&filePath, "f", "", "path to the file to process") flag.BoolVar(&trim, "t", false, "trim whitespace from each line (default false)") flag.IntVar(&lineLengthLimit, "ll", defaultLineLengthLimit, "limit the length of each line being processed, ignoring any data beyond that length (values under 16 are ignored)") + flag.BoolVar(&versionFlag, "version", false, "print the version") flag.Parse() + // Print the version and exit. + if versionFlag { + fmt.Println(version, commit) + return + } + reader, closeFn, err := readFile(filePath, lineLengthLimit) if err != nil { log.Fatalf("Failed to read from file '%s'. Error: %s\n", filePath, err.Error()) From 6d54ab3852a50cd783bd111a7c0dbcf2c0b88841 Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Mon, 29 Apr 2024 11:05:36 +0200 Subject: [PATCH 2/3] Update GoReleaser. --- .goreleaser.yaml | 6 +----- .tool-versions | 2 ++ justfile | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) create mode 100644 .tool-versions diff --git a/.goreleaser.yaml b/.goreleaser.yaml index eecc0df..614cd09 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -19,14 +19,10 @@ changelog: brews: - name: unique - alternative_names: - - unique@{{ .Version }} - - unique@{{ .Major }}.{{ .Minor }} - - unique@{{ .Major }} description: "Outputs the unique lines of its input" homepage: "https://github.com/ro-tex/unique" license: "MIT" - folder: Formula + directory: Formula url_template: "https://github.com/ro-tex/unique/releases/download/{{ .Tag }}/{{ .ArtifactName }}" commit_author: name: goreleaserbot diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..bfa1c23 --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +golang 1.22.2 +goreleaser 1.25.1 diff --git a/justfile b/justfile index 377cc80..d8a909b 100644 --- a/justfile +++ b/justfile @@ -1,9 +1,9 @@ # https://github.com/casey/just build: - goreleaser build --clean + goreleaser build --clean --snapshot -build version commitHash: +buildWithVersion version commitHash: go build -ldflags "-X main.version={{version}} -X main.commit={{commitHash}}" . release version: From 0bc11af25e8d3dd380aaf6d93af8e0d38ca6423a Mon Sep 17 00:00:00 2001 From: Ivaylo Novakov Date: Mon, 29 Apr 2024 11:08:43 +0200 Subject: [PATCH 3/3] Update Changelog. --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 708a061..beb799f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `--version` command that outputs version and commit hash. + ### Changed - Switched to using v2 of xxhash library.