Skip to content

Commit dd3af9b

Browse files
authored
Merge pull request #170 from blinklabs-io/feat/log-version
feat: log version
2 parents de546fc + 31b2c18 commit dd3af9b

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ ROOT_DIR=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
66
# Gather all .go files for use in dependencies below
77
GO_FILES=$(shell find $(ROOT_DIR) -name '*.go')
88

9+
# Extract Go module name from go.mod
10+
GOMODULE=$(shell grep ^module $(ROOT_DIR)/go.mod | awk '{ print $$2 }')
11+
12+
# Set version strings based on git tag and current ref
13+
GO_LDFLAGS=-ldflags "-s -w -X '$(GOMODULE)/internal/version.Version=$(shell git describe --tags --exact-match 2>/dev/null)' -X '$(GOMODULE)/internal/version.CommitHash=$(shell git rev-parse --short HEAD)'"
14+
915
mod-tidy:
1016
go mod tidy
1117

1218
# Build our program binary
1319
# Depends on GO_FILES to determine when rebuild is needed
1420
$(BINARY): mod-tidy $(GO_FILES)
15-
# Needed to fetch new dependencies and add them to go.mod
16-
go build -o $(BINARY) ./cmd/$(BINARY)
21+
go build \
22+
$(GO_LDFLAGS) \
23+
-o $(BINARY) \
24+
./cmd/$(BINARY)
1725

1826
.PHONY: build clean image mod-tidy
1927

cmd/tx-submit-api/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ package main
1717
import (
1818
"flag"
1919
"fmt"
20-
"github.com/blinklabs-io/tx-submit-api/internal/api"
21-
"github.com/blinklabs-io/tx-submit-api/internal/config"
22-
"github.com/blinklabs-io/tx-submit-api/internal/logging"
2320
"net/http"
2421
_ "net/http/pprof"
2522
"os"
23+
24+
"github.com/blinklabs-io/tx-submit-api/internal/api"
25+
"github.com/blinklabs-io/tx-submit-api/internal/config"
26+
"github.com/blinklabs-io/tx-submit-api/internal/logging"
27+
"github.com/blinklabs-io/tx-submit-api/internal/version"
2628
)
2729

2830
var cmdlineFlags struct {
@@ -57,6 +59,8 @@ func main() {
5759
}
5860
}()
5961

62+
logger.Infof("starting tx-submit-api %s", version.GetVersionString())
63+
6064
// Start debug listener
6165
if cfg.Debug.ListenPort > 0 {
6266
logger.Infof(

internal/version/version.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2023 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"fmt"
19+
)
20+
21+
// These are populated at build time
22+
var Version string
23+
var CommitHash string
24+
25+
func GetVersionString() string {
26+
if Version != "" {
27+
return fmt.Sprintf("%s (commit %s)", Version, CommitHash)
28+
} else {
29+
return fmt.Sprintf("devel (commit %s)", CommitHash)
30+
}
31+
}

0 commit comments

Comments
 (0)