-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
169 lines (133 loc) · 3.69 KB
/
client.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gossh
import (
//"fmt"
"os"
"strconv"
log "github.com/uthng/golog"
"golang.org/x/crypto/ssh"
)
// Client encapsulates ssh client
type Client struct {
client *ssh.Client
logger *log.Logger
}
// NewClient initializes a ssh client following
// authentication configuration
func NewClient(config *Config) (*Client, error) {
c := &Client{}
client, err := ssh.Dial("tcp", config.Host+":"+strconv.Itoa(config.Port), config.ClientConfig)
if err != nil {
return nil, err
}
c.client = client
c.logger = log.NewLogger()
c.logger.SetVerbosity(log.NONE)
return c, nil
}
// SetVerbosity sets log level
func (c *Client) SetVerbosity(level int) {
c.logger.SetVerbosity(level)
}
// DisableLogColor disables log colors
func (c *Client) DisableLogColor(level int) {
c.logger.DisableColor()
}
// ExecCommand executes a shell command on remote machine
func (c *Client) ExecCommand(cmd string) ([]byte, error) {
session, err := c.client.NewSession()
if err != nil {
return nil, err
}
defer session.Close()
return session.CombinedOutput(cmd)
}
// SCPBytes sends content in bytes to remote machine and save it
// in a file with the given path
func (c *Client) SCPSendBytes(content []byte, destFile, mode string) error {
c.checkLogEnvVars()
session, err := c.client.NewSession()
if err != nil {
return nil
}
defer session.Close()
scpSession, err := newSCPSession(c, session)
if err != nil {
return err
}
return scpSession.SendBytes(content, destFile, mode)
}
// SCPFile sends a file to remote machine
func (c *Client) SCPSendFile(srcFile, destFile, mode string) error {
c.checkLogEnvVars()
session, err := c.client.NewSession()
if err != nil {
return nil
}
defer session.Close()
scpSession, err := newSCPSession(c, session)
if err != nil {
return err
}
return scpSession.SendFile(srcFile, destFile, mode)
}
// SCPDir sends recursively a directory to remote machine.
// Mode is only applied for the 1st directory. All files/folders
// inside the srcDir will preserve the same mode on remote machine
func (c *Client) SCPSendDir(srcDir, destDir, mode string) error {
c.checkLogEnvVars()
session, err := c.client.NewSession()
if err != nil {
return nil
}
defer session.Close()
scpSession, err := newSCPSession(c, session)
if err != nil {
return err
}
return scpSession.SendDir(srcDir, destDir, mode)
}
// SCPGetFile gets srcFile from remote machine and save in destDir.
// srcFile must be a regular file.
// destFile is the local regular in which srcFile's content will be stored;.
// If destFile does not exists, it will be created.
func (c *Client) SCPGetFile(srcFile, destFile string) error {
c.checkLogEnvVars()
session, err := c.client.NewSession()
if err != nil {
return nil
}
defer session.Close()
scpSession, err := newSCPSession(c, session)
if err != nil {
return err
}
return scpSession.GetFile(srcFile, destFile)
}
// SCPGetDir gets srcDir from remote machine and save in destDir.
// srcDir must be a folder. destDir is the local folder in which
// all files or subfolders inside srcDir will be stored.
// If destDir does not exists, it will be created.
func (c *Client) SCPGetDir(srcDir, destDir string) error {
c.checkLogEnvVars()
session, err := c.client.NewSession()
if err != nil {
return nil
}
defer session.Close()
scpSession, err := newSCPSession(c, session)
if err != nil {
return err
}
return scpSession.GetDir(srcDir, destDir)
}
/////////////// INTERNAL FUNCTIONS //////////////////////////
func (c *Client) checkLogEnvVars() {
verbosity := os.Getenv("GOSSH_VERBOSITY")
if s, err := strconv.Atoi(verbosity); err == nil {
c.logger.SetVerbosity(s)
}
disabled := os.Getenv("GOSSH_DISABLE_COLOR")
if disabled != "" {
c.logger.DisableColor()
}
}