Skip to content

Commit

Permalink
Merge pull request #77 from blinklabs-io/feat/version
Browse files Browse the repository at this point in the history
feat: version
  • Loading branch information
wolf31o2 authored Sep 12, 2023
2 parents e6697fa + beead91 commit d846a0a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ GO_FILES=$(shell find $(ROOT_DIR) -name '*.go')
# Gather list of expected binaries
BINARIES=nview

# Extract Go module name from go.mod
GOMODULE=$(shell grep ^module $(ROOT_DIR)/go.mod | awk '{ print $$2 }')

# Set version strings based on git tag and current ref
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)'"

.PHONY: build mod-tidy clean test

all: format build
Expand All @@ -30,4 +36,6 @@ test: tidy
# Build our program binaries
# Depends on GO_FILES to determine when rebuild is needed
$(BINARIES): mod-tidy $(GO_FILES)
go build -ldflags="-s -w" -o $(@) .
go build \
$(GO_LDFLAGS) \
-o $(@) .
31 changes: 31 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 Blink Labs, LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package version

import (
"fmt"
)

// These are populated at build time
var Version string
var CommitHash string

func GetVersionString() string {
if Version != "" {
return fmt.Sprintf("%s (commit %s)", Version, CommitHash)
} else {
return fmt.Sprintf("devel (commit %s)", CommitHash)
}
}
41 changes: 32 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"github.com/rivo/tview"
"github.com/shirou/gopsutil/v3/process"
terminal "golang.org/x/term"

"github.com/blinklabs-io/nview/internal/version"
)

// Global command line flags
Expand Down Expand Up @@ -298,10 +300,17 @@ func getTestText(ctx context.Context, promMetrics *PromMetrics) string {

var sb strings.Builder

// Style / UI
var width = 71

var twoColWidth int = (width - 3) / 2
var twoColSecond int = twoColWidth + 2

// Main section
uptime := timeLeft(uptimes)
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20)))
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime))
sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString()))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1)))

// Epoch progress
var epochProgress float32
Expand Down Expand Up @@ -470,8 +479,9 @@ func getHomeText(ctx context.Context, promMetrics *PromMetrics) string {

// Main section
uptime := timeLeft(uptimes)
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20)))
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime))
sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString()))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1)))

// Epoch progress
var epochProgress float32
Expand Down Expand Up @@ -860,10 +870,17 @@ func getInfoText(ctx context.Context) string {

var sb strings.Builder

// Style / UI
var width = 71

var twoColWidth int = (width - 3) / 2
var twoColSecond int = twoColWidth + 2

// Main section
uptime := timeLeft(uptimes)
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20)))
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime))
sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString()))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1)))

if showPeers {
sb.WriteString(fmt.Sprintf(
Expand Down Expand Up @@ -919,10 +936,17 @@ func getPeerText(ctx context.Context) string {

var sb strings.Builder

// Style / UI
var width = 71

var twoColWidth int = (width - 3) / 2
var twoColSecond int = twoColWidth + 2

// Main section
uptime := timeLeft(uptimes)
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%s[white]\n", uptime))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", 20)))
sb.WriteString(fmt.Sprintf(" Uptime: [blue]%-"+strconv.Itoa(twoColSecond-9-len(uptime))+"s[white]", uptime))
sb.WriteString(fmt.Sprintf(" nview Version: [blue]%-"+strconv.Itoa(twoColWidth)+"s[white]\n", version.GetVersionString()))
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width+1)))

// bail on FreeBSD due to missing connections support
if runtime.GOOS == "freebsd" {
Expand Down Expand Up @@ -1024,7 +1048,6 @@ func getPeerText(ctx context.Context) string {
charMarked = string('▌')
charUnmarked = string('▖')
}
width := 71
granularity := width - 3
granularitySmall := granularity / 2
if checkPeers {
Expand Down

0 comments on commit d846a0a

Please sign in to comment.