Example #1
0
// ftp task do action
func (ft *FtpTask) Do(b *builder.Builder, ctx *builder.Context) error {
	client, err := ftp.DialTimeout(ft.opt.url.Host, time.Second*10)
	if err != nil {
		return err
	}
	log15.Debug("Deploy.[" + ft.opt.Address + "].Connect")
	defer client.Quit()
	if ft.opt.User != "" {
		if err = client.Login(ft.opt.User, ft.opt.Password); err != nil {
			return err
		}
	}
	log15.Debug("Deploy.[" + ft.opt.Address + "].Logged")
	ftpDir := strings.TrimPrefix(ft.opt.url.Path, "/")

	// change to UTF-8 mode
	if _, _, err = client.Exec(ftp.StatusCommandOK, "OPTS UTF8 ON"); err != nil {
		return err
	}

	if _, ok := client.Features()["UTF8"]; !ok {
		return errors.New("FTP server need utf-8 support")
	}

	// make dir
	makeFtpDir(client, getDirs(ftpDir))

	// change  to directory
	if err = client.ChangeDir(ftpDir); err != nil {
		return err
	}

	if b.Count < 2 {
		log15.Debug("Deploy.[" + ft.opt.Address + "].UploadAll")
		return ft.uploadAllFiles(client, ctx)
	}

	log15.Debug("Deploy.[" + ft.opt.Address + "].UploadDiff")
	return ft.uploadDiffFiles(client, ctx)
}
Example #2
0
// Do do ftp deploy process
func (f *Ftp) Do() error {
	// connect to ftp
	client, err := ftp.DialTimeout(f.Host, time.Second*10)
	if err != nil {
		return err
	}
	log15.Info("FTP|%s|Connect", f.Host)
	defer client.Quit()
	if f.User != "" {
		if err = client.Login(f.User, f.Password); err != nil {
			return err
		}
	}

	// change to UTF-8 mode
	log15.Debug("FTP|%s|UTF-8", f.Host)
	if _, _, err = client.Exec(ftp.StatusCommandOK, "OPTS UTF8 ON"); err != nil {
		if !strings.Contains(err.Error(), "No need to") { // sometimes show 202, no need to set UTF8 mode because always on
			return fmt.Errorf("OPTS UTF8 ON:%s", err.Error())
		}
	}
	if _, ok := client.Features()["UTF8"]; !ok {
		return fmt.Errorf("FTP server need utf-8 support")
	}

	// make dir
	makeFtpDir(client, getRecursiveDirs(f.Directory))

	// change  to directory
	if err = client.ChangeDir(f.Directory); err != nil {
		return err
	}

	log15.Debug("FTP|UploadAll")
	return ftpUploadAll(client, f.Local)
}