Skip to content

Commit

Permalink
add displaying version number
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrSaber committed Oct 14, 2024
1 parent fb803c4 commit b51e36b
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.github
.git
.vscode
build
testing
dist
testing
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
build
testing
sample-config.yaml
sample-config.yaml
3 changes: 3 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ builds:
env:
- CGO_ENABLED=0

ldflags:
- "-s -w -X main.version={{.Tag}}"

# GOOS list to build for.
# For more info refer to: https://golang.org/doc/install/source#environment
goos:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN go mod download

COPY ./ ./

RUN go build -o /redirector src/main.go
RUN go build --ldflags '-s -w -X main.version=$(git describe --tags)' -o /redirector src/main.go


# Deploy stage
Expand Down
3 changes: 3 additions & 0 deletions src/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/AmrSaber/redirector/src/config"
"github.com/AmrSaber/redirector/src/lib/logger"
"github.com/AmrSaber/redirector/src/servers"
"github.com/AmrSaber/redirector/src/utils"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -37,6 +38,8 @@ var StartCommand = &cli.Command{
},
},
Action: func(c *cli.Context) error {
logger.Std.Printf("Starting redirector %s\n", utils.GetVersion())

// Runtime context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
20 changes: 20 additions & 0 deletions src/commands/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package commands

import (
"github.com/AmrSaber/redirector/src/lib/logger"
"github.com/AmrSaber/redirector/src/utils"
"github.com/urfave/cli/v2"
)

var VersionCommand = &cli.Command{
Name: "version",
Usage: "print redirector version",
Action: func(c *cli.Context) error {
logger.ResetLoggersFlags()

version := utils.GetVersion()
logger.Std.Println(version)

return nil
},
}
7 changes: 7 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import (

"github.com/AmrSaber/redirector/src/commands"
"github.com/AmrSaber/redirector/src/lib/logger"
"github.com/AmrSaber/redirector/src/utils"
"github.com/urfave/cli/v2"
)

var version string

func main() {
// Set version number if it's loaded
utils.SetVersion(version)

app := &cli.App{
Name: "redirector",
Usage: "Redirects requests to different servers",
Expand All @@ -17,6 +23,7 @@ func main() {
commands.StartCommand,
commands.PingCommand,
commands.StopCommand,
commands.VersionCommand,
},
}

Expand Down
48 changes: 48 additions & 0 deletions src/utils/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils

import (
"bytes"
"os"
"os/exec"
)

var version string

func SetVersion(v string) {
version = v
}

func GetVersion() string {
if version != "" {
return version
}

gitVersion := getVersionFromGit()
if gitVersion != "" {
return gitVersion
}

return "??"
}

func getVersionFromGit() string {
if _, err := os.Stat(".git"); err != nil {
return ""
}

lastTag, err := exec.Command("git", "describe", "--tags", "--abbrev=0").Output()
if err != nil {
return ""
}

lastTag = bytes.TrimSpace(lastTag)

tagCommitHash, _ := exec.Command("git", "show", string(lastTag), `--pretty=format:"%h"`, "--no-patch").Output()
lastCommitHash, _ := exec.Command("git", "show", `--pretty=format:"%h"`, "--no-patch").Output()

if !bytes.Equal(tagCommitHash, lastCommitHash) {
lastTag = append(lastTag, '+')
}

return string(lastTag)
}

0 comments on commit b51e36b

Please sign in to comment.