diff --git a/.goreleaser.yml b/.goreleaser.yml index 0259664..80599f1 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -6,9 +6,7 @@ builds: - CGO_ENABLED=0 ldflags: - -s -w - - -X github.com/terraform-docs/terraform-docs/internal/version.version={{ .Version }} - - -X github.com/terraform-docs/terraform-docs/internal/version.commitHash={{ .ShortCommit }} - - -X github.com/terraform-docs/terraform-docs/internal/version.buildDate={{ .Date }} + - -X github.com/terraform-docs/terraform-docs/internal/version.commit={{ .ShortCommit }} goos: - darwin - linux diff --git a/Makefile b/Makefile index be08cc9..09e7e27 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,6 @@ LICENSE := MIT # Build variables BUILD_DIR := bin COMMIT_HASH ?= $(shell git rev-parse --short HEAD 2>/dev/null) -BUILD_DATE ?= $(shell date +%FT%T%z) CUR_VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null || git describe --tags 2>/dev/null || echo "v0.0.0-$(COMMIT_HASH)") COVERAGE_OUT := coverage.out @@ -26,9 +25,7 @@ GO_PACKAGE := github.com/$(PROJECT_OWNER)/$(PROJECT_NAME) GOOS ?= $(shell $(GO) env GOOS) GOARCH ?= $(shell $(GO) env GOARCH) -GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.version=$(CUR_VERSION) -GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.commitHash=$(COMMIT_HASH) -GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.buildDate=$(BUILD_DATE) +GOLDFLAGS += -X $(GO_PACKAGE)/internal/version.commit=$(COMMIT_HASH) GOBUILD ?= CGO_ENABLED=0 $(GO) build -ldflags="$(GOLDFLAGS)" GORUN ?= GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) run diff --git a/cmd/root.go b/cmd/root.go index 7dc3bdd..6631cae 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,10 +23,11 @@ import ( "github.com/terraform-docs/terraform-docs/cmd/pretty" "github.com/terraform-docs/terraform-docs/cmd/tfvars" "github.com/terraform-docs/terraform-docs/cmd/toml" - "github.com/terraform-docs/terraform-docs/cmd/version" + versioncmd "github.com/terraform-docs/terraform-docs/cmd/version" "github.com/terraform-docs/terraform-docs/cmd/xml" "github.com/terraform-docs/terraform-docs/cmd/yaml" "github.com/terraform-docs/terraform-docs/internal/cli" + "github.com/terraform-docs/terraform-docs/internal/version" ) // Execute adds all child commands to the root command and sets flags appropriately. @@ -98,7 +99,7 @@ func NewCommand() *cobra.Command { // other subcommands cmd.AddCommand(completion.NewCommand()) - cmd.AddCommand(version.NewCommand()) + cmd.AddCommand(versioncmd.NewCommand()) return cmd } diff --git a/cmd/version/version.go b/cmd/version/version.go index fcb88ab..e253e5c 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -26,7 +26,7 @@ func NewCommand() *cobra.Command { Use: "version", Short: "Print the version number of terraform-docs", Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("terraform-docs version %s\n", Full()) + fmt.Printf("terraform-docs version %s\n", version.Full()) plugins, err := plugin.Discover() if err != nil { return @@ -46,8 +46,3 @@ func NewCommand() *cobra.Command { } return cmd } - -// Full returns the full version of the binary -func Full() string { - return version.Full() -} diff --git a/internal/cli/run.go b/internal/cli/run.go index 3031a3f..bc4e6d7 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -90,7 +90,7 @@ func PreRunEFunc(config *Config) func(*cobra.Command, []string) error { //nolint return fmt.Errorf("unable to decode config, %w", err) } - if err := checkConstraint(config.Version, version.Short()); err != nil { + if err := checkConstraint(config.Version, version.Core()); err != nil { return err } diff --git a/internal/version/version.go b/internal/version/version.go index 360ee82..4d29dcb 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -13,46 +13,38 @@ package version import ( "fmt" "runtime" - "strings" - "time" ) // current version -const dev = "v0.13.0" - -// Provisioned by ldflags -var ( - version string - commitHash string - buildDate string +const ( + coreVersion = "0.14.0" + prerelease = "alpha" ) -// Load defaults for info variables -func init() { - if version == "" { - version = dev - } - if version == "v0.0.0-" { // building in a directory which is not a git repository - version = dev - } - if commitHash == "" { - commitHash = dev - } - if buildDate == "" { - buildDate = time.Now().Format(time.RFC3339) - } +// Provisioned by ldflags +var commit string + +// Core return the core version. +func Core() string { + return coreVersion } -// Short return the version of the binary +// Short return the version with pre-release, if available. func Short() string { - return version + v := coreVersion + + if prerelease != "" { + v += "-" + prerelease + } + + return v } -// Full return the full version of the binary including commit hash and build date +// Full return the full version including pre-release, commit hash, runtime os and arch. func Full() string { - if !strings.HasSuffix(version, commitHash) { - version += " " + commitHash + if commit != "" && commit[:1] != " " { + commit = " " + commit } - osArch := runtime.GOOS + "/" + runtime.GOARCH - return fmt.Sprintf("%s %s BuildDate: %s", version, osArch, buildDate) + + return fmt.Sprintf("v%s%s %s/%s", Short(), commit, runtime.GOOS, runtime.GOARCH) }