// make ftp directories, // need make sub and parent directories func makeFtpDir(client *ftp.ServerConn, dirs []string) error { for i := len(dirs) - 1; i >= 0; i-- { if err := client.MakeDir(dirs[i]); err != nil { return err } } return nil }
// upload files without checking diff status func ftpUploadAll(client *ftp.ServerConn, local string) error { return filepath.Walk(local, func(path string, info os.FileInfo, err error) error { if info.IsDir() { return nil } rel, _ := filepath.Rel(local, path) rel = filepath.ToSlash(rel) makeFtpDir(client, getRecursiveDirs(filepath.Dir(rel))) // upload file f, err := os.Open(path) if err != nil { return err } defer f.Close() if err = client.Stor(rel, f); err != nil { return err } log15.Debug("FTP|Stor|%s", path) 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 }) }
// 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 }) }