// upload all files ignore diff status func (ft *FtpTask) uploadAllFiles(client *ftp.ServerConn, 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) // entry remove status, just remove it // the other files, just upload ignore diff status if entry.Behavior == builder.DIFF_REMOVE { log15.Debug("Deploy.Ftp.Delete", "file", rel) return client.Delete(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.MakeDir(dir); err == nil { createdDirs[dir] = true } } } } // upload file f, err := os.Open(name) if err != nil { return err } defer f.Close() if err = client.Stor(rel, f); err != nil { return err } log15.Debug("Deploy.Ftp.Stor", "file", rel) return nil }) }
// upload files with checking diff status func (ft *FtpTask) uploadDiffFiles(client *ftp.ServerConn, 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.Ftp.Delete", "file", rel) return client.Delete(rel) } if entry.Behavior == builder.DIFF_KEEP { if list, _ := client.List(rel); len(list) == 1 { // entry file should be older than uploaded file if entry.Time.Sub(list[0].Time).Seconds() < 0 { return nil } } } dirs := getDirs(path.Dir(rel)) for i := len(dirs) - 1; i >= 0; i-- { client.MakeDir(dirs[i]) } // upload file f, err := os.Open(name) if err != nil { return err } defer f.Close() if err = client.Stor(rel, f); err != nil { return err } log15.Debug("Deploy.Ftp.Stor", "file", rel) return nil }) }