/
factory.go
93 lines (76 loc) · 1.95 KB
/
factory.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
83
84
85
86
87
88
89
90
91
92
93
package transport
import (
"errors"
"sync"
"github.com/scrapli/scrapligo/logging"
"github.com/scrapli/scrapligo/util"
)
// GetTransportNames is returns a slice of available transport type names.
func GetTransportNames() []string {
return []string{SystemTransport, StandardTransport, TelnetTransport}
}
// GetNetconfTransportNames returns a slice of available NETCONF transport type names.
func GetNetconfTransportNames() []string {
return []string{SystemTransport, StandardTransport}
}
// NewTransport returns an instance of Transport with the requested transport implementation (as
// defined in transportType) set. Typically, users should not need to call this as the process of
// Driver creation will handle this for you.
func NewTransport(
l *logging.Instance,
host, transportType string,
options ...util.Option,
) (*Transport, error) {
var i Implementation
var err error
var args *Args
args, err = NewArgs(l, host, options...)
if err != nil {
return nil, err
}
if args.UserImplementation != nil {
i = args.UserImplementation
} else {
switch transportType {
case SystemTransport, StandardTransport:
var sshArgs *SSHArgs
sshArgs, err = NewSSHArgs(options...)
if err != nil {
return nil, err
}
switch transportType {
case SystemTransport:
i, err = NewSystemTransport(sshArgs)
case StandardTransport:
i, err = NewStandardTransport(sshArgs)
}
case TelnetTransport:
var telnetArgs *TelnetArgs
telnetArgs, err = NewTelnetArgs(options...)
if err != nil {
return nil, err
}
i, err = NewTelnetTransport(telnetArgs)
case FileTransport:
i, err = NewFileTransport()
}
if err != nil {
return nil, err
}
}
for _, option := range options {
err = option(i)
if err != nil {
if !errors.Is(err, util.ErrIgnoredOption) {
return nil, err
}
}
}
t := &Transport{
Args: args,
Impl: i,
implLock: &sync.Mutex{},
timeoutLock: &sync.Mutex{},
}
return t, nil
}