Skip to content

Commit

Permalink
Create version package, so it can be used outside of main
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Nov 10, 2018
1 parent f0b33d9 commit 105c32a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ builds:
- CGO_ENABLED=0
ldflags:
# gitTag set from a generated file (see ./tag_release.sh)
- -s -w -X main.builtAt={{.Date}} -X main.gitCommit={{.Commit}}
- -s -w -X github.com/weaveworks/eksctl/pkg/version.builtAt={{.Date}} -X github.com/weaveworks/eksctl/pkg/version.gitCommit={{.Commit}}
goos:
- windows
- darwin
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
built_at := $(shell date +%s)
git_commit := $(shell git describe --dirty --always)

version_pkg := github.com/weaveworks/eksctl/pkg/version

EKSCTL_BUILD_IMAGE ?= weaveworks/eksctl:build
EKSCTL_IMAGE ?= weaveworks/eksctl:latest

Expand All @@ -20,7 +22,7 @@ install-build-deps: ## Install dependencies (packages and tools)

.PHONY: build
build: ## Build eksctl
@go build -ldflags "-X main.gitCommit=$(git_commit) -X main.builtAt=$(built_at)" ./cmd/eksctl
@go build -ldflags "-X $(version_pkg).gitCommit=$(git_commit) -X $(version_pkg).builtAt=$(built_at)" ./cmd/eksctl

##@ Testing & CI

Expand Down
14 changes: 3 additions & 11 deletions cmd/eksctl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@ package main
import (
"github.com/spf13/cobra"

"github.com/weaveworks/eksctl/pkg/version"

"github.com/kubicorn/kubicorn/pkg/logger"
)

//go:generate go run version_generate.go

func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Output the version of eksctl",
Run: func(_ *cobra.Command, _ []string) {
versionInfo := map[string]string{
"builtAt": builtAt,
"gitCommit": gitCommit,
}

if gitTag != "" {
versionInfo["gitTag"] = gitTag
}

logger.Info("versionInfo = %#v", versionInfo)
logger.Info("%#v", version.Get())
},
}
}
32 changes: 0 additions & 32 deletions cmd/eksctl/version_generate.go

This file was deleted.

9 changes: 0 additions & 9 deletions cmd/eksctl/version_norelease.go

This file was deleted.

7 changes: 0 additions & 7 deletions cmd/eksctl/version_release.go

This file was deleted.

7 changes: 7 additions & 0 deletions pkg/version/norelease.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !release

package version

var (
builtAt, gitCommit, gitTag string
)
10 changes: 10 additions & 0 deletions pkg/version/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// +build release

package version

// This file was generated by release_generate.go; DO NOT EDIT.

// Values of builtAt and gitCommit will be set by the linker.
var builtAt = ""
var gitCommit = ""
var gitTag = "0.1.11"
37 changes: 37 additions & 0 deletions pkg/version/release_generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// +build ignore

package main

import (
"bytes"
"io/ioutil"
"log"
"os"

. "github.com/dave/jennifer/jen"
)

func main() {
f := NewFile("version")

f.Comment("This file was generated by release_generate.go; DO NOT EDIT.")
f.Line()

f.Comment("Values of builtAt and gitCommit will be set by the linker.")
f.Var().Id("builtAt").Op("=").Lit("")
f.Var().Id("gitCommit").Op("=").Lit("")

gitTag := os.Getenv("RELEASE_GIT_TAG")
f.Var().Id("gitTag").Op("=").Lit(gitTag)

buf := &bytes.Buffer{}
if _, err := buf.WriteString("// +build release\n\n"); err != nil {
log.Fatal(err.Error())
}
if err := f.Render(buf); err != nil {
log.Fatal(err.Error())
}
if err := ioutil.WriteFile("release.go", buf.Bytes(), 0644); err != nil {
log.Fatal(err.Error())
}
}
31 changes: 31 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package version

import (
"encoding/json"
)

//go:generate go run ./release_generate.go

// Info hold version information
type Info struct {
BuiltAt string
GitCommit string `json:",omitempty"`
GitTag string `json:",omitempty"`
}

var info = Info{
BuiltAt: builtAt,
GitCommit: gitCommit,
GitTag: gitTag,
}

// Get return version Info struct
func Get() Info { return info }

// String return version info as JSON
func String() string {
if data, err := json.Marshal(info); err == nil {
return string(data)
}
return ""
}

0 comments on commit 105c32a

Please sign in to comment.