Skip to content

Commit 6820b4c

Browse files
committed
Normalize version to prevent malformed error
As part of this normalization the following is made sure: - leading `v` is shown in `-v, --version` and `version` command - commit hash is shown in `-v, --version` and `version` command - build date is removed - version core (without pre-release) is used for constraint comparison Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent 08cb4f2 commit 6820b4c

File tree

6 files changed

+28
-45
lines changed

6 files changed

+28
-45
lines changed

.goreleaser.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ builds:
66
- CGO_ENABLED=0
77
ldflags:
88
- -s -w
9-
- -X github.com/terraform-docs/terraform-docs/internal/version.version={{ .Version }}
10-
- -X github.com/terraform-docs/terraform-docs/internal/version.commitHash={{ .ShortCommit }}
11-
- -X github.com/terraform-docs/terraform-docs/internal/version.buildDate={{ .Date }}
9+
- -X github.com/terraform-docs/terraform-docs/internal/version.commit={{ .ShortCommit }}
1210
goos:
1311
- darwin
1412
- linux

Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ LICENSE := MIT
1616
# Build variables
1717
BUILD_DIR := bin
1818
COMMIT_HASH ?= $(shell git rev-parse --short HEAD 2>/dev/null)
19-
BUILD_DATE ?= $(shell date +%FT%T%z)
2019
CUR_VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null || git describe --tags 2>/dev/null || echo "v0.0.0-$(COMMIT_HASH)")
2120
COVERAGE_OUT := coverage.out
2221

@@ -26,9 +25,7 @@ GO_PACKAGE := github.com/$(PROJECT_OWNER)/$(PROJECT_NAME)
2625
GOOS ?= $(shell $(GO) env GOOS)
2726
GOARCH ?= $(shell $(GO) env GOARCH)
2827

29-
GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.version=$(CUR_VERSION)
30-
GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.commitHash=$(COMMIT_HASH)
31-
GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.buildDate=$(BUILD_DATE)
28+
GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.commit=$(COMMIT_HASH)
3229

3330
GOBUILD ?= CGO_ENABLED=0 $(GO) build -ldflags="$(GOLDFLAGS)"
3431
GORUN ?= GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) run

cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import (
2323
"github.com/terraform-docs/terraform-docs/cmd/pretty"
2424
"github.com/terraform-docs/terraform-docs/cmd/tfvars"
2525
"github.com/terraform-docs/terraform-docs/cmd/toml"
26-
"github.com/terraform-docs/terraform-docs/cmd/version"
26+
versioncmd "github.com/terraform-docs/terraform-docs/cmd/version"
2727
"github.com/terraform-docs/terraform-docs/cmd/xml"
2828
"github.com/terraform-docs/terraform-docs/cmd/yaml"
2929
"github.com/terraform-docs/terraform-docs/internal/cli"
30+
"github.com/terraform-docs/terraform-docs/internal/version"
3031
)
3132

3233
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -98,7 +99,7 @@ func NewCommand() *cobra.Command {
9899

99100
// other subcommands
100101
cmd.AddCommand(completion.NewCommand())
101-
cmd.AddCommand(version.NewCommand())
102+
cmd.AddCommand(versioncmd.NewCommand())
102103

103104
return cmd
104105
}

cmd/version/version.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewCommand() *cobra.Command {
2626
Use: "version",
2727
Short: "Print the version number of terraform-docs",
2828
Run: func(cmd *cobra.Command, args []string) {
29-
fmt.Printf("terraform-docs version %s\n", Full())
29+
fmt.Printf("terraform-docs version %s\n", version.Full())
3030
plugins, err := plugin.Discover()
3131
if err != nil {
3232
return
@@ -46,8 +46,3 @@ func NewCommand() *cobra.Command {
4646
}
4747
return cmd
4848
}
49-
50-
// Full returns the full version of the binary
51-
func Full() string {
52-
return version.Full()
53-
}

internal/cli/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func PreRunEFunc(config *Config) func(*cobra.Command, []string) error { //nolint
9090
return fmt.Errorf("unable to decode config, %w", err)
9191
}
9292

93-
if err := checkConstraint(config.Version, version.Short()); err != nil {
93+
if err := checkConstraint(config.Version, version.Core()); err != nil {
9494
return err
9595
}
9696

internal/version/version.go

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,38 @@ package version
1313
import (
1414
"fmt"
1515
"runtime"
16-
"strings"
17-
"time"
1816
)
1917

2018
// current version
21-
const dev = "v0.13.0"
19+
const (
20+
coreVersion = "0.14.0"
21+
prerelease = "alpha"
22+
)
2223

2324
// Provisioned by ldflags
24-
var (
25-
version string
26-
commitHash string
27-
buildDate string
28-
)
25+
var commit string
2926

30-
// Load defaults for info variables
31-
func init() {
32-
if version == "" {
33-
version = dev
34-
}
35-
if version == "v0.0.0-" { // building in a directory which is not a git repository
36-
version = dev
37-
}
38-
if commitHash == "" {
39-
commitHash = dev
40-
}
41-
if buildDate == "" {
42-
buildDate = time.Now().Format(time.RFC3339)
43-
}
27+
// Core return the core version.
28+
func Core() string {
29+
return coreVersion
4430
}
4531

46-
// Short return the version of the binary
32+
// Short return the version with pre-release, if available.
4733
func Short() string {
48-
return version
34+
v := coreVersion
35+
36+
if prerelease != "" {
37+
v += "-" + prerelease
38+
}
39+
40+
return v
4941
}
5042

51-
// Full return the full version of the binary including commit hash and build date
43+
// Full return the full version including pre-release, commit hash, runtime os and arch.
5244
func Full() string {
53-
if !strings.HasSuffix(version, commitHash) {
54-
version += " " + commitHash
45+
if commit != "" && commit[:1] != " " {
46+
commit = " " + commit
5547
}
56-
osArch := runtime.GOOS + "/" + runtime.GOARCH
57-
return fmt.Sprintf("%s %s BuildDate: %s", version, osArch, buildDate)
48+
49+
return fmt.Sprintf("v%s%s %s/%s", Short(), commit, runtime.GOOS, runtime.GOARCH)
5850
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy