Skip to content

Commit

Permalink
Eliminate localhost.Config, just use bool
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Feb 14, 2024
1 parent 06d8a6c commit 9258b4f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
18 changes: 8 additions & 10 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"gopkg.in/yaml.v2"
)

func TestConnectionWithConfigurer(t *testing.T) {
func TestClientWithConfigurer(t *testing.T) {
cc := &rig.CompositeConfig{
Localhost: &localhost.Config{Enabled: true},
Localhost: true,
}
conn, err := rig.NewConnection(
rig.WithConnectionConfigurer(cc),
Expand All @@ -26,16 +26,16 @@ func TestConnectionWithConfigurer(t *testing.T) {
require.Equal(t, "hello", out)
}

func TestConnectionWithClient(t *testing.T) {
client, err := localhost.NewConnection(localhost.Config{Enabled: true})
func TestClient(t *testing.T) {
conn, err := localhost.NewConnection()
require.NoError(t, err)
conn, err := rig.NewConnection(rig.WithConnection(client))
client, err := rig.NewConnection(rig.WithConnection(conn))
require.NoError(t, err)
require.NotNil(t, conn)

require.NoError(t, conn.Connect())
require.NoError(t, client.Connect())

out, err := conn.ExecOutput("echo hello")
out, err := client.ExecOutput("echo hello")
require.NoError(t, err)
require.Equal(t, "hello", out)
}
Expand Down Expand Up @@ -101,9 +101,7 @@ type testHostConfigured struct {

func TestConfiguredConnectionUnmarshal(t *testing.T) {
hostConfig := map[string]any{
"localhost": map[string]any{
"enabled": true,
},
"localhost": true,
}
mainConfig := map[string]any{
"hosts": []map[string]any{hostConfig},
Expand Down
16 changes: 8 additions & 8 deletions compositeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var _ ConnectionConfigurer = (*CompositeConfig)(nil)
// CompositeConfig is a composite configuration of all the protocols supported by rig.
// It is intended to be embedded into host structs that are unmarshaled from configuration files.
type CompositeConfig struct {
WinRM *winrm.Config `yaml:"winRM,omitempty"`
SSH *ssh.Config `yaml:"ssh,omitempty"`
Localhost *localhost.Config `yaml:"localhost,omitempty"`
OpenSSH *openssh.Config `yaml:"openSSH,omitempty"`
WinRM *winrm.Config `yaml:"winRM,omitempty"`
SSH *ssh.Config `yaml:"ssh,omitempty"`
Localhost bool `yaml:"localhost,omitempty"`
OpenSSH *openssh.Config `yaml:"openSSH,omitempty"`

s *string
}
Expand All @@ -34,10 +34,6 @@ func (c *CompositeConfig) Connection() (Connection, error) {
client, err = c.WinRM.Connection()
}

if c.Localhost != nil {
client, err = c.Localhost.Connection()
}

if c.SSH != nil {
client, err = c.SSH.Connection()
}
Expand All @@ -46,6 +42,10 @@ func (c *CompositeConfig) Connection() (Connection, error) {
client, err = c.OpenSSH.Connection()
}

if c.Localhost {
client, err = localhost.NewConnection()
}

if client == nil && err == nil {
return nil, ErrNoConnectionConfig
}
Expand Down
21 changes: 7 additions & 14 deletions localhost/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@ import (

const name = "[local] localhost"

// Config is the configuration for the Localhost connection
type Config struct {
Enabled bool `yaml:"enabled" validate:"required,eq=true" default:"true"`
}

// Connection returns a new localhost Connection from the configuration
func (c Config) Connection() (*Connection, error) {
return NewConnection(c)
}

// Connection is a direct localhost connection
type Connection struct {
Config `yaml:",inline"`
type Connection struct{}

// Connection returns the connection itself. This is because there's no config for localhost connections.
func (c *Connection) Connection() (*Connection, error) {
return c, nil
}

// NewConnection creates a new Localhost connection. Error is currently always nil.
func NewConnection(cfg Config) (*Connection, error) {
return &Connection{cfg}, nil
func NewConnection() (*Connection, error) {
return &Connection{}, nil
}

// Protocol returns the protocol name, "Local"
Expand Down
2 changes: 1 addition & 1 deletion test/rig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func GetHost(t *testing.T, options ...rig.Option) *Host {
require.NoError(t, err)
client = winrmclient
case "localhost":
client, _ = localhost.NewConnection(localhost.Config{Enabled: true})
client, _ = localhost.NewConnection()
case "openssh":
cfg := openssh.Config{
Address: targetHost,
Expand Down

0 comments on commit 9258b4f

Please sign in to comment.