From 5b27584e6e4f93012e6f4ac4c555193b85d1dc78 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Wed, 10 Feb 2021 10:29:47 +0200 Subject: [PATCH] Fix connection.Protocol and Address (#9) --- connection.go | 20 ++++++++++++++------ connection_test.go | 13 +++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/connection.go b/connection.go index 48746f6c..99bb55c8 100644 --- a/connection.go +++ b/connection.go @@ -91,20 +91,28 @@ func (c *Connection) SetDefaults() { // Protocol returns the connection protocol name func (c *Connection) Protocol() string { - if !c.IsConnected() { - return "NC" + if c.client != nil { + return c.client.Protocol() + } + + if client := c.configuredClient(); client != nil { + return client.Protocol() } - return c.client.Protocol() + return "" } // Address returns the connection address func (c *Connection) Address() string { - if !c.IsConnected() { - return "" + if c.client != nil { + return c.client.IPAddress() + } + + if client := c.configuredClient(); client != nil { + return client.IPAddress() } - return c.client.IPAddress() + return "" } // IsConnected returns true if the client is assumed to be connected. diff --git a/connection_test.go b/connection_test.go index 8cfa1928..fbcd93c6 100644 --- a/connection_test.go +++ b/connection_test.go @@ -3,6 +3,7 @@ package rig import ( "testing" + "github.com/creasty/defaults" "github.com/stretchr/testify/require" ) @@ -19,6 +20,7 @@ func TestHostFunctions(t *testing.T) { }, } + require.NoError(t, defaults.Set(&h)) require.NoError(t, h.Connect()) require.Equal(t, "[local] localhost", h.String()) require.True(t, h.IsConnected()) @@ -26,4 +28,15 @@ func TestHostFunctions(t *testing.T) { require.Equal(t, "127.0.0.1", h.Address()) h.Disconnect() require.False(t, h.IsConnected()) + + h = Host{ + Connection: Connection{ + SSH: &SSH{ + Address: "10.0.0.1", + }, + }, + } + require.NoError(t, defaults.Set(&h)) + require.Equal(t, "SSH", h.Protocol()) + require.Equal(t, "10.0.0.1", h.Address()) }