From b32731d09fccee6e130e1787529a963eda7cd9d0 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Tue, 19 Jul 2016 15:06:32 -0700 Subject: [PATCH] Makes sure version is always displayed consistently. --- command/agent/command.go | 53 ++++++++++++---------------------------- command/version.go | 23 ++++------------- commands.go | 23 ++++------------- version.go | 29 ++++++++++++++++++++++ 4 files changed, 54 insertions(+), 74 deletions(-) diff --git a/command/agent/command.go b/command/agent/command.go index 0163223966..1a70c34a16 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -37,20 +37,18 @@ var validDatacenter = regexp.MustCompile("^[a-zA-Z0-9_-]+$") // ShutdownCh. If two messages are sent on the ShutdownCh it will forcibly // exit. type Command struct { - Revision string - Version string - VersionPrerelease string - Ui cli.Ui - ShutdownCh <-chan struct{} - args []string - logFilter *logutils.LevelFilter - logOutput io.Writer - agent *Agent - rpcServer *AgentRPC - httpServers []*HTTPServer - dnsServer *DNSServer - scadaProvider *scada.Provider - scadaHttp *HTTPServer + Version string + Ui cli.Ui + ShutdownCh <-chan struct{} + args []string + logFilter *logutils.LevelFilter + logOutput io.Writer + agent *Agent + rpcServer *AgentRPC + httpServers []*HTTPServer + dnsServer *DNSServer + scadaProvider *scada.Provider + scadaHttp *HTTPServer } // readConfig is responsible for setup of our configuration using @@ -310,11 +308,6 @@ func (c *Command) readConfig() *Config { c.Ui.Error("WARNING: Bootstrap mode enabled! Do not enable unless necessary") } - // Set the version info - config.Revision = c.Revision - config.Version = c.Version - config.VersionPrerelease = c.VersionPrerelease - return config } @@ -406,19 +399,7 @@ func (c *Command) setupLoggers(config *Config) (*GatedWriter, *logWriter, io.Wri // setupAgent is used to start the agent and various interfaces func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *logWriter) error { - var version string - - version = "v" + config.Version - - if len(config.VersionPrerelease) != 0 { - version += " " + config.VersionPrerelease - - if len(config.Revision) != 0 { - version += " " + config.Revision - } - } - - c.Ui.Output("Starting Consul agent (" + version + ")...") + c.Ui.Output("Starting Consul agent...") agent, err := Create(config, logOutput) if err != nil { c.Ui.Error(fmt.Sprintf("Error starting agent: %s", err)) @@ -533,12 +514,7 @@ func (c *Command) checkpointResults(results *checkpoint.CheckResponse, err error return } if results.Outdated { - versionStr := c.Version - if c.VersionPrerelease != "" { - versionStr += fmt.Sprintf("-%s", c.VersionPrerelease) - } - - c.Ui.Error(fmt.Sprintf("Newer Consul version available: %s (currently running: %s)", results.CurrentVersion, versionStr)) + c.Ui.Error(fmt.Sprintf("Newer Consul version available: %s (currently running: %s)", results.CurrentVersion, c.Version)) } for _, alert := range results.Alerts { switch alert.Level { @@ -836,6 +812,7 @@ func (c *Command) Run(args []string) int { c.agent.StartSync() c.Ui.Output("Consul agent running!") + c.Ui.Info(fmt.Sprintf(" Version: '%s'", c.Version)) c.Ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName)) c.Ui.Info(fmt.Sprintf(" Datacenter: '%s'", config.Datacenter)) c.Ui.Info(fmt.Sprintf(" Server: %v (bootstrap: %v)", config.Server, config.Bootstrap)) diff --git a/command/version.go b/command/version.go index fd8a5336c3..f8b9705422 100644 --- a/command/version.go +++ b/command/version.go @@ -1,7 +1,6 @@ package command import ( - "bytes" "fmt" "github.com/hashicorp/consul/consul" "github.com/mitchellh/cli" @@ -9,10 +8,8 @@ import ( // VersionCommand is a Command implementation prints the version. type VersionCommand struct { - Revision string - Version string - VersionPrerelease string - Ui cli.Ui + Version string + Ui cli.Ui } func (c *VersionCommand) Help() string { @@ -20,19 +17,9 @@ func (c *VersionCommand) Help() string { } func (c *VersionCommand) Run(_ []string) int { - var versionString bytes.Buffer - fmt.Fprintf(&versionString, "Consul %s", c.Version) - if c.VersionPrerelease != "" { - fmt.Fprintf(&versionString, ".%s", c.VersionPrerelease) - - if c.Revision != "" { - fmt.Fprintf(&versionString, " (%s)", c.Revision) - } - } - - c.Ui.Output(versionString.String()) - c.Ui.Output(fmt.Sprintf("Consul Protocol: %d (Understands back to: %d)", - consul.ProtocolVersionMax, consul.ProtocolVersionMin)) + c.Ui.Output(fmt.Sprintf("Consul Version: %s", c.Version)) + c.Ui.Output(fmt.Sprintf("Supported Protocol Version(s): %d to %d", + consul.ProtocolVersionMin, consul.ProtocolVersionMax)) return 0 } diff --git a/commands.go b/commands.go index 57c640ab5a..51d55d8b07 100644 --- a/commands.go +++ b/commands.go @@ -19,11 +19,9 @@ func init() { Commands = map[string]cli.CommandFactory{ "agent": func() (cli.Command, error) { return &agent.Command{ - Revision: GitCommit, - Version: Version, - VersionPrerelease: VersionPrerelease, - Ui: ui, - ShutdownCh: make(chan struct{}), + Version: GetVersion(), + Ui: ui, + ShutdownCh: make(chan struct{}), }, nil }, @@ -121,20 +119,9 @@ func init() { }, "version": func() (cli.Command, error) { - ver := Version - rel := VersionPrerelease - if GitDescribe != "" { - ver = GitDescribe - } - if GitDescribe == "" && rel == "" { - rel = "dev" - } - return &command.VersionCommand{ - Revision: GitCommit, - Version: ver, - VersionPrerelease: rel, - Ui: ui, + Version: GetVersion(), + Ui: ui, }, nil }, diff --git a/version.go b/version.go index c45bedfbeb..589b4ac872 100644 --- a/version.go +++ b/version.go @@ -1,5 +1,10 @@ package main +import ( + "fmt" + "strings" +) + // The git commit that was compiled. This will be filled in by the compiler. var ( GitCommit string @@ -13,3 +18,27 @@ const Version = "0.7.0" // then it means that it is a final release. Otherwise, this is a pre-release // such as "dev" (in development), "beta", "rc1", etc. const VersionPrerelease = "dev" + +// GetVersion returns the full version of Consul. +func GetVersion() string { + version := Version + if GitDescribe != "" { + version = GitDescribe + } + + release := VersionPrerelease + if GitDescribe == "" && release == "" { + release = "dev" + } + + fullVersion := fmt.Sprintf("%s", version) + if release != "" { + fullVersion += fmt.Sprintf("-%s", release) + if GitCommit != "" { + fullVersion += fmt.Sprintf(" (%s)", GitCommit) + } + } + + // Strip off any single quotes added by the git information. + return strings.Replace(fullVersion, "'", "", -1) +}