Skip to content

Commit 14aff9e

Browse files
authored
feat(main): Add version flag to display application version (#45)
* feat(main): Add version flag to display application version #41 Signed-off-by: Mert Şişmanoğlu <[email protected]> * feat(goreleaser): Add versioning support and static build flags - s - w - extldflags static - X main.Version = APP_VERSION Signed-off-by: Mert Şişmanoğlu <[email protected]> --------- Signed-off-by: Mert Şişmanoğlu <[email protected]>
1 parent 93122c5 commit 14aff9e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

.goreleaser.yml

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ project_name: capture
44
builds:
55
- id: capture
66
main: ./cmd/capture
7+
ldflags:
8+
- -s -w
9+
- -extldflags "-static"
10+
- -X "main.Version={{.Version}}"
711
env:
812
- CGO_ENABLED=0
913
goos:
@@ -34,6 +38,7 @@ kos:
3438
ldflags:
3539
- -s -w
3640
- -extldflags "-static"
41+
- -X "main.Version={{.Version}}"
3742
creation_time: "{{.CommitTimestamp}}"
3843
sbom: spdx
3944
env:

cmd/capture/main.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"context"
5+
"flag"
6+
"fmt"
57
"log"
68
"net/http"
79
"os"
@@ -15,12 +17,26 @@ import (
1517
"github.com/gin-gonic/gin"
1618
)
1719

18-
var appConfig = config.NewConfig(
19-
os.Getenv("PORT"),
20-
os.Getenv("API_SECRET"),
21-
)
20+
var appConfig *config.Config
21+
22+
var Version = "develop" // This will be set during compile time using go build ldflags
2223

2324
func main() {
25+
showVersion := flag.Bool("version", false, "Display the version of the capture")
26+
flag.Parse()
27+
28+
// Check if the version flag is provided
29+
if *showVersion {
30+
fmt.Printf("Capture version: %s\n", Version)
31+
os.Exit(0)
32+
}
33+
34+
appConfig = config.NewConfig(
35+
os.Getenv("PORT"),
36+
os.Getenv("API_SECRET"),
37+
)
38+
39+
// Initialize the Gin with default middlewares
2440
r := gin.Default()
2541
apiV1 := r.Group("/api/v1")
2642
apiV1.Use(middleware.AuthRequired(appConfig.APISecret))

0 commit comments

Comments
 (0)