func (ft *SftpTask) uploadAllFiles(client *sftp.Client, ctx *builder.Context) error { var ( createdDirs = make(map[string]bool) err error ) return ctx.Diff.Walk(func(name string, entry *builder.DiffEntry) error { rel, _ := filepath.Rel(ctx.DstDir, name) rel = filepath.ToSlash(rel) if entry.Behavior == builder.DIFF_REMOVE { log15.Debug("Deploy.Sftp.Delete", "file", rel) return client.Remove(path.Join(ft.opt.Directory, rel)) } // create directory recursive dirs := getDirs(path.Dir(rel)) if len(dirs) > 0 { for i := len(dirs) - 1; i >= 0; i-- { dir := dirs[i] if !createdDirs[dir] { if err = client.Mkdir(path.Join(ft.opt.Directory, dir)); err == nil { createdDirs[dir] = true } } } } // upload file f, err := os.Open(name) if err != nil { return err } defer f.Close() f2, err := client.Create(path.Join(ft.opt.Directory, rel)) if err != nil { return err } defer f2.Close() if _, err = io.Copy(f2, f); err != nil { return err } log15.Debug("Deploy.Sftp.Stor", "file", rel) return nil }) }
func (ft *SftpTask) uploadDiffFiles(client *sftp.Client, ctx *builder.Context) error { return ctx.Diff.Walk(func(name string, entry *builder.DiffEntry) error { rel, _ := filepath.Rel(ctx.DstDir, name) rel = filepath.ToSlash(rel) if entry.Behavior == builder.DIFF_REMOVE { log15.Debug("Deploy.Sftp.Delete", "file", rel) return client.Remove(path.Join(ft.opt.Directory, rel)) } target := path.Join(ft.opt.Directory, rel) if entry.Behavior == builder.DIFF_KEEP { if fi, _ := client.Stat(target); fi != nil { // entry file should be older than uploaded file if entry.Time.Sub(fi.ModTime()).Seconds() < 0 { return nil } } } dirs := getDirs(path.Dir(rel)) for i := len(dirs) - 1; i >= 0; i-- { client.Mkdir(path.Join(ft.opt.Directory, dirs[i])) } // upload file f, err := os.Open(name) if err != nil { return err } defer f.Close() f2, err := client.Create(target) if err != nil { return err } defer f2.Close() if _, err = io.Copy(f2, f); err != nil { return err } log15.Debug("Deploy.Sftp.Stor", "file", rel) return nil }) }
func ProcessDelete(client *sftp.Client, dc <-chan file, dryRun bool, done chan<- bool) { for d := range dc { if dryRun { log.Info("!DEL", "path", d.relPath, "size", d.size) continue } log.Info("DEL", "path", d.relPath) err := client.Remove(d.path) if err != nil { log.Warn("DEL", "path", d.relPath, "err", err) continue } } done <- true }