Skip to content

Commit 7d63ff8

Browse files
authored
version: Extend output to include ECE API Version (#53)
Extends the `ecctl version` output to include a structured output and a series of information which was missing: ```console $ ./bin/ecctl version Version: 1.0.0-rc1-dev Client API Version: 2.4.2 Go version: go1.13.4 Git commit: 8d72808 Built: Fri 15 Nov 09:24:14 2019 OS/Arch: darwin / amd64 ``` Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent 8d72808 commit 7d63ff8

File tree

8 files changed

+174
-36
lines changed

8 files changed

+174
-36
lines changed

build/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CURRENT_ARCH := $(shell uname -s | tr '[:upper:]' '[:lower:]')
66
export OWNER := elastic
77
export REPO := ecctl
88

9-
DEFAULT_LDFLAGS ?= -X main.version=$(VERSION)-dev -X main.commit=$(shell git rev-parse HEAD) -X main.owner=$(OWNER) -X main.repo=$(REPO)
9+
DEFAULT_LDFLAGS ?= -X main.version=$(VERSION)-dev -X main.commit=$(shell git rev-parse HEAD) -X main.owner=$(OWNER) -X main.repo=$(REPO) -X main.built=$(shell date -u +%a_%d_%b_%H:%M:%S_%Y)
1010

1111
REPORT_PATH ?= reports
1212
TEST_UNIT_FLAGS ?= -timeout 10s -p 4 -race -cover -coverprofile=$(REPORT_PATH)/c.out

cmd/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ var (
5454
)
5555

5656
var (
57-
version, commit, owner, repo string
58-
excludedApplicationCommands = []string{
57+
versionInfo ecctl.VersionInfo
58+
excludedApplicationCommands = []string{
5959
"help", "version", "generate", "docs", "completions", "init",
6060
}
6161
messageErrHasNoPreRunCheck = "command %s/%s has no PreRunE check set"
@@ -81,11 +81,11 @@ var RootCmd = &cobra.Command{
8181
// Execute adds all child commands to the root command sets flags appropriately.
8282
// This is called by main.main(). It only needs to happen once to the rootCmd.
8383
// It returns the statuscode to be used by os.Exit.
84-
func Execute(v, c, o, r string) int {
84+
func Execute(v ecctl.VersionInfo) int {
8585
defer stopDebug(defaultViper)
8686

8787
populateValidArgs(RootCmd)
88-
version, commit, owner, repo = v, c, o, r
88+
versionInfo = v
8989

9090
if err := RootCmd.Execute(); err != nil {
9191
fmt.Fprintln(RootCmd.OutOrStderr(), err)

cmd/version.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ package cmd
1919

2020
import (
2121
"fmt"
22+
"io"
2223
"os"
2324
"runtime"
2425

2526
"github.com/elastic/uptd"
2627
"github.com/pkg/errors"
2728
"github.com/spf13/cobra"
29+
30+
"github.com/elastic/ecctl/pkg/ecctl"
2831
)
2932

3033
const (
@@ -44,34 +47,9 @@ var versionCmd = &cobra.Command{
4447
Short: "Shows ecctl version",
4548
PreRunE: cobra.MaximumNArgs(0),
4649
RunE: func(cmd *cobra.Command, args []string) error {
47-
fmt.Printf("%s %s (build %s)\n", RootCmd.Use, version, commit)
48-
49-
githubUpdateProvider, err := uptd.NewGithubProvider(owner, repo, githubToken())
50-
if err != nil {
51-
fmt.Fprintln(cmd.OutOrStdout(), errors.Wrap(err, errWrapError.Error()))
52-
return nil
53-
}
50+
fmt.Fprint(cmd.OutOrStdout(), versionInfo)
5451

55-
uptodate, err := uptd.New(githubUpdateProvider, version)
56-
if err != nil {
57-
fmt.Fprintln(cmd.OutOrStdout(), errors.Wrap(err, errWrapError.Error()))
58-
return nil
59-
}
60-
61-
res, err := uptodate.Check()
62-
if err != nil {
63-
fmt.Fprintln(cmd.OutOrStdout(), errors.Wrap(err, errWrapError.Error()))
64-
return nil
65-
}
66-
67-
if res.NeedsUpdate {
68-
var message = fmt.Sprintf(updateFmt, RootCmd.Name(),
69-
res.Latest.Version.String(), res.Latest.URL,
70-
)
71-
fmt.Fprintln(cmd.OutOrStdout(), message)
72-
}
73-
74-
return nil
52+
return checkUpdate(versionInfo, cmd.OutOrStderr())
7553
},
7654
}
7755

@@ -88,6 +66,36 @@ func githubToken() string {
8866
return ""
8967
}
9068

69+
func checkUpdate(version ecctl.VersionInfo, device io.Writer) error {
70+
githubUpdateProvider, err := uptd.NewGithubProvider(
71+
version.Organization, version.Repository, githubToken(),
72+
)
73+
if err != nil {
74+
fmt.Fprintln(device, errors.Wrap(err, errWrapError.Error()))
75+
return nil
76+
}
77+
78+
uptodate, err := uptd.New(githubUpdateProvider, version.Version)
79+
if err != nil {
80+
fmt.Fprintln(device, errors.Wrap(err, errWrapError.Error()))
81+
return nil
82+
}
83+
84+
res, err := uptodate.Check()
85+
if err != nil {
86+
fmt.Fprintln(device, errors.Wrap(err, errWrapError.Error()))
87+
return nil
88+
}
89+
90+
if res.NeedsUpdate {
91+
var message = fmt.Sprintf(updateFmt, RootCmd.Name(),
92+
res.Latest.Version.String(), res.Latest.URL,
93+
)
94+
fmt.Fprintln(device, message)
95+
}
96+
return nil
97+
}
98+
9199
func init() {
92100
RootCmd.AddCommand(versionCmd)
93101
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/chzyer/logex v1.1.10 // indirect
99
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
1010
github.com/davecgh/go-spew v1.1.1
11-
github.com/elastic/cloud-sdk-go v1.0.0-bc4
11+
github.com/elastic/cloud-sdk-go v1.0.0-bc5
1212
github.com/elastic/uptd v1.0.0
1313
github.com/ghodss/yaml v1.0.0
1414
github.com/go-openapi/runtime v0.19.8

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
5151
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
5252
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
5353
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
54-
github.com/elastic/cloud-sdk-go v1.0.0-bc4 h1:8Gc/gFos+dJv992c8FQ06X9Mx080p2ceuiB1dUPWeUA=
55-
github.com/elastic/cloud-sdk-go v1.0.0-bc4/go.mod h1:19tuRaJglGTECDJfiWEDMOVj23ZL+9+YYoIV1fZIGzc=
54+
github.com/elastic/cloud-sdk-go v1.0.0-bc5 h1:POlV6bafKwT728xJy+did8Q1QlDweE0z/lVfgRY6ic4=
55+
github.com/elastic/cloud-sdk-go v1.0.0-bc5/go.mod h1:19tuRaJglGTECDJfiWEDMOVj23ZL+9+YYoIV1fZIGzc=
5656
github.com/elastic/uptd v1.0.0 h1:oUhbbTK6hjFYB5w5dwjo1HtbqrWyLiqj+6Sb05oawU8=
5757
github.com/elastic/uptd v1.0.0/go.mod h1:2Pm07gLal/a/gTPq3el2QgOjoxu97pB2I17AprXxa48=
5858
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=

main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,29 @@ package main
2020
import (
2121
"os"
2222

23+
"github.com/elastic/cloud-sdk-go/pkg/api"
24+
2325
"github.com/elastic/ecctl/cmd"
26+
"github.com/elastic/ecctl/pkg/ecctl"
2427
)
2528

2629
var (
2730
version string
2831
commit string
2932
owner string
3033
repo string
34+
built string
3135
)
3236

3337
func main() {
3438
os.Exit(
35-
cmd.Execute(version, commit, owner, repo),
39+
cmd.Execute(ecctl.VersionInfo{
40+
Version: version,
41+
Commit: commit,
42+
APIVersion: api.Version,
43+
Built: built,
44+
Organization: owner,
45+
Repository: repo,
46+
}),
3647
)
3748
}

pkg/ecctl/version.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package ecctl
19+
20+
import (
21+
"bytes"
22+
"fmt"
23+
"runtime"
24+
"strings"
25+
"text/tabwriter"
26+
)
27+
28+
// VersionInfo contains detailed information about the binary.
29+
type VersionInfo struct {
30+
Version string
31+
APIVersion string
32+
Commit string
33+
Built string
34+
35+
// Non Displayed fields
36+
Repository string
37+
Organization string
38+
}
39+
40+
func (v VersionInfo) String() string {
41+
buf := new(bytes.Buffer)
42+
43+
w := tabwriter.NewWriter(buf, 2, 2, 3, ' ', 0)
44+
45+
var commit = v.Commit
46+
if len(commit) >= 8 {
47+
commit = commit[:8]
48+
}
49+
50+
fmt.Fprintln(w, "Version:\t", v.Version)
51+
fmt.Fprintln(w, "Client API Version:\t", v.APIVersion)
52+
fmt.Fprintln(w, "Go version:\t", runtime.Version())
53+
fmt.Fprintln(w, "Git commit:\t", commit)
54+
fmt.Fprintln(w, "Built:\t", strings.ReplaceAll(v.Built, "_", " "))
55+
fmt.Fprintln(w, "OS/Arch:\t", runtime.GOOS, "/", runtime.GOARCH)
56+
57+
w.Flush()
58+
59+
return buf.String()
60+
}

pkg/ecctl/version_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package ecctl
19+
20+
import (
21+
"fmt"
22+
"runtime"
23+
"testing"
24+
)
25+
26+
const vTemplate = `Version: 1.0.0
27+
Client API Version: 2.4.2
28+
Go version: %s
29+
Git commit: 12345678
30+
Built: Fri 15 Nov 09:24:14 2019
31+
OS/Arch: %s / %s
32+
`
33+
34+
func TestVersionInfo_String(t *testing.T) {
35+
tests := []struct {
36+
name string
37+
fields VersionInfo
38+
want string
39+
}{
40+
{
41+
name: "Prints the version",
42+
fields: VersionInfo{
43+
Version: "1.0.0",
44+
APIVersion: "2.4.2",
45+
Commit: "12345678910",
46+
Built: "Fri_15_Nov_09:24:14_2019",
47+
},
48+
want: fmt.Sprintf(vTemplate, runtime.Version(), runtime.GOOS, runtime.GOARCH),
49+
},
50+
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
v := tt.fields
54+
if got := v.String(); got != tt.want {
55+
t.Errorf("VersionInfo.String() = %v, want %v", got, tt.want)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)