Skip to content

Commit

Permalink
Create automated versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
96malhar committed Dec 11, 2023
1 parent a81f90b commit 9b6ac72
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"expvar"
"flag"
"fmt"
"github.com/96malhar/greenlight/internal/data"
"github.com/96malhar/greenlight/internal/email"
"github.com/96malhar/greenlight/internal/vcs"
"github.com/jackc/pgx/v5/pgxpool"
"log/slog"
"os"
Expand All @@ -15,7 +17,7 @@ import (
"time"
)

const version = "1.0.0"
var version = vcs.Version()

type config struct {
port int
Expand Down Expand Up @@ -122,10 +124,18 @@ func parseConfig() config {
return nil
})

// Create a new version boolean flag with the default value of false.
displayVersion := flag.Bool("version", false, "Display version and exit")

cfg.publishMetrics = true

flag.Parse()

if *displayVersion {
fmt.Printf("Version:\t%s\n", version)
os.Exit(0)
}

return cfg
}

Expand Down
31 changes: 31 additions & 0 deletions internal/vcs/vcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package vcs

import (
"fmt"
"runtime/debug"
)

func Version() string {
var revision string
var modified bool

bi, ok := debug.ReadBuildInfo()
if ok {
for _, s := range bi.Settings {
switch s.Key {
case "vcs.revision":
revision = s.Value
case "vcs.modified":
if s.Value == "true" {
modified = true
}
}
}
}

if modified {
return fmt.Sprintf("%s-dirty", revision)
}

return revision
}
10 changes: 9 additions & 1 deletion smoke_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ sleep 2
# Make a GET request to the server and check the response
RESPONSE=$(curl http://localhost:4000/v1/healthcheck)

# Get the last git commit hash
VERSION=$(git rev-parse HEAD)

# check if there are any non committed changes
if [[ $(git status -s) ]]; then
VERSION="$VERSION-dirty"
fi

# Check if the response contains the expected string
EXPECTED='{"status":"available","system_info":{"environment":"development","version":"1.0.0"}}'
EXPECTED="{\"status\":\"available\",\"system_info\":{\"environment\":\"development\",\"version\":\"$VERSION\"}}"
if [[ $RESPONSE != "$EXPECTED" ]]; then
echo "Smoke test failed. Server did not respond as expected."
echo "Expected: $EXPECTED"
Expand Down

0 comments on commit 9b6ac72

Please sign in to comment.