-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathsshclient.go
More file actions
877 lines (720 loc) · 18.5 KB
/
sshclient.go
File metadata and controls
877 lines (720 loc) · 18.5 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
// Package sshclient implements an SSH client.
package sshclient
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"sync"
"time"
"github.com/kr/fs"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
type remoteScriptType byte
type remoteShellType byte
const (
cmdLine remoteScriptType = iota
rawScript
scriptFile
interactiveShell remoteShellType = iota
nonInteractiveShell
)
// A Client implements an SSH client that supports running commands and scripts remotely.
type Client struct {
sshClient *ssh.Client
sftpSessions sync.Map
}
// DialWithPasswd starts a client connection to the given SSH server with passwd authmethod.
func DialWithPasswd(addr, user, passwd string) (*Client, error) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(passwd),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return Dial("tcp", addr, config)
}
// DialWithKey starts a client connection to the given SSH server with key authmethod.
func DialWithKey(addr, user, keyfile string) (*Client, error) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return Dial("tcp", addr, config)
}
// DialWithKeyWithPassphrase same as DialWithKey but with a passphrase to decrypt the private key
func DialWithKeyWithPassphrase(addr, user, keyfile string, passphrase string) (*Client, error) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKeyWithPassphrase(key, []byte(passphrase))
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return Dial("tcp", addr, config)
}
// Dial starts a client connection to the given SSH server.
// This wraps ssh.Dial.
func Dial(network, addr string, config *ssh.ClientConfig) (*Client, error) {
sshClient, err := ssh.Dial(network, addr, config)
if err != nil {
return nil, err
}
return &Client{sshClient: sshClient}, nil
}
// DialWithConnection starts a client connection using existing net.Conn.
func DialWithConnection(conn net.Conn, addr string, config *ssh.ClientConfig) (*Client, error) {
ncc, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
return nil, err
}
sshClient := ssh.NewClient(ncc, chans, reqs)
return &Client{
sshClient: sshClient,
}, nil
}
// Close closes the underlying client network connection.
func (c *Client) Close() error {
merr := newMultiError()
c.sftpSessions.Range(func(key, value interface{}) bool {
err := value.(*RemoteFileSystem).Close()
merr.Append(err)
return true
})
merr.Append(c.sshClient.Close())
return merr.ErrorOrNil()
}
// UnderlyingClient get the underlying client.
func (c *Client) UnderlyingClient() *ssh.Client {
return c.sshClient
}
// Dial initiates a Client to the addr from the remote host.
func (c *Client) Dial(network, addr string, config *ssh.ClientConfig) (*Client, error) {
conn, err := c.sshClient.Dial(network, addr)
if err != nil {
return nil, err
}
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
return nil, err
}
client := ssh.NewClient(sshConn, chans, reqs)
return &Client{sshClient: client}, nil
}
// DialWithPasswd initiates a Client to the addr from the remote host with passwd authmethod.
func (c *Client) DialWithPasswd(addr, user, passwd string) (*Client, error) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(passwd),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return c.Dial("tcp", addr, config)
}
// DialWithKey initiates a Client to the addr from the remote host with key authmethod.
func (c *Client) DialWithKey(addr, user, keyfile string) (*Client, error) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return c.Dial("tcp", addr, config)
}
// DialWithKeyWithPassphrase same as DialWithKey but with a passphrase to decrypt the private key
func (c *Client) DialWithKeyWithPassphrase(addr, user, keyfile, passphrase string) (*Client, error) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKeyWithPassphrase(key, []byte(passphrase))
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
return c.Dial("tcp", addr, config)
}
// Cmd creates a RemoteScript that can run the command on the client. The cmd string is split on newlines and each line is executed separately.
func (c *Client) Cmd(cmd string) *RemoteScript {
return &RemoteScript{
_type: cmdLine,
client: c.sshClient,
script: bytes.NewBufferString(cmd + "\n"),
}
}
// Script creates a RemoteScript that can run the script on the client.
func (c *Client) Script(script string) *RemoteScript {
return &RemoteScript{
_type: rawScript,
client: c.sshClient,
script: bytes.NewBufferString(script + "\n"),
}
}
// ScriptFile creates a RemoteScript that can read a local script file and run it remotely on the client.
func (c *Client) ScriptFile(fname string) *RemoteScript {
return &RemoteScript{
_type: scriptFile,
client: c.sshClient,
scriptFile: fname,
}
}
// A RemoteScript represents script that can be run remotely.
type RemoteScript struct {
client *ssh.Client
_type remoteScriptType
script *bytes.Buffer
scriptFile string
err error
stdout io.Writer
stderr io.Writer
}
// Run runs the script on the client.
//
// The returned error is nil if the command runs, has no problems
// copying stdin, stdout, and stderr, and exits with a zero exit
// status.
func (rs *RemoteScript) Run() error {
if rs.err != nil {
fmt.Println(rs.err) // TODO
return rs.err
}
if rs._type == cmdLine {
return rs.runCmds()
} else if rs._type == rawScript {
return rs.runScript()
} else if rs._type == scriptFile {
return rs.runScriptFile()
} else {
return errors.New("Not supported RemoteScript type")
}
}
// Output runs the script on the client and returns its standard output.
func (rs *RemoteScript) Output() ([]byte, error) {
if rs.stdout != nil {
return nil, errors.New("Stdout already set")
}
var out bytes.Buffer
rs.stdout = &out
err := rs.Run()
return out.Bytes(), err
}
// SmartOutput runs the script on the client. On success, its standard ouput is returned. On error, its standard error is returned.
func (rs *RemoteScript) SmartOutput() ([]byte, error) {
if rs.stdout != nil {
return nil, errors.New("Stdout already set")
}
if rs.stderr != nil {
return nil, errors.New("Stderr already set")
}
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
rs.stdout = &stdout
rs.stderr = &stderr
err := rs.Run()
if err != nil {
return stderr.Bytes(), err
}
return stdout.Bytes(), err
}
// Cmd appends a command to the RemoteScript.
func (rs *RemoteScript) Cmd(cmd string) *RemoteScript {
_, err := rs.script.WriteString(cmd + "\n")
if err != nil {
rs.err = err
}
return rs
}
// SetStdio specifies where its standard output and error data will be written.
func (rs *RemoteScript) SetStdio(stdout, stderr io.Writer) *RemoteScript {
rs.stdout = stdout
rs.stderr = stderr
return rs
}
func (rs *RemoteScript) runCmd(cmd string) error {
session, err := rs.client.NewSession()
if err != nil {
return err
}
defer session.Close()
session.Stdout = rs.stdout
session.Stderr = rs.stderr
if err := session.Run(cmd); err != nil {
return err
}
return nil
}
func (rs *RemoteScript) runCmds() error {
for {
statment, err := rs.script.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := rs.runCmd(statment); err != nil {
return err
}
}
return nil
}
func (rs *RemoteScript) runScript() error {
session, err := rs.client.NewSession()
if err != nil {
return err
}
session.Stdin = rs.script
session.Stdout = rs.stdout
session.Stderr = rs.stderr
if err := session.Shell(); err != nil {
return err
}
if err := session.Wait(); err != nil {
return err
}
return nil
}
func (rs *RemoteScript) runScriptFile() error {
var buffer bytes.Buffer
file, err := os.Open(rs.scriptFile)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(&buffer, file)
if err != nil {
return err
}
rs.script = &buffer
return rs.runScript()
}
// A RemoteShell represents a login shell on the client.
type RemoteShell struct {
client *ssh.Client
requestPty bool
terminalConfig *TerminalConfig
stdin io.Reader
stdout io.Writer
stderr io.Writer
}
// A TerminalConfig represents the configuration for an interactive shell session.
type TerminalConfig struct {
Term string
Height int
Weight int
Modes ssh.TerminalModes
}
// Terminal create a interactive shell on client.
func (c *Client) Terminal(config *TerminalConfig) *RemoteShell {
return &RemoteShell{
client: c.sshClient,
terminalConfig: config,
requestPty: true,
}
}
// Shell create a noninteractive shell on client.
func (c *Client) Shell() *RemoteShell {
return &RemoteShell{
client: c.sshClient,
requestPty: false,
}
}
// SetStdio specifies where the its standard output and error data will be written.
func (rs *RemoteShell) SetStdio(stdin io.Reader, stdout, stderr io.Writer) *RemoteShell {
rs.stdin = stdin
rs.stdout = stdout
rs.stderr = stderr
return rs
}
// Start starts a remote shell on client.
func (rs *RemoteShell) Start() error {
session, err := rs.client.NewSession()
if err != nil {
return err
}
defer session.Close()
if rs.stdin == nil {
session.Stdin = os.Stdin
} else {
session.Stdin = rs.stdin
}
if rs.stdout == nil {
session.Stdout = os.Stdout
} else {
session.Stdout = rs.stdout
}
if rs.stderr == nil {
session.Stderr = os.Stderr
} else {
session.Stderr = rs.stderr
}
if rs.requestPty {
tc := rs.terminalConfig
if tc == nil {
tc = &TerminalConfig{
Term: "xterm",
Height: 40,
Weight: 80,
}
}
if err := session.RequestPty(tc.Term, tc.Height, tc.Weight, tc.Modes); err != nil {
return err
}
}
if err := session.Shell(); err != nil {
return err
}
if err := session.Wait(); err != nil {
return err
}
return nil
}
// RemoteFileSystem represents a remoote file system.
type RemoteFileSystem struct {
sftp *sftp.Client
err error
config remoteFileSystemConfig
opts []sftp.ClientOption
cli *Client
}
// RemoteFile represents a remote file.
type RemoteFile struct {
*sftp.File
}
// Sftp creates a new SFTP session, using zero or more option functions.
func (c *Client) Sftp(opts ...SftpOption) *RemoteFileSystem {
config := remoteFileSystemConfig{-1, -1, -1, -1, -1}
for _, opt := range opts {
opt(&config)
}
rawRfs, ok := c.sftpSessions.Load(config)
if ok {
return rawRfs.(*RemoteFileSystem)
}
sftpClient, err := sftp.NewClient(c.sshClient, config.sftpClientOptions()...)
rfs := &RemoteFileSystem{
config: config,
sftp: sftpClient,
cli: c,
err: err,
}
if rfs.err == nil {
c.sftpSessions.Store(rfs.config, rfs)
}
return rfs
}
// Close closes the SFTP session.
func (rfs *RemoteFileSystem) Close() error {
if rfs.err != nil {
return rfs.err
}
rfs.cli.sftpSessions.Delete(rfs.config)
return rfs.sftp.Close()
}
func (rfs *RemoteFileSystem) Chmod(path string, mode os.FileMode) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Chmod(path, mode)
}
func (rfs *RemoteFileSystem) Chown(path string, uid, gid int) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Chown(path, uid, gid)
}
func (rfs *RemoteFileSystem) Chtimes(path string, atime time.Time, mtime time.Time) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Chtimes(path, atime, mtime)
}
func (rfs *RemoteFileSystem) Create(path string) (*RemoteFile, error) {
if rfs.err != nil {
return nil, rfs.err
}
file, err := rfs.sftp.Create(path)
if err != nil {
return nil, err
}
return &RemoteFile{file}, nil
}
func (rfs *RemoteFileSystem) Getwd() (string, error) {
if rfs.err != nil {
return "", rfs.err
}
return rfs.sftp.Getwd()
}
func (rfs *RemoteFileSystem) Glob(pattern string) (matches []string, err error) {
if rfs.err != nil {
return nil, rfs.err
}
return rfs.sftp.Glob(pattern)
}
func (rfs *RemoteFileSystem) Link(oldname, newname string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Link(oldname, newname)
}
func (rfs *RemoteFileSystem) Lstat(path string) (os.FileInfo, error) {
if rfs.err != nil {
return nil, rfs.err
}
return rfs.sftp.Lstat(path)
}
func (rfs *RemoteFileSystem) Mkdir(path string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Mkdir(path)
}
func (rfs *RemoteFileSystem) MkdirAll(path string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.MkdirAll(path)
}
func (rfs *RemoteFileSystem) Open(path string) (*RemoteFile, error) {
if rfs.err != nil {
return nil, rfs.err
}
file, err := rfs.sftp.Open(path)
if err != nil {
return nil, err
}
return &RemoteFile{file}, nil
}
func (rfs *RemoteFileSystem) OpenFile(path string, f int) (*RemoteFile, error) {
if rfs.err != nil {
return nil, rfs.err
}
file, err := rfs.sftp.OpenFile(path, f)
if err != nil {
return nil, err
}
return &RemoteFile{file}, nil
}
func (rfs *RemoteFileSystem) PosixRename(oldname, newname string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.PosixRename(oldname, newname)
}
func (rfs *RemoteFileSystem) ReadDir(path string) ([]os.FileInfo, error) {
if rfs.err != nil {
return nil, rfs.err
}
return rfs.sftp.ReadDir(path)
}
func (rfs *RemoteFileSystem) ReadLink(path string) (string, error) {
if rfs.err != nil {
return "", rfs.err
}
return rfs.sftp.ReadLink(path)
}
func (rfs *RemoteFileSystem) RealPath(path string) (string, error) {
if rfs.err != nil {
return "", rfs.err
}
return rfs.sftp.RealPath(path)
}
func (rfs *RemoteFileSystem) Remove(path string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Remove(path)
}
func (rfs *RemoteFileSystem) RemoveDirectory(path string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.RemoveDirectory(path)
}
func (rfs *RemoteFileSystem) Rename(oldname, newname string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Rename(oldname, newname)
}
func (rfs *RemoteFileSystem) Stat(path string) (os.FileInfo, error) {
if rfs.err != nil {
return nil, rfs.err
}
return rfs.sftp.Stat(path)
}
type StatVFS struct {
*sftp.StatVFS
}
func (rfs *RemoteFileSystem) StatVFS(path string) (*StatVFS, error) {
if rfs.err != nil {
return nil, rfs.err
}
stat, err := rfs.sftp.StatVFS(path)
if err != nil {
return nil, err
}
return &StatVFS{stat}, nil
}
func (rfs *RemoteFileSystem) Symlink(oldname, newname string) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Symlink(oldname, newname)
}
func (rfs *RemoteFileSystem) Truncate(path string, size int64) error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Truncate(path, size)
}
func (rfs *RemoteFileSystem) Wait() error {
if rfs.err != nil {
return rfs.err
}
return rfs.sftp.Wait()
}
func (rfs *RemoteFileSystem) Walk(root string) (*fs.Walker, error) {
if rfs.err != nil {
return nil, rfs.err
}
return rfs.sftp.Walk(root), nil
}
func (rfs *RemoteFileSystem) Upload(hostPath, remotePath string) (retErr error) {
if rfs.err != nil {
return rfs.err
}
multierr := newMultiError()
defer func() {
retErr = multierr.ErrorOrNil()
}()
hostFile, err := os.Open(hostPath)
if err != nil {
return multierr.Append(err)
}
defer multierr.AppendErrFunc(hostFile.Close)
remoteFile, err := rfs.sftp.OpenFile(remotePath, os.O_CREATE|os.O_WRONLY)
if err != nil {
return multierr.Append(err)
}
defer multierr.AppendErrFunc(remoteFile.Close)
_, err = remoteFile.ReadFrom(hostFile)
return multierr.Append(err)
}
func (rfs *RemoteFileSystem) Download(remotePath, hostPath string) (retErr error) {
if rfs.err != nil {
return rfs.err
}
multierr := newMultiError()
defer func() {
retErr = multierr.ErrorOrNil()
}()
remoteFile, err := rfs.sftp.Open(remotePath)
if err != nil {
return multierr.Append(err)
}
defer multierr.AppendErrFunc(remoteFile.Close)
hostFile, err := os.OpenFile(hostPath, os.O_CREATE|os.O_WRONLY, os.ModeAppend|os.ModePerm)
if err != nil {
return multierr.Append(err)
}
defer multierr.AppendErrFunc(hostFile.Close)
_, err = remoteFile.WriteTo(hostFile)
return multierr.Append(err)
}
func (rfs *RemoteFileSystem) ReadFile(name string) ([]byte, error) {
if rfs.err != nil {
return nil, rfs.err
}
f, err := rfs.sftp.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
var size int
if info, err := f.Stat(); err == nil {
size64 := info.Size()
if int64(int(size64)) == size64 {
size = int(size64)
}
}
size++ // one byte for final read at EOF
// If a file claims a small size, read at least 512 bytes.
// In particular, files in Linux's /proc claim size 0 but
// then do not work right if read in small pieces,
// so an initial read of 1 byte would not work correctly.
if size < 512 {
size = 512
}
data := make([]byte, 0, size)
for {
if len(data) >= cap(data) {
d := append(data[:cap(data)], 0)
data = d[:len(data)]
}
n, err := f.Read(data[len(data):cap(data)])
data = data[:len(data)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return data, err
}
}
}
func (rfs *RemoteFileSystem) WriteFile(name string, data []byte, perm os.FileMode) error {
if rfs.err != nil {
return rfs.err
}
f, err := rfs.sftp.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
if err != nil {
return err
}
_, err = f.Write(data)
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
return err
}