Skip to content

Commit

Permalink
Add connection.Protocol() and .Address() (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
kke authored Feb 9, 2021
1 parent c0711ec commit 7e8a8a0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type client interface {
Exec(string, ...exec.Option) error
ExecInteractive(string) error
String() string
Protocol() string
IPAddress() string
IsConnected() bool
}

Expand Down Expand Up @@ -87,6 +89,24 @@ func (c *Connection) SetDefaults() {
_ = defaults.Set(c.client)
}

// Protocol returns the connection protocol name
func (c *Connection) Protocol() string {
if !c.IsConnected() {
return "NC"
}

return c.client.Protocol()
}

// Address returns the connection address
func (c *Connection) Address() string {
if !c.IsConnected() {
return ""
}

return c.client.IPAddress()
}

// IsConnected returns true if the client is assumed to be connected.
// "Assumed" - as in `Connect()` has been called and no error was returned.
// The underlying client may actually have disconnected and has become
Expand Down
2 changes: 2 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func TestHostFunctions(t *testing.T) {
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())
}
10 changes: 10 additions & 0 deletions localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ type Localhost struct {
Enabled bool `yaml:"enabled" validate:"required,eq=true" default:"true"`
}

// Protocol returns the protocol name, "Local"
func (c *Localhost) Protocol() string {
return "Local"
}

// IPAddress returns the connection address
func (c *Localhost) IPAddress() string {
return "127.0.0.1"
}

// String returns the connection's printable name
func (c *Localhost) String() string {
return name
Expand Down
10 changes: 10 additions & 0 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (c *SSH) SetDefaults() {
}
}

// Protocol returns the protocol name, "SSH"
func (c *SSH) Protocol() string {
return "SSH"
}

// IPAddress returns the connection address
func (c *SSH) IPAddress() string {
return c.Address
}

// String returns the connection's printable name
func (c *SSH) String() string {
if c.name == "" {
Expand Down
10 changes: 10 additions & 0 deletions winrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func (c *WinRM) SetDefaults() {
}
}

// Protocol returns the protocol name, "WinRM"
func (c *WinRM) Protocol() string {
return "WinRM"
}

// IPAddress returns the connection address
func (c *WinRM) IPAddress() string {
return c.Address
}

// String returns the connection's printable name
func (c *WinRM) String() string {
if c.name == "" {
Expand Down

0 comments on commit 7e8a8a0

Please sign in to comment.