-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
269 lines (222 loc) · 6.59 KB
/
push.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
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
package main
import (
"flag"
"github.com/mitchellh/cli"
"log"
"os"
"path/filepath"
"time"
)
type PushCommand struct {
Ui cli.Ui
}
func pushCmdFactory() (cli.Command, error) {
ui := &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
return &PushCommand{
Ui: &cli.ColoredUi{
Ui: ui,
OutputColor: cli.UiColorBlue,
},
}, nil
}
func (c *PushCommand) Run(args []string) int {
// flags
var cfgfile string
var err error
xferverb = "sent"
cmdFlags := flag.NewFlagSet("push", flag.ContinueOnError)
cmdFlags.StringVar(&profile, "profile", "default", "sftp session profile to use")
cmdFlags.StringVar(&cfgfile, "config", "config.ini", "config file in git config ini format")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
// config
section, err := InitialiseConfig(cfgfile)
if err != nil {
log.Fatalf("error : failed to initialise config : %s", err)
return 1
}
sess, err := NewSftpSession(section)
if err != nil {
log.Printf("%s\n", err)
return 1
}
defer sess.Close()
// run email check at end
defer sess.TriggerEmail(err)
// build local file list
filepath.Walk(config.Profile[profile].LocalDir, sess.WalkLocal)
// if file list connect
if len(sess.LocalFiles) > 0 {
// build remote file list
err = sess.WalkRemote()
if err != nil {
log.Printf("unable to walk remote server: %v", err)
return 95
}
// check remote dest directory
if _, ok := sess.RemoteFiles["."]; ok {
if debug {
log.Printf("DEBUG remote directory %s already exists on remote\n", config.Profile[profile].RemoteDir)
}
} else {
err = sess.MkDirRemote(config.Profile[profile].RemoteDir, 0700)
}
// for each local file, if it isn't on remote, send
for path := range sess.LocalFiles {
var lsize, rsize int64 = 0, 0
rfinfo, rexists := sess.RemoteFiles[path]
lfinfo := sess.LocalFiles[path]
lsize = lfinfo.Size()
mode := lfinfo.Mode()
if rexists {
rsize = rfinfo.Size()
}
if lfinfo.IsDir() {
if debug {
log.Printf("DEBUG processing local directory : %s, %d\n", path, lsize)
}
// prepend the remote dir to the remote file path
rfile := filepath.Join(config.Profile[profile].RemoteDir, path)
if !rexists {
sess.MkDirRemote(rfile, mode)
if err != nil {
log.Printf("push error creating remote directory : %s, %s\n", rfile, err)
sess.Bad = append(sess.Bad, FileError{path: path, err: err})
continue
}
}
if archive {
archivepath := filepath.Join(sess.section.ArchiveDir, path)
if _, err := os.Stat(archivepath); err != nil {
if os.IsNotExist(err) {
// dir does not exist
err = os.Mkdir(archivepath, mode)
if err != nil {
log.Printf("error creating archive directory path : %s, %s\n", archivepath, err)
}
if debug {
log.Printf("DEBUG created archive dir %s\n", archivepath)
}
}
}
}
// enough for a directory
continue
}
if debug {
log.Printf("DEBUG processing local file : %s, %d\n", path, lsize)
}
// ignore if it is on remote
if rexists && lsize == rsize {
if debug {
log.Printf("DEBUG path %s already exists on remote with size %d bytes\n", path, lsize)
}
continue
} else {
sess.GetFiles[path] = lfinfo
}
pcount := len(sess.GetFiles)
if pcount > 0 {
log.Printf("found %d local files for upload\n", pcount)
} else {
log.Printf("no local files eligible for upload.\n")
return 0
}
// if stable check enabled, wait StableDuration seconds
if stable {
log.Printf("stable check enabled - pausing for %d to confirm remote file size stability\n", stableblock)
time.Sleep(time.Duration(stableblock) * time.Second)
log.Printf("stable check pause complete.\n")
}
for path := range sess.GetFiles {
var archivepath string
// if it isn't on remote and it's a different size, send it
// prepend the remote dir to the remote file path
rfile := filepath.Join(config.Profile[profile].RemoteDir, path)
// prepend the local dir to the local file path
lfilepath := filepath.Join(config.Profile[profile].LocalDir, path)
lfilesrc := lfilepath
mode := lfinfo.Mode()
if sess.section.Encrypt {
if matched := sess.Ecryptregexp.MatchString(path); matched {
lfilepath, lfinfo, err = sess.EncryptFile(lfilepath)
if err != nil {
log.Printf("push error encrypting file : %s, %s\n", lfilepath, err)
sess.Bad = append(sess.Bad, FileError{path: path, err: err})
continue
}
lsize = lfinfo.Size()
log.Printf("push encrypted file: %s\n", lfilepath)
rfile = rfile + sess.section.EncryptSuffix
}
} else {
log.Printf("push file %s size %d\n", rfile, lsize)
}
err = sess.Push(lfilepath, rfile, lsize, mode)
if err != nil {
log.Printf("error pushing file : %s %s\n", path, err)
sess.Bad = append(sess.Bad, FileError{path: path, err: err})
// bail??
continue
}
if archive {
archivepath = filepath.Join(sess.section.ArchiveDir, path)
// copy lfilepath to archivepath
err = sess.CopyFile(lfilepath, archivepath)
if err != nil {
log.Printf("error archiving file : %s %s\n", path, err)
sess.Bad = append(sess.Bad, FileError{path: path, err: err})
continue
}
if debug {
log.Printf("DEBUG archive file %s\n", archivepath)
}
}
if clean {
if sess.section.Encrypt {
err = os.Remove(lfilepath)
if err != nil {
log.Printf("clean error removing : %s, %s", lfilepath, err)
sess.Bad = append(sess.Bad, FileError{path: path, err: err})
}
log.Printf("cleaned : %s", lfilepath)
}
err = os.Remove(lfilesrc)
if err != nil {
log.Printf("clean error removing : %s, %s", lfilesrc, err)
sess.Bad = append(sess.Bad, FileError{path: path, err: err})
}
log.Printf("cleaned : %s", lfilesrc)
}
sess.Good = append(sess.Good, path)
}
}
}
// summarise results
if len(sess.Good) > 0 {
log.Printf("%d files successfully pushed\n", len(sess.Good))
for i := range sess.Good {
log.Printf("pushed: %s\n", sess.Good[i])
}
}
if len(sess.Bad) > 0 {
log.Printf("%d files had errors\n", len(sess.Bad))
for i := range sess.Bad {
log.Printf("not pushed: %s %s\n", sess.Bad[i].path, sess.Bad[i].err.Error())
}
return 1
}
return 0
}
func (c *PushCommand) Help() string {
return "help: push files to a remote sftp server"
}
func (c *PushCommand) Synopsis() string {
return "synopsis: push files to a remote sftp server"
}