Skip to content

Commit

Permalink
Fix connection.Protocol and Address (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kke authored Feb 10, 2021
1 parent 7e8a8a0 commit 5b27584
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
20 changes: 14 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rig
import (
"testing"

"github.com/creasty/defaults"
"github.com/stretchr/testify/require"
)

Expand All @@ -19,11 +20,23 @@ 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())
require.Equal(t, "Local", h.Protocol())
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())
}

0 comments on commit 5b27584

Please sign in to comment.