Skip to content

Commit

Permalink
refactor: use version infos via ldflags and simplify version checking…
Browse files Browse the repository at this point in the history
… logic
  • Loading branch information
bnema committed Nov 20, 2024
1 parent 693cd2b commit b2949e4
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 120 deletions.
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ TAG := dev
DIST_DIR := ./dist
ENGINE := podman

# Version information
VERSION := $(shell git describe --tags --always --dirty)
COMMIT := $(shell git rev-parse --short HEAD)
BUILD_DATE := $(shell date -u '+%Y-%m-%d_%I:%M:%S%p')

# Build flags
LDFLAGS := -s -w \
-X main.version=$(VERSION) \
-X main.commit=$(COMMIT) \
-X main.date=$(BUILD_DATE)

# Architectures
ARCHS := amd64 arm64

Expand All @@ -19,8 +30,9 @@ build:
@echo "Building Go binaries..."
@mkdir -p $(DIST_DIR)
@rm -f $(DIST_DIR)/*
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(DIST_DIR)/gordon-linux-amd64 ./main.go
@CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $(DIST_DIR)/gordon-linux-arm64 ./main.go
@echo "Building with version $(VERSION), commit $(COMMIT), date $(BUILD_DATE)"
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o $(DIST_DIR)/gordon-linux-amd64 ./main.go
@CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="$(LDFLAGS)" -o $(DIST_DIR)/gordon-linux-arm64 ./main.go
@echo "Go binaries built successfully"

# Build and push Docker images
Expand Down Expand Up @@ -61,4 +73,4 @@ clean:
@echo "Cleaning up..."
@rm -rf $(DIST_DIR)
@$(ENGINE) system prune -f
@echo "Cleanup completed."
@echo "Cleanup completed."
20 changes: 4 additions & 16 deletions cmd/gordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ package cmd

import (
"fmt"
"regexp"
"time"

"github.com/bnema/gordon/internal/cli"
"github.com/bnema/gordon/internal/cli/cmd"
"github.com/bnema/gordon/internal/common"
"github.com/bnema/gordon/internal/server"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"

// env auto load
_ "github.com/joho/godotenv/autoload"
)

var (
build string
commit string
date string
rootCmd = &cobra.Command{Use: "gordon"}
proxyURL = "https://gordon-proxy.bamen.dev"
)
Expand All @@ -38,8 +34,7 @@ func Execute(client *cli.App, server *server.App) {
cobra.CheckErr(rootCmd.Execute())
}

func ExecuteCLI() {
build = regexp.MustCompile(`\d+\.\d+\.\d+`).FindString(build)
func ExecuteCLI(build, commit, date string) {
buildInfo := &common.BuildConfig{
BuildVersion: build,
BuildCommit: commit,
Expand All @@ -58,15 +53,8 @@ func ExecuteCLI() {

common.DockerInit(&s.Config.ContainerEngine)

// Check for new version
go func() {
msg, err := common.CheckVersionPeriodically(&s.Config)
if err != nil {
log.Warnf("Error checking for new version: %s", err)
} else if msg != "" {
log.Info(msg)
}
}()
// Start periodic version checking in the background
go common.CheckVersionPeriodically(buildInfo.BuildVersion, 3*time.Hour)

Execute(a, s)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23

require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/Masterminds/semver/v3 v3.3.1
github.com/PuerkitoBio/goquery v1.9.2
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/lipgloss v0.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkk
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
Expand Down
22 changes: 17 additions & 5 deletions internal/cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/bnema/gordon/internal/cli"
"github.com/bnema/gordon/internal/common"
"github.com/spf13/cobra"
)

Expand All @@ -12,11 +13,22 @@ func NewVersionCommand(a *cli.App) *cobra.Command {
Use: "version",
Short: "Display the version of Gordon",
Run: func(cmd *cobra.Command, args []string) {
version := a.Config.Build.BuildVersion
if version == "" {
fmt.Println("Version: devel")
} else {
fmt.Printf("Version: %s\n", version)
info := common.GetVersionInfo(
a.Config.Build.BuildVersion,
a.Config.Build.BuildCommit,
a.Config.Build.BuildDate,
)
fmt.Println(info.String())

// Check for updates
hasUpdate, latestVersion, err := common.CheckForNewVersion(info.Version)
if err != nil {
fmt.Printf("Error checking for updates: %v\n", err)
return
}

if hasUpdate {
fmt.Printf("\nNew version %s available! Visit: https://github.com/bnema/gordon/releases/latest\n", latestVersion)
}
},
}
Expand Down
5 changes: 5 additions & 0 deletions internal/common/init_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,8 @@ func (c *HttpConfig) FullDomain() string {
}
return c.Domain
}

// GetVersion
func (c *Config) GetVersion() string {
return c.Build.BuildVersion
}
Loading

0 comments on commit b2949e4

Please sign in to comment.