This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
/
copy.go
75 lines (64 loc) · 1.77 KB
/
copy.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
package libscp
import "github.com/sirupsen/logrus"
// ReceiveFileFromRemote is used to receive a file using the SCP protocol from a
// remote host / machine
func (c *Client) ReceiveFileFromRemote(remoteFilePath string, remoteFilename string, localFilePath string, localFileName string) error {
err := c.VerifyClient()
if err != nil {
return err
}
// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
session, err := c.client.NewSession()
if err != nil {
return err
}
defer session.Close()
sc, err := NewSessionClient(session)
if err != nil {
return err
}
defer sc.writer.Close()
sc.wg.Add(1)
go sc.FileSink(localFilePath, localFileName)
session.Run("/usr/bin/scp -f " + remoteFilePath + "/" + remoteFilename)
for err := range sc.errors {
logrus.WithError(err).Errorln("Error receiving the file")
}
sc.wg.Wait()
return nil
}
// SendFileToRemote is used to send a file using the SCP protocol to a remote
// host or machine
func (c *Client) SendFileToRemote(fp string) error {
logrus.Debugln("Verifying client")
err := c.VerifyClient()
if err != nil {
return err
}
logrus.Debugln("Client passed verification")
logrus.Debugln("Creating session")
// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
s, err := c.client.NewSession()
if err != nil {
return err
}
defer s.Close()
logrus.Debugln("Creating session client")
sc, err := NewSessionClient(s)
if err != nil {
return err
}
defer sc.writer.Close()
sc.wg.Add(1)
go sc.FileSource(fp)
logrus.Infoln("Beginning transfer")
go s.Run("/usr/bin/scp -t ./")
logrus.Debugln("Waiting for transfer to complete...")
sc.wg.Wait()
for err := range sc.errors {
logrus.WithError(err).Errorln("Error sending the file")
}
return nil
}