Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

internal/cli: version shows Waypoint server version #1364

Merged
merged 3 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1364.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: version command now shoes the server version
```
2 changes: 1 addition & 1 deletion internal/cli/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (c *baseCommand) Init(opts ...Option) error {

// Create our client
if baseCfg.Client {
c.project, err = c.initClient()
c.project, err = c.initClient(nil)
if err != nil {
c.logError(c.Log, "failed to create client", err)
return err
Expand Down
12 changes: 10 additions & 2 deletions internal/cli/base_init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"errors"
"fmt"
"path/filepath"
Expand Down Expand Up @@ -64,7 +65,10 @@ func (c *baseCommand) initConfigLoad(path string) (*configpkg.Config, error) {
}

// initClient initializes the client.
func (c *baseCommand) initClient() (*clientpkg.Project, error) {
//
// If ctx is nil, c.Ctx will be used. If ctx is non-nil, that context will be
// used and c.Ctx will be ignored.
func (c *baseCommand) initClient(ctx context.Context) (*clientpkg.Project, error) {
// We use our flag-based connection info if the user set an addr.
var flagConnection *clicontext.Config
if v := c.flagConnection; v.Server.Address != "" {
Expand Down Expand Up @@ -102,6 +106,10 @@ func (c *baseCommand) initClient() (*clientpkg.Project, error) {
opts = append(opts, clientpkg.WithUI(c.ui))
}

if ctx == nil {
ctx = c.Ctx
}

// Create our client
return clientpkg.New(c.Ctx, opts...)
return clientpkg.New(ctx, opts...)
}
2 changes: 1 addition & 1 deletion internal/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (c *InitCommand) validateServer() bool {
defer sg.Wait()

s := sg.Add("Validating server credentials...")
client, err := c.initClient()
client, err := c.initClient(nil)
if err != nil {
c.stepError(s, initStepConnect, err)
return false
Expand Down
32 changes: 23 additions & 9 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,29 @@ func Main(args []string) int {
base, commands := Commands(ctx, log, logOutput)
defer base.Close()

// Build the CLI
cli := &cli.CLI{
Name: args[0],
Args: args[1:],
Version: vsn.FullVersionNumber(true),
Commands: commands,
Autocomplete: true,
AutocompleteNoDefaultFlags: true,
HelpFunc: GroupedHelpFunc(cli.BasicHelpFunc(cliName)),
// Build the CLI. We use a CLI factory function because to modify the
// args once you call a func on CLI you need to create a new CLI instance.
cliFactory := func() *cli.CLI {
return &cli.CLI{
Name: args[0],
Args: args[1:],
Version: vsn.FullVersionNumber(true),
Commands: commands,
Autocomplete: true,
AutocompleteNoDefaultFlags: true,
HelpFunc: GroupedHelpFunc(cli.BasicHelpFunc(cliName)),
}
}

// Copy the CLI to check if it is a version call. If so, we modify
// the args to just be the version subcommand. This ensures that
// --version behaves by calling `waypoint version` and we get consistent
// behavior.
cli := cliFactory()
if cli.IsVersion() {
// We need to reinit because you can't modify fields after calling funcs
cli = cliFactory()
cli.Args = []string{"version"}
}

// Run the CLI
Expand Down
29 changes: 25 additions & 4 deletions internal/cli/version.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cli

import (
"context"
"errors"
"time"

"github.com/posener/complete"

"github.com/hashicorp/waypoint/internal/pkg/flag"
"github.com/hashicorp/waypoint/internal/serverclient"
"github.com/hashicorp/waypoint/internal/version"
)

Expand All @@ -22,12 +27,26 @@ func (c *VersionCommand) Run(args []string) int {
WithFlags(flagSet),
WithNoConfig(),
WithClient(false),
WithNoAutoServer(),
); err != nil {
return 1
}

out := c.VersionInfo.FullVersionNumber(true)
c.ui.Output(out)
c.ui.Output("CLI: %s", out)

// Get our server version. We use a short context here.
ctx, cancel := context.WithTimeout(c.Ctx, 2*time.Second)
defer cancel()
client, err := c.initClient(ctx)
if err != nil && !errors.Is(err, serverclient.ErrNoServerConfig) {
c.ui.Output("Error connecting to server to read server version: %s", err.Error())
}

if err == nil {
server := client.ServerVersion()
c.ui.Output("Server: %s", server.Version)
}

return 0
}
Expand All @@ -52,9 +71,11 @@ func (c *VersionCommand) Help() string {
return formatHelp(`
Usage: waypoint version

Prints the version of this Waypoint CLI.
Prints the version information for Waypoint.

This command will show the version of the current Waypoint CLI. If
the CLI is configured to communicate to a Waypoint server, the server
version will also be shown.

There are no arguments or flags to this command. Any additional arguments or
flags are ignored.
`)
}
6 changes: 6 additions & 0 deletions internal/client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Project struct {
labels map[string]string
dataSourceOverrides map[string]string
cleanupFunc func()
serverVersion *pb.VersionInfo

local bool

Expand Down Expand Up @@ -141,6 +142,11 @@ func (c *Project) Local() bool {
return c.localServer
}

// ServerVersion returns the server version that this client is connected to.
func (c *Project) ServerVersion() *pb.VersionInfo {
return c.serverVersion
}

// Close should be called to clean up any resources that the client created.
func (c *Project) Close() error {
// Stop the runner early so that it we block here waiting for any outstanding jobs to finish
Expand Down
3 changes: 3 additions & 0 deletions internal/client/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func (c *Project) negotiateApiVersion(ctx context.Context) error {
"entrypoint_current", resp.Info.Entrypoint.Current,
)

// Store the server version info
c.serverVersion = resp.Info

vsn, err := protocolversion.Negotiate(protocolversion.Current().Api, resp.Info.Api)
if err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion internal/serverclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package serverclient
import (
"context"
"crypto/tls"
"errors"
"fmt"
"os"
"time"
Expand All @@ -15,6 +16,10 @@ import (
"github.com/hashicorp/waypoint/internal/serverconfig"
)

// ErrNoServerConfig is the error when there is no server configuration
// found for connection.
var ErrNoServerConfig = errors.New("no server connection configuration found")

// ConnectOption is used to configure how Waypoint server connection
// configuration is sourced.
type ConnectOption func(*connectConfig) error
Expand All @@ -40,7 +45,7 @@ func Connect(ctx context.Context, opts ...ConnectOption) (*grpc.ClientConn, erro
return nil, nil
}

return nil, fmt.Errorf("no server credentials found")
return nil, ErrNoServerConfig
}

ctx, cancel := context.WithTimeout(ctx, cfg.Timeout)
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *VersionInfo) FullVersionNumber(rev bool) string {
return "Waypoint (version unknown)"
}

fmt.Fprintf(&versionString, "Waypoint %s", c.Version)
fmt.Fprintf(&versionString, "%s", c.Version)
if c.VersionPrerelease != "" && c.GitDescribe == "" {
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
}
Expand Down