From 93ba5104d5dfaa9ff964d5fcca4cbcb0f55402eb Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Tue, 13 Jun 2017 16:51:02 +0200 Subject: transport/ssh: allow passing SSH options Adds the possibility of passing options to SSH transport. Options have the form of functions modifying ssh.ClientConfig. --- plumbing/transport/ssh/common.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'plumbing/transport/ssh') diff --git a/plumbing/transport/ssh/common.go b/plumbing/transport/ssh/common.go index d53fc12..6ab0835 100644 --- a/plumbing/transport/ssh/common.go +++ b/plumbing/transport/ssh/common.go @@ -11,7 +11,16 @@ import ( ) // DefaultClient is the default SSH client. -var DefaultClient = common.NewClient(&runner{}) +var DefaultClient = NewClient() + +// NewClient creates a new SSH client with the given options. +func NewClient(opts ...ClientOption) transport.Transport { + return common.NewClient(&runner{options: opts}) +} + +// ClientOption is a function that gets a standard ssh.ClientConfig and modifies +// it. +type ClientOption func(config *ssh.ClientConfig) // DefaultAuthBuilder is the function used to create a default AuthMethod, when // the user doesn't provide any. @@ -21,10 +30,12 @@ var DefaultAuthBuilder = func(user string) (AuthMethod, error) { const DefaultPort = 22 -type runner struct{} +type runner struct { + options []ClientOption +} func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod) (common.Command, error) { - c := &command{command: cmd, endpoint: ep} + c := &command{command: cmd, endpoint: ep, options: r.options} if auth != nil { c.setAuth(auth) } @@ -42,6 +53,7 @@ type command struct { endpoint transport.Endpoint client *ssh.Client auth AuthMethod + options []ClientOption } func (c *command) setAuth(auth transport.AuthMethod) error { @@ -95,6 +107,10 @@ func (c *command) connect() error { return err } + for _, opt := range c.options { + opt(config) + } + c.client, err = ssh.Dial("tcp", c.getHostWithPort(), config) if err != nil { return err -- cgit