2017-09-10 16:13:05 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2018-02-17 01:08:15 +01:00
|
|
|
"context"
|
2017-09-10 16:13:05 +02:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/pkg/errors"
|
2018-02-17 01:08:15 +01:00
|
|
|
"github.com/problame/go-netssh"
|
2017-09-10 16:13:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SSHStdinserverConnecter struct {
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Port uint16
|
|
|
|
IdentityFile string `mapstructure:"identity_file"`
|
|
|
|
TransportOpenCommand []string `mapstructure:"transport_open_command"`
|
|
|
|
SSHCommand string `mapstructure:"ssh_command"`
|
|
|
|
Options []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSSHStdinserverConnecter(i map[string]interface{}) (c *SSHStdinserverConnecter, err error) {
|
|
|
|
|
|
|
|
c = &SSHStdinserverConnecter{}
|
|
|
|
if err = mapstructure.Decode(i, c); err != nil {
|
|
|
|
err = errors.New(fmt.Sprintf("could not parse ssh transport: %s", err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO assert fields are filled
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:45:10 +02:00
|
|
|
func (c *SSHStdinserverConnecter) Connect() (rwc io.ReadWriteCloser, err error) {
|
2018-02-17 01:08:15 +01:00
|
|
|
|
|
|
|
var endpoint netssh.Endpoint
|
|
|
|
if err = copier.Copy(&endpoint, c); err != nil {
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
2018-02-17 01:08:15 +01:00
|
|
|
if rwc, err = netssh.Dial(context.TODO(), endpoint); err != nil {
|
2017-09-11 15:45:10 +02:00
|
|
|
err = errors.WithStack(err)
|
2017-09-10 16:13:05 +02:00
|
|
|
return
|
|
|
|
}
|
2017-09-11 15:45:10 +02:00
|
|
|
return
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|