forked from go-xiaohei/pugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp.go
146 lines (133 loc) · 3.21 KB
/
sftp.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
package deploy
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/Unknwon/com"
"github.com/pkg/sftp"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh"
"gopkg.in/inconshreveable/log15.v2"
)
type Sftp struct {
Host string
User string
Password string
Directory string
Local string
sshClient *ssh.Client
sftpClient *sftp.Client
}
// Command return sftp deploy command
func (s *Sftp) Command() cli.Command {
return cli.Command{
Name: "sftp",
Usage: "deploy via SSH account with SFTP",
Flags: []cli.Flag{
cli.StringFlag{Name: "local", Value: "dest", Usage: "local website directory"},
cli.StringFlag{Name: "user", Usage: "ssh account name"},
cli.StringFlag{Name: "password", Usage: "ssh account password"},
cli.StringFlag{Name: "host", Usage: "ssh server address"},
cli.StringFlag{Name: "directory", Usage: "sftp directory"},
},
Action: func(ctx *cli.Context) {
s2, err := s.Create(ctx)
if err != nil {
log15.Error("SFTP|Fail|%s", err.Error())
return
}
if err = s2.Do(); err != nil {
log15.Error("SFTP|Fail|%s", err.Error())
return
}
log15.Info("SFTP|Finish")
},
}
}
func (s *Sftp) String() string {
return "SFTP"
}
func (s *Sftp) Create(ctx *cli.Context) (Method, error) {
s2 := &Sftp{
Host: ctx.String("host"),
User: ctx.String("user"),
Password: ctx.String("password"),
Directory: ctx.String("directory"),
Local: ctx.String("local"),
}
if !com.IsDir(s2.Local) {
return nil, fmt.Errorf("directory '%s' is empty", s2.Local)
}
if s2.Host == "" {
return nil, fmt.Errorf("host is empty")
}
if s2.User == "" || s2.Password == "" {
log15.Warn("SFTP|No user or password")
}
if strings.HasPrefix(s2.Directory, "/~") {
s2.Directory = strings.TrimPrefix(s2.Directory, "/~/")
}
return s2, nil
}
func (s *Sftp) Do() error {
if err := s.connect(); err != nil {
return err
}
defer s.sftpClient.Close()
defer s.sshClient.Close()
log15.Debug("SFTP|%s|Connect", s.Host)
makeSftpDir(s.sftpClient, getRecursiveDirs(s.Directory))
log15.Debug("SFTP|UploadAll")
return s.UploadAll(s.Local)
}
func (s *Sftp) connect() error {
conf := &ssh.ClientConfig{
User: s.User,
Auth: []ssh.AuthMethod{
ssh.Password(s.Password),
},
}
client, err := ssh.Dial("tcp", s.Host, conf)
if err != nil {
return err
}
s.sshClient = client
s.sftpClient, err = sftp.NewClient(client)
return err
}
// upload files without checking diff status
func (s *Sftp) UploadAll(local string) error {
return filepath.Walk(local, func(p string, info os.FileInfo, err error) error {
rel, _ := filepath.Rel(local, p)
rel = filepath.ToSlash(rel)
makeSftpDir(s.sftpClient, getRecursiveDirs(filepath.Dir(rel)))
// upload file
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
f2, err := s.sftpClient.Create(path.Join(s.Directory, rel))
if err != nil {
return err
}
defer f2.Close()
if _, err = io.Copy(f2, f); err != nil {
return err
}
log15.Debug("SFTP|Stor|%s", p)
return nil
})
}
// make sftp directories
func makeSftpDir(client *sftp.Client, dirs []string) error {
for i := len(dirs) - 1; i >= 0; i-- {
if err := client.Mkdir(dirs[i]); err != nil {
return err
}
}
return nil
}