Skip to content

Commit

Permalink
fix versions uses across bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
OzBena committed Aug 18, 2024
1 parent fa0b7e1 commit 5a283bd
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
tags:
- 'v*.*.*'
- 'v*.*.*-rc*'
branches:
- master

permissions:
contents: write
Expand Down
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ builds:
goarch:
- amd64
- arm64
ldflags:
- -s -w -X bbox/version.version={{.Version}} -X bbox/version.commit={{.Commit}} -X bbox/version.date={{.Date}} -X bbox/version.builtBy=goreleaser

archives:
- format: tar.gz
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Thank you for your interest in contributing to BBOX! We appreciate your efforts
2. [How to Request a Feature](#how-to-request-a-feature)
3. [How to Suggest Documentation Improvements](#how-to-suggest-documentation-improvements)
4. [How to Submit a Pull Request](#how-to-submit-a-pull-request)
5. [Commit Message Convention](#commit-message-convention)
5. [Branch Naming Convention](#branch-naming-convention)
6. [Style Guide](#style-guide)
7. [Testing](#testing)
8. [Documentation](#documentation)
Expand Down Expand Up @@ -47,7 +47,7 @@ To contribute code:

1. **Fork the Repository**: Create a personal fork of the repository on GitHub.
2. **Clone Your Fork**: Clone your fork to your local machine.
3. **Create a New Branch**: Follow the branch naming convention provided in the [Brach Naming Convention](#branch-naming-convention) section.
3. **Create a New Branch**: Follow the branch naming convention provided in the [Branch Naming Convention](#branch-naming-convention) section.
4. **Make Your Changes**: Implement your changes in your branch.
5. **Run Linter**: Ensure your code passes `golangci-lint`.
6. **Test Your Changes**: Write tests for your code and ensure all tests pass.
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ Print the version number of bbox

`bbox version [flags]`

|Flags| Description|
|------|----------|
|`-d, --describe` | Return full version description|

## Contributing

Contributions are always welcome!
Expand Down
10 changes: 9 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"github.com/spf13/cobra"
)

var describeVersion bool

func init() {
versionCmd.Flags().BoolVarP(&describeVersion, "describe", "d", false, "Return full version description")
RootCmd.AddCommand(versionCmd)
}

Expand All @@ -20,6 +23,11 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of bbox",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("bbox version: %s", version.GetVersion())
if (describeVersion) {
fmt.Println("bbox version:", version.GetFormattedVersion())
return
} else {
fmt.Println("bbox version:", version.GetVersion())
}
},
}
44 changes: 11 additions & 33 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
NAME ?= bbox
PROJECT ?= gitlab.similarweb.io/infrastructure/bbox
UNFORMATTED_FILES=$(shell find . -not -path "./vendor/*" -name "*.go" | xargs gofmt -s -l)
PROJECT ?= https://github.com/similarweb/bbox
GOCMD=go
GOTEST=$(GOCMD) test
GOBUILD=$(GOCMD) build
GOARCH=amd64

.PHONY: release
release: ## Release locally all version
@goreleaser release --clean --config .goreleaser.yml

test: ## Run tests for the project
$(GOTEST) -short -race -cover -failfast ./...

.PHONY: build
build-binary: prompt-version ## Build Locally
@echo "Building locally $(VERSION)..."
$(GOBUILD) -ldflags="-X $(NAME)/version.version=$(VERSION)" -o $(NAME)_dev

fmt: ## Format Go code
@gofmt -w -s main.go $(UNFORMATTED_FILES)
$(GOTEST) ./... -v

lint:
golangci-lint run --timeout=3m

fmt-check: ## Check go code formatting
@echo "==> Checking that code complies with gofmt requirements..."
@if [ ! -z "$(UNFORMATTED_FILES)" ]; then \
echo "gofmt needs to be run on the following files:"; \
echo "$(UNFORMATTED_FILES)" | xargs -n1; \
echo "You can use the command: \`make fmt\` to reformat code."; \
exit 1; \
else \
echo "Check passed."; \
fi
lint: ## Run linter for the project
golangci-lint run

help: ## Show Help menu
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'


.PHONY: prompt-version
prompt-version:
$(eval VERSION := $(shell read -p "Enter a version number: " version && echo $$version))
.PHONY: version
version: ## Print the version of the project
$(GOCMD) run $(NAME) version

.PHONY: version describe
version-describe: ## Print the version of the project with full description
$(GOCMD) run $(NAME) version -d
31 changes: 23 additions & 8 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
package version

import "fmt"
import (
"fmt"
"os/exec"
"strings"
)

func ExecuteGitCommand(args ...string) string {
cmd := exec.Command("git", args...)
out, err := cmd.Output()
if err != nil {
return "unknown"
}
return strings.TrimSpace(string(out))
}

// Set with LDFLAGS.
var (
// Version of the release, the value injected by .goreleaser
version = `{{.Version}}`
// Version of the release, dynamically fetched from git
version = ExecuteGitCommand("describe", "--tags", "--abbrev=0")

// Commit hash of the release, the value injected by .goreleaser
commit = `{{.Commit}}`
// Commit hash of the release, dynamically fetched from git
commit = ExecuteGitCommand("rev-parse", "HEAD")

// Date of the commit, dynamically fetched from git
date = ExecuteGitCommand("show", "-s", "--format=%ci", "HEAD")
)

func GetVersion() string {
return version
}

// GetFormattedVersion returns the current version and commit hash
func GetFormattedVersion() string {
return fmt.Sprintf("%s (%s)", version, commit)
return fmt.Sprintf("%s (%s, %s)", version, commit, date)
}

0 comments on commit 5a283bd

Please sign in to comment.