Skip to content

Commit

Permalink
refactor: Remove duplicate type declarations in sshutils package
Browse files Browse the repository at this point in the history
  • Loading branch information
aronchick committed Nov 30, 2024
1 parent e34ea6c commit e8bf36e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 179 deletions.
162 changes: 1 addition & 161 deletions pkg/sshutils/ssh_config_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,164 +105,4 @@ func NewMockSSHConfigWithBehavior(behavior ExpectedSSHBehavior) *MockSSHConfig {
return mockSSHConfig
}

// MockSSHConfig is a mock implementation of SSHConfiger
type MockSSHConfig struct {
mock.Mock
lastOutput string
}

func (m *MockSSHConfig) PushFile(
ctx context.Context,
dst string,
fileContents []byte,
executable bool,
) error {
fmt.Printf("PushFile called with: %s\n", dst)
args := m.Called(ctx, dst, fileContents, executable)
return args.Error(0)
}

func (m *MockSSHConfig) PushFileWithCallback(
ctx context.Context,
dst string,
fileContents []byte,
executable bool,
progressCallback func(int64, int64),
) error {
args := m.Called(ctx, dst, fileContents, executable, progressCallback)
return args.Error(0)
}

func (m *MockSSHConfig) ExecuteCommand(
ctx context.Context,
cmd string,
) (string, error) {
fmt.Printf("ExecuteCommand called with: %s\n", cmd)
args := m.Called(ctx, cmd)
if args.Get(0) == nil {
return "", args.Error(1)
}
// Try to convert the result to a string
switch v := args.Get(0).(type) {
case string:
return v, args.Error(1)
case []byte:
return string(v), args.Error(1)
default:
// If it's not a string or []byte, convert it to a JSON string
jsonBytes, err := json.Marshal(v)
if err != nil {
return "", fmt.Errorf("failed to convert mock result to string: %v", err)
}
return string(jsonBytes), args.Error(1)
}
}

func (m *MockSSHConfig) ExecuteCommandWithCallback(
ctx context.Context,
cmd string,
progressCallback func(string),
) (string, error) {
args := m.Called(ctx, cmd, progressCallback)
return args.Get(0).(string), args.Error(1)
}

func (m *MockSSHConfig) InstallSystemdService(
ctx context.Context,
serviceName string,
serviceContent string,
) error {
args := m.Called(ctx, serviceName, serviceContent)
return args.Error(0)
}

func (m *MockSSHConfig) RestartService(ctx context.Context, serviceName string) error {
args := m.Called(ctx, serviceName)
return args.Error(0)
}

func (m *MockSSHConfig) Connect() (SSHClienter, error) {
args := m.Called()
return args.Get(0).(SSHClienter), args.Error(1)
}

func (m *MockSSHConfig) Close() error { return nil }

func (m *MockSSHConfig) WaitForSSH(
ctx context.Context,
retries int,
retryDelay time.Duration,
) error {
args := m.Called(ctx, retries, retryDelay)
return args.Error(0)
}

func (m *MockSSHConfig) StartService(
ctx context.Context,
serviceName string,
) error {
return nil
}

func (m *MockSSHConfig) GetLastOutput() string {
return m.lastOutput
}

func (m *MockSSHConfig) GetHost() string {
args := m.Called()
return args.String(0)
}

func (m *MockSSHConfig) GetPort() int {
args := m.Called()
return args.Int(0)
}

func (m *MockSSHConfig) GetUser() string {
args := m.Called()
return args.String(0)
}

func (m *MockSSHConfig) GetPrivateKeyMaterial() []byte {
args := m.Called()
if ret := args.Get(0); ret != nil {
return ret.([]byte)
}
return nil
}

func (m *MockSSHConfig) GetSSHDial() SSHDialer {
args := m.Called()
return args.Get(0).(SSHDialer)
}

func (m *MockSSHConfig) SetSSHDial(dialer SSHDialer) {
m.Called(dialer)
}

func (m *MockSSHConfig) SetValidateSSHConnection(callback func() error) {
m.Called(callback)
}

func (m *MockSSHConfig) GetSSHClienter() SSHClienter {
args := m.Called()
return args.Get(0).(SSHClienter)
}

func (m *MockSSHConfig) SetSSHClienter(clienter SSHClienter) {
m.Called(clienter)
}

func (m *MockSSHConfig) GetSSHClient() *ssh.Client {
args := m.Called()
if ret := args.Get(0); ret != nil {
return ret.(*ssh.Client)
}
return nil
}

func (m *MockSSHConfig) SetSSHClient(client *ssh.Client) {
m.Called(client)
}

var _ SSHConfiger = &MockSSHConfig{}
// MockSSHConfig is now defined in mock_sshutils.go
9 changes: 1 addition & 8 deletions pkg/sshutils/ssh_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ import (

var SSHDialerFunc = NewSSHDial

type SSHDialer interface {
Dial(network, addr string, config *ssh.ClientConfig) (SSHClienter, error)
DialContext(
ctx context.Context,
network, addr string,
config *ssh.ClientConfig,
) (SSHClienter, error)
}
// SSHDialer is now defined in interfaces.go

type sshDial struct {
host string
Expand Down
11 changes: 1 addition & 10 deletions pkg/sshutils/ssh_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ import (
"strings"
)

type SSHSessioner interface {
Run(cmd string) error
Start(cmd string) error
Wait() error
Close() error
StdinPipe() (io.WriteCloser, error)
StdoutPipe() (io.Reader, error)
StderrPipe() (io.Reader, error)
CombinedOutput(cmd string) ([]byte, error)
}
// SSHSessioner is now defined in interfaces.go

// ValidateSSHPublicKey checks if the provided SSH public key is valid
func ValidateSSHPublicKey(key string) error {
Expand Down

0 comments on commit e8bf36e

Please sign in to comment.