Skip to content

Commit

Permalink
Add support for version command (#741)
Browse files Browse the repository at this point in the history
* Use correct name for binary

* Add support for version command

- Also refactor the version code out into top-level version pkg to match
how the control-plane is factored
  • Loading branch information
lkysow authored Sep 30, 2021
1 parent 6305e74 commit ff27e8a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 46 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ IMPROVEMENTS:
* Control Plane
* Upgrade Docker image Alpine version from 3.13 to 3.14. [[GH-737](https://github.com/hashicorp/consul-k8s/pull/737)]
* Helm Chart
* Enable adding extra containers to server and client Pods. [[GH-749](https://github.com/hashicorp/consul-k8s/pull/749)]
* Enable adding extra containers to server and client Pods. [[GH-749](https://github.com/hashicorp/consul-k8s/pull/749)]
* CLI
* Add `version` command. [[GH-741](https://github.com/hashicorp/consul-k8s/pull/741)]

## 0.34.1 (September 17, 2021)

Expand Down
2 changes: 1 addition & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ Global Options:
-kubeconfig=<string>
Path to kubeconfig file. This is aliased as "-c".
```
```
67 changes: 25 additions & 42 deletions cli/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,32 @@ package version

import (
"fmt"
"strings"
)
"sync"

var (
// The git commit that was compiled. These will be filled in by the compiler.
GitCommit string
GitDescribe string

// The main version number that is being run at the moment.
//
// Version must conform to the format expected by
// github.com/hashicorp/go-version for tests to work.
Version = "0.0.1"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease = ""
"github.com/hashicorp/consul-k8s/cli/cmd/common"
)

// GetHumanVersion composes the parts of the version in a way that's suitable
// for displaying to humans.
func GetHumanVersion() string {
version := Version
if GitDescribe != "" {
version = GitDescribe
}

release := VersionPrerelease
if GitDescribe == "" && release == "" {
release = "dev"
}

if release != "" {
if !strings.HasSuffix(version, "-"+release) {
// if we tagged a prerelease version then the release is in the version already
version += fmt.Sprintf("-%s", release)
}
if GitCommit != "" {
version += fmt.Sprintf(" (%s)", GitCommit)
}
}

// Strip off any single quotes added by the git information.
return strings.Replace(version, "'", "", -1)
type Command struct {
*common.BaseCommand

Version string
once sync.Once
}

func (c *Command) init() {
c.Init()
}

func (c *Command) Run(_ []string) int {
c.once.Do(c.init)
c.UI.Output(fmt.Sprintf("consul-k8s %s", c.Version))
return 0
}

func (c *Command) Synopsis() string {
return "Prints the version of the CLI."
}

func (c *Command) Help() string {
return "Usage: consul version [options]\n"
}
8 changes: 8 additions & 0 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/hashicorp/consul-k8s/cli/cmd/common"
"github.com/hashicorp/consul-k8s/cli/cmd/install"
"github.com/hashicorp/consul-k8s/cli/cmd/uninstall"
cmdversion "github.com/hashicorp/consul-k8s/cli/cmd/version"
"github.com/hashicorp/consul-k8s/cli/version"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
)
Expand All @@ -28,6 +30,12 @@ func initializeCommands(ctx context.Context, log hclog.Logger) (*common.BaseComm
BaseCommand: baseCommand,
}, nil
},
"version": func() (cli.Command, error) {
return &cmdversion.Command{
BaseCommand: baseCommand,
Version: version.GetHumanVersion(),
}, nil
},
}

return baseCommand, commands
Expand Down
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os/signal"
"syscall"

"github.com/hashicorp/consul-k8s/cli/cmd/version"
"github.com/hashicorp/consul-k8s/cli/version"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
)
Expand Down
50 changes: 50 additions & 0 deletions cli/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package version

import (
"fmt"
"strings"
)

var (
// The git commit that was compiled. These will be filled in by the compiler.
GitCommit string
GitDescribe string

// The main version number that is being run at the moment.
//
// Version must conform to the format expected by
// github.com/hashicorp/go-version for tests to work.
Version = "0.34.1"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease = "dev"
)

// GetHumanVersion composes the parts of the version in a way that's suitable
// for displaying to humans.
func GetHumanVersion() string {
version := Version
if GitDescribe != "" {
version = GitDescribe
}

release := VersionPrerelease
if GitDescribe == "" && release == "" {
release = "dev"
}

if release != "" {
if !strings.HasSuffix(version, "-"+release) {
// if we tagged a prerelease version then the release is in the version already
version += fmt.Sprintf("-%s", release)
}
if GitCommit != "" {
version += fmt.Sprintf(" (%s)", GitCommit)
}
}

// Strip off any single quotes added by the git information.
return strings.Replace(version, "'", "", -1)
}
2 changes: 1 addition & 1 deletion control-plane/subcommand/version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Command struct {
}

func (c *Command) Run(_ []string) int {
c.UI.Output(fmt.Sprintf("consul-k8s %s", c.Version))
c.UI.Output(fmt.Sprintf("consul-k8s-control-plane %s", c.Version))
return 0
}

Expand Down

0 comments on commit ff27e8a

Please sign in to comment.