forked from bramvdbogaerde/go-scp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurer.go
82 lines (71 loc) · 2.16 KB
/
configurer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Copyright (c) 2020 Bram Vandenbogaerde
* You may use, distribute or modify this code under the
* terms of the Mozilla Public License 2.0, which is distributed
* along with the source code.
*/
package scp
import (
"time"
"golang.org/x/crypto/ssh"
)
// ClientConfigurer a struct containing all the configuration options
// used by an scp client.
type ClientConfigurer struct {
host string
clientConfig *ssh.ClientConfig
session *ssh.Session
timeout time.Duration
remoteBinary string
sshClient *ssh.Client
}
// NewConfigurer creates a new client configurer.
// It takes the required parameters: the host and the ssh.ClientConfig and
// returns a configurer populated with the default values for the optional
// parameters.
//
// These optional parameters can be set by using the methods provided on the
// ClientConfigurer struct.
func NewConfigurer(host string, config *ssh.ClientConfig) *ClientConfigurer {
return &ClientConfigurer{
host: host,
clientConfig: config,
timeout: 0, // no timeout by default
remoteBinary: "scp",
}
}
// RemoteBinary sets the path of the location of the remote scp binary
// Defaults to: /usr/bin/scp.
func (c *ClientConfigurer) RemoteBinary(path string) *ClientConfigurer {
c.remoteBinary = path
return c
}
// Host alters the host of the client connects to.
func (c *ClientConfigurer) Host(host string) *ClientConfigurer {
c.host = host
return c
}
// Timeout Changes the connection timeout.
// Defaults to one minute.
func (c *ClientConfigurer) Timeout(timeout time.Duration) *ClientConfigurer {
c.timeout = timeout
return c
}
// ClientConfig alters the ssh.ClientConfig.
func (c *ClientConfigurer) ClientConfig(config *ssh.ClientConfig) *ClientConfigurer {
c.clientConfig = config
return c
}
func (c *ClientConfigurer) SSHClient(sshClient *ssh.Client) *ClientConfigurer {
c.sshClient = sshClient
return c
}
// Create builds a client with the configuration stored within the ClientConfigurer.
func (c *ClientConfigurer) Create() Client {
return Client{
Host: c.host,
ClientConfig: c.clientConfig,
Timeout: c.timeout,
RemoteBinary: c.remoteBinary,
sshClient: c.sshClient,
}
}