56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package system
|
|
|
|
import (
|
|
"fmt"
|
|
"git.davepedu.com/dave/sshuri"
|
|
sftplib "github.com/pkg/sftp"
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// SystemManager providers the functionality to interrogate / interact with target systems
|
|
type SystemManager struct {
|
|
connection *SystemConnection
|
|
}
|
|
|
|
func NewSystemManagerFromUri(uri string) (*SystemManager, error) {
|
|
conn, err := NewSystemConnectionFromUri(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &SystemManager{
|
|
connection: conn,
|
|
}, nil
|
|
}
|
|
|
|
func (s *SystemManager) Test() error {
|
|
return s.connection.Test()
|
|
}
|
|
|
|
// SystemConnection provides an unopinionated interface for interacting with remote systems on a low level
|
|
type SystemConnection struct {
|
|
client *ssh.Client
|
|
}
|
|
|
|
func (s *SystemConnection) Test() error {
|
|
sftp, err := sftplib.NewClient(s.client)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get sftp client: %s", err)
|
|
}
|
|
// Stat some file as a test
|
|
_, err = sftp.Stat("/")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to sat: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewSystemConnectionFromUri(uri string) (*SystemConnection, error) {
|
|
sshClient, err := sshuri.NewSSHClientFromUrlString(uri)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ssh client creation failed: %s", err)
|
|
}
|
|
return &SystemConnection{
|
|
client: sshClient,
|
|
}, nil
|
|
}
|