Skip to content

Commit e7dde36

Browse files
feature: Add version command
1 parent 5448b24 commit e7dde36

File tree

6 files changed

+104
-6
lines changed

6 files changed

+104
-6
lines changed

.github/workflows/go.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@ jobs:
99
steps:
1010
- name: Checkout
1111
uses: actions/checkout@v4
12+
with:
13+
fetch-depth: 0 # Needed for git history
1214
- name: Set up Go
1315
uses: actions/setup-go@v4
16+
- name: Set version info
17+
id: version
18+
run: |
19+
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "development")
20+
COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
21+
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
22+
echo "VERSION=${VERSION}" >> $GITHUB_ENV
23+
echo "COMMIT=${COMMIT}" >> $GITHUB_ENV
24+
echo "BUILD_TIME=${BUILD_TIME}" >> $GITHUB_ENV
25+
echo "LDFLAGS=-X 'codacy/cli-v2/version.Version=${VERSION}' -X 'codacy/cli-v2/version.GitCommit=${COMMIT}' -X 'codacy/cli-v2/version.BuildTime=${BUILD_TIME}'" >> $GITHUB_ENV
1426
- name: Build CLI for Linux
1527
run: |
16-
GOOS=linux GOARCH=amd64 go build -o cli-v2-linux ./cli-v2.go
28+
GOOS=linux GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o cli-v2-linux ./cli-v2.go
1729
- name: Build CLI for Windows
1830
run: |
19-
GOOS=windows GOARCH=amd64 go build -o cli-v2.exe ./cli-v2.go
31+
GOOS=windows GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o cli-v2.exe ./cli-v2.go
2032
- name: Build CLI for macOS
2133
run: |
22-
GOOS=darwin GOARCH=amd64 go build -o cli-v2-macos ./cli-v2.go
34+
GOOS=darwin GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o cli-v2-macos ./cli-v2.go
2335
- name: Upload CLI binaries
2436
uses: actions/upload-artifact@v4
2537
with:

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.PHONY: build clean
2+
3+
# Get the version from git describe or fallback to a default version
4+
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "development")
5+
COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
6+
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
7+
8+
# Build flags
9+
LDFLAGS := -X 'codacy/cli-v2/version.Version=$(VERSION)' -X 'codacy/cli-v2/version.GitCommit=$(COMMIT)' -X 'codacy/cli-v2/version.BuildTime=$(BUILD_TIME)'
10+
11+
# Build the CLI
12+
build:
13+
go build -ldflags "$(LDFLAGS)" -o cli-v2
14+
15+
# Clean build artifacts
16+
clean:
17+
rm -f cli-v2
18+
19+
# Build for all platforms
20+
build-all: clean
21+
GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o cli-v2_linux_amd64
22+
GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o cli-v2_darwin_amd64
23+
GOOS=windows GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o cli-v2_windows_amd64.exe

cli-v2.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"codacy/cli-v2/config"
66
config_file "codacy/cli-v2/config-file"
77
"codacy/cli-v2/utils/logger"
8+
"codacy/cli-v2/version"
89
"fmt"
910
"os"
1011
"path/filepath"
@@ -22,8 +23,10 @@ func main() {
2223
fmt.Printf("Failed to initialize logger: %v\n", err)
2324
}
2425

25-
// Log startup message
26-
logger.Debug("Starting Codacy CLI.", logrus.Fields{})
26+
// Log startup message and version
27+
logger.Info("Starting Codacy CLI", logrus.Fields{
28+
"version": version.GetVersion(),
29+
})
2730

2831
// This also setup the config global !
2932
configErr := config_file.ReadConfigFile(config.Config.ProjectConfigFile())

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77

88
"codacy/cli-v2/config"
99
"codacy/cli-v2/utils/logger"
10+
"codacy/cli-v2/version"
1011

1112
"github.com/fatih/color"
1213
"github.com/spf13/cobra"
1314
)
1415

1516
var rootCmd = &cobra.Command{
1617
Use: "codacy-cli",
17-
Short: "Codacy CLI - A command line interface for Codacy",
18+
Short: fmt.Sprintf("Codacy CLI v%s - A command line interface for Codacy", version.GetVersion()),
1819
Long: "",
1920
Example: getExampleText(),
2021
PersistentPreRun: func(cmd *cobra.Command, args []string) {

cmd/version.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"codacy/cli-v2/version"
5+
"fmt"
6+
"runtime"
7+
8+
"github.com/fatih/color"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var versionCmd = &cobra.Command{
13+
Use: "version",
14+
Short: "Display version information",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
bold := color.New(color.Bold)
17+
cyan := color.New(color.FgCyan)
18+
19+
fmt.Println()
20+
bold.Println("Codacy CLI Version Information")
21+
fmt.Println("-----------------------------")
22+
cyan.Printf("Version: ")
23+
fmt.Println(version.Version)
24+
cyan.Printf("Commit: ")
25+
fmt.Println(version.GitCommit)
26+
cyan.Printf("Built: ")
27+
fmt.Println(version.BuildTime)
28+
cyan.Printf("Go version: ")
29+
fmt.Println(runtime.Version())
30+
cyan.Printf("OS/Arch: ")
31+
fmt.Printf("%s/%s\n", runtime.GOOS, runtime.GOARCH)
32+
fmt.Println()
33+
},
34+
}
35+
36+
func init() {
37+
rootCmd.AddCommand(versionCmd)
38+
}

version/version.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package version
2+
3+
// Version information set at build time
4+
var (
5+
// Version is the current version of codacy-cli
6+
Version = "development"
7+
8+
// GitCommit is the git commit hash of the build
9+
GitCommit = "unknown"
10+
11+
// BuildTime is the time the binary was built
12+
BuildTime = "unknown"
13+
)
14+
15+
// GetVersion returns the current version of codacy-cli
16+
func GetVersion() string {
17+
if GitCommit != "unknown" {
18+
return Version + " (" + GitCommit + ") built at " + BuildTime
19+
}
20+
return Version
21+
}

0 commit comments

Comments
 (0)