Skip to content

Commit

Permalink
Improve Bazel-based version determiniation
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Mar 18, 2024
1 parent 3d89e92 commit 45fcee6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
8 changes: 7 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ gazelle(name = "gazelle")

go_library(
name = "aws-in-a-box_lib",
srcs = ["main.go"],
srcs = [
"main.go",
"version.go",
],
importpath = "aws-in-a-box",
visibility = ["//visibility:private"],
deps = [
Expand All @@ -18,6 +21,9 @@ go_library(
"//services/s3",
"//services/sqs",
],
x_defs = {
"aws-in-a-box.BazelSuffix": " (Bazel)"
},
)

go_binary(
Expand Down
40 changes: 17 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ import (
"aws-in-a-box/services/sqs"
)

var BazelSuffix = ""

func versionString() string {
buildinfo, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
revision := "unknown"
dirty := false
for _, s := range buildinfo.Settings {
if s.Key == "vcs.modified" {
dirty = s.Value == "true"
} else if s.Key == "vcs.revision" {
revision = s.Value
version := Version + BazelSuffix

// We only have ReadBuildInfo in non-bazel builds.
if version != Version {
buildinfo, ok := debug.ReadBuildInfo()
if ok {
for _, s := range buildinfo.Settings {
if s.Key == "vcs.modified" {
version += " (dirty)"
break
}
}
}
}
if dirty {
revision += "(dirty)"
}
return revision

return version
}

func main() {
Expand Down Expand Up @@ -84,14 +85,7 @@ func main() {
textHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})
logger := slog.New(textHandler)

version := versionString()
if version == "" {
logger.Warn("Could not read build info")
} else {
logger = logger.With("version", versionString())
}
logger := slog.New(textHandler).With("version", versionString())

methodRegistry := make(http.Registry)

Expand Down
5 changes: 4 additions & 1 deletion release.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
echo "package main\n\nconst Version = \"$VERSION\"" > version.go
git add .
git commit -m "Update version file to $VERSION"
git tag -a $VERSION -m "$VERSION"
git push origin $VERSION
git push origin master $VERSION
3 changes: 3 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

const Version = "v0.0"

0 comments on commit 45fcee6

Please sign in to comment.