當前位置: 首頁>>代碼示例>>Golang>>正文


Golang sftp.Close函數代碼示例

本文整理匯總了Golang中github.com/pkg/sftp.Close函數的典型用法代碼示例。如果您正苦於以下問題:Golang Close函數的具體用法?Golang Close怎麽用?Golang Close使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Close函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: UploadScript

func (i *blueBoxInstance) UploadScript(ctx gocontext.Context, script []byte) error {
	client, err := i.sshClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	sftp, err := sftp.NewClient(client)
	if err != nil {
		return err
	}
	defer sftp.Close()

	_, err = sftp.Lstat("build.sh")
	if err == nil {
		return ErrStaleVM
	}

	f, err := sftp.Create("build.sh")
	if err != nil {
		return err
	}

	_, err = f.Write(script)
	if err != nil {
		return err
	}

	return err
}
開發者ID:henrikhodne-abandoned,項目名稱:worker,代碼行數:30,代碼來源:bluebox.go

示例2: Put

func (conn *SshConfig) Put(local, remote string) error {
	sftp, err := conn.transport()
	if err != nil {
		return err
	}

	defer sftp.Close()

	src, err := ioutil.ReadFile(local)
	if err != nil {
		return err
	}

	dst, err := sftp.Create(remote)
	if err != nil {
		return errors.New("Could not create the remote file.")
	}

	fmt.Printf("%s --> %s:%s\n", local, conn.Name, remote)
	if _, err := dst.Write(src); err != nil {
		return err
	}

	return nil
}
開發者ID:kbdhero,項目名稱:cogs,代碼行數:25,代碼來源:ftp.go

示例3: Create

// Create creates all the necessary files and directories for a new sftp
// backend at dir. Afterwards a new config blob should be created. `dir` must
// be delimited by forward slashes ("/"), which is required by sftp.
func Create(dir string, program string, args ...string) (*SFTP, error) {
	debug.Log("%v %v", program, args)
	sftp, err := startClient(program, args...)
	if err != nil {
		return nil, err
	}

	// test if config file already exists
	_, err = sftp.c.Lstat(Join(dir, backend.Paths.Config))
	if err == nil {
		return nil, errors.New("config file already exists")
	}

	// create paths for data, refs and temp blobs
	for _, d := range paths(dir) {
		err = sftp.mkdirAll(d, backend.Modes.Dir)
		if err != nil {
			return nil, err
		}
	}

	err = sftp.Close()
	if err != nil {
		return nil, errors.Wrap(err, "Close")
	}

	// open backend
	return Open(dir, program, args...)
}
開發者ID:ckemper67,項目名稱:restic,代碼行數:32,代碼來源:sftp.go

示例4: copyFile

func (c *DockerDeployClient) copyFile(source string, target string) error {
	sftp, err := sftp.NewClient(c.sshClient)
	if err != nil {
		log.Fatalf("Could not initialize SFTP connection: %v", err)
	}
	defer sftp.Close()

	tf, err := sftp.Create(target)
	if err != nil {
		return err
	}
	defer tf.Close()

	sf, err := os.Open(source)
	if err != nil {
		return err
	}
	defer sf.Close()

	n, err := io.Copy(tf, sf)
	if err != nil {
		return err
	}

	log.Printf("Artifact from %v to %v copied. %v Bytes transferred.", source, target, n)
	return nil
}
開發者ID:herzog31,項目名稱:docker-deploy-bamboo-go,代碼行數:27,代碼來源:client.go

示例5: Rollback

func Rollback(rollback int, deploy_config setup.DeployConfiguration, commands setup.CommandConfig) {
	/* remove the current directory, and switch back to version current - number
	 */
	connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath)
	if err != nil {
		log.Panic(err)
	}
	sftp, err := sftp.NewClient(connection)
	if err != nil {
		log.Fatal(err)
	}
	defer sftp.Close()

	var build_list []string

	w, err := sftp.ReadDir(deploy_config.TargetDirectory)
	for _, folder := range w {
		if folder.Name() == "current" || !folder.IsDir() {
			continue
		}
		build_list = append(build_list, folder.Name())
	}
	sort.Sort(ByDate(build_list))

	rollback_version := build_list[len(build_list)-rollback] //list is ordered old to new, so we want to index in relation to the end of the list.

	//Create a symbolic link to the new directory.
	cmd := fmt.Sprintf("unlink %s", deploy_config.TargetDirectory+"/current")
	ExecuteCmd(connection, cmd, nil, nil)

	cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+rollback_version, deploy_config.TargetDirectory+"/current")
	ExecuteCmd(connection, cmd, nil, nil)
}
開發者ID:newxan,項目名稱:gopistrano,代碼行數:33,代碼來源:control.go

示例6: Example

func Example(conn *ssh.ClientConn) {
	// open an SFTP sesison over an existing ssh connection.
	sftp, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatal(err)
	}
	defer sftp.Close()

	// walk a directory
	w := sftp.Walk("/home/user")
	for w.Step() {
		if w.Err() != nil {
			continue
		}
		log.Println(w.Path())
	}

	// leave your mark
	f, err := sftp.Create("hello.txt")
	if err != nil {
		log.Fatal(err)
	}
	if _, err := f.Write([]byte("Hello world!")); err != nil {
		log.Fatal(err)
	}

	// check it's there
	fi, err := sftp.Lstat("hello.txt")
	if err != nil {
		log.Fatal(err)
	}
	log.Println(fi)
}
開發者ID:kr,項目名稱:sftp,代碼行數:33,代碼來源:example_test.go

示例7: UploadFile

func (c *sshClientImpl) UploadFile(username string, password string, ip string, srcFile string, destFile string) error {
	config := &myssh.ClientConfig{
		User: username,
		Auth: []myssh.AuthMethod{
			myssh.Password(password),
		},
	}
	if !IsIP(ip) {
		return errors.New("invalid IP address")
	}

	if IsDir(srcFile) || IsDir(destFile) {
		return errors.New("Is a directory")
	}

	client, err := myssh.Dial("tcp", ip+":22", config)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer client.Close()

	sftp, err := sftp.NewClient(client)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer sftp.Close()

	data, err := ioutil.ReadFile(srcFile)
	if err != nil {
		log.Fatal(err)
		return err
	}

	f, err := sftp.Create(destFile)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer f.Close()

	_, err = f.Write([]byte(data))
	if err != nil {
		log.Fatal(err)
		return err
	}

	_, err = sftp.Lstat(destFile)
	if err != nil {
		log.Fatal(err)
		return err
	}
	return nil
}
開發者ID:ardnaxelarak,項目名稱:bosh-softlayer-cpi,代碼行數:55,代碼來源:ssh_client.go

示例8: create_version

/*
	Create a new version, remove oldest if there are more than *Rollback
	return the new version number so that the deploy knows where to place shit.
*/
func create_version(deploy_config setup.DeployConfiguration) string {
	connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath)
	if err != nil {
		log.Panic(err)
	}
	sftp, err := sftp.NewClient(connection)
	if err != nil {
		log.Fatal(err)
	}
	defer sftp.Close()

	new_version := time.Now().Format("20060102150405")

	var build_list []string

	w, err := sftp.ReadDir(deploy_config.TargetDirectory)
	for _, folder := range w {
		if folder.Name() == "current" || !folder.IsDir() {
			continue
		}
		build_list = append(build_list, folder.Name())
	}
	sort.Sort(ByDate(build_list))

	//Remove oldest in build_list
	if len(build_list)+1 > deploy_config.Rollbacks+1 {
		remove_list := build_list[:len(build_list)-deploy_config.Rollbacks] //remove any old that exceeds Rollbaks, which are in the start of the list
		for _, version := range remove_list {
			log.Printf("Removing version: %s\n", deploy_config.TargetDirectory+"/"+version)
			//err :=sftp.Remove(deploy_config.TargetDirectory + "/" + version)
			cmd := fmt.Sprintf("rm -r %s", deploy_config.TargetDirectory+"/"+version)
			err := ExecuteCmd(connection, cmd, nil, nil)
			if err != nil {
				log.Println(err)
			}
		}
	}

	log.Printf("Creating folder: %s\n", deploy_config.TargetDirectory+"/"+new_version)
	sftp.Mkdir(deploy_config.TargetDirectory + "/" + new_version)

	//Create a symbolic link to the new directory.
	cmd := fmt.Sprintf("unlink %s", deploy_config.TargetDirectory+"/current")
	//log.Printf("Removing old symlink: %s", cmd)
	ExecuteCmd(connection, cmd, nil, nil)

	cmd = fmt.Sprintf("ln -s -f %s %s", deploy_config.TargetDirectory+"/"+new_version, deploy_config.TargetDirectory+"/current")
	//log.Printf("Creating symlink: %s", cmd)
	ExecuteCmd(connection, cmd, nil, nil)

	return new_version
}
開發者ID:newxan,項目名稱:gopistrano,代碼行數:56,代碼來源:transfer.go

示例9: main

func main() {
	if err := resolveSettings(pars); err != nil {
		log.Fatal(err)
	}

	conn, err := initSSH(pars)
	if err != nil {
		log.Fatal(err)
	}

	sftp, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatal(err)
	}
	defer sftp.Close()

	finStat, err := os.Stat(inPath)
	if err != nil {
		log.Fatal(err)
	}
	origSize := finStat.Size()

	fin, err := os.Open(inPath)
	if err != nil {
		log.Fatal(err)
	}
	defer fin.Close()

	outPath := path.Clean(path.Join(pars.DstFolder, filepath.Base(inPath)))
	fout, err := sftp.Create(outPath)
	if err != nil {
		log.Fatal(err)
	}
	if _, err := io.Copy(fout, fin); err != nil {
		log.Fatal(err)
	}

	foutStat, err := sftp.Lstat(outPath)
	if err != nil {
		log.Fatal(err)
	}
	if finalSize := foutStat.Size(); origSize != finalSize {
		if err := sftp.Remove(outPath); err != nil {
			log.Printf("[ERROR] Unable to remove target file: %v", err)
		}
		log.Fatalf("Failed to upload %s to %s: expected %d bytes, got %d (missing %d)",
			inPath, outPath, origSize, finalSize, origSize-finalSize)
	}

	fmt.Println("Successfully uploaded", outPath)
}
開發者ID:alexaandru,項目名稱:up,代碼行數:51,代碼來源:main.go

示例10: DownloadFile

func (c *sshClientImpl) DownloadFile(username string, password string, ip string, srcFile string, destFile string) error {
	config := &myssh.ClientConfig{
		User: username,
		Auth: []myssh.AuthMethod{
			myssh.Password(password),
		},
	}

	if !IsIP(ip) {
		return errors.New("invalid IP address")
	}

	if IsDir(srcFile) || IsDir(destFile) {
		return errors.New("Is a directory")
	}

	client, err := myssh.Dial("tcp", ip+":22", config)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer client.Close()

	sftp, err := sftp.NewClient(client)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer sftp.Close()

	pFile, err := sftp.Open(srcFile)
	if err != nil {
		log.Fatal(err)
		return err
	}
	defer pFile.Close()

	data, err := ioutil.ReadAll(pFile)
	if err != nil {
		log.Fatal(err)
		return err
	}

	err = ioutil.WriteFile(destFile, data, 0755)
	if err != nil {
		log.Fatal(err)
		return err
	}

	return nil
}
開發者ID:ardnaxelarak,項目名稱:bosh-softlayer-cpi,代碼行數:51,代碼來源:ssh_client.go

示例11: deploy_local

func deploy_local(deploy_config setup.DeployConfiguration, commands setup.CommandConfig) {
	connection, err := NewConnection(deploy_config.TargetHost, deploy_config.TargetPort, deploy_config.Username, deploy_config.Password, deploy_config.PublicKeyPath)
	if err != nil {
		log.Panic(err)
	}

	//If this is the first time deploying, we need to create the path to the target directory
	if !CheckTargetDirectory(connection, deploy_config) {
		cmd := fmt.Sprintf("mkdir -p %s", deploy_config.TargetDirectory)
		ExecuteCmd(connection, cmd, nil, nil)
		//cmd = fmt.Sprintf("mkdir -p %s", deploy_config.TargetDirectory+"/current")
		//ExecuteCmd(connection, cmd, nil, nil)
	}

	new_version := create_version(deploy_config)

	sftp, err := sftp.NewClient(connection)
	if err != nil {
		log.Fatal(err)
	}
	defer sftp.Close()

	files := list_local_directory(deploy_config.Ignores)

	//TODO use "current" instead of the new_version directory, since we have a symbolic link
	for _, file := range files {
		content, _ := ioutil.ReadFile(file)
		file = strings.Replace(file, `\`, `/`, -1)
		target := deploy_config.TargetDirectory + "/" + new_version + "/" + file
		tree := deploy_config.TargetDirectory + "/" + new_version + "/"

		for _, dir := range strings.Split(file, "/")[:len(strings.Split(file, "/"))-1] {
			tree += dir + "/"
			log.Printf("Creating directory: %s\n", tree)
			err := sftp.Mkdir(tree)
			if err != nil {

			}
		}
		log.Printf("Creating file: %s\n", target)
		f, err := sftp.Create(target)
		if err != nil {
			log.Panic(err)
		}
		if _, err := f.Write([]byte(content)); err != nil {
			log.Panic(err)
		}
	}
}
開發者ID:newxan,項目名稱:gopistrano,代碼行數:49,代碼來源:transfer.go

示例12: ReadAll

// Read a remote file and return the contents.
func (c *Client) ReadAll(filepath string) ([]byte, error) {
	sftp, err := c.SFTPClient()
	if err != nil {
		panic(err)
	}
	defer sftp.Close()

	file, err := sftp.Open(filepath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	return ioutil.ReadAll(file)
}
開發者ID:bvnarayan,項目名稱:simplessh,代碼行數:16,代碼來源:simplessh.go

示例13: main

func main() {

	// Attempt to open the input file
	flag.Parse()

	csv, err := os.OpenFile(*input, os.O_RDONLY, 0)
	if err != nil {
		log.Fatalln(err)
	}
	defer csv.Close()
	reader := bufio.NewReader(csv)
	if err != nil {
		log.Fatalln(err)
	}

	// SSH connection.
	config := &ssh.ClientConfig{
		User: *user,
		Auth: []ssh.AuthMethod{
			ssh.Password(*secret),
		},
	}

	conn, err := ssh.Dial("tcp", *address, config)
	if err != nil {
		log.Fatalln(err)
	}
	defer conn.Close()

	// SFTP client.
	sftp, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatalln(err)
	}
	defer sftp.Close()

	// Upload over SFTP.
	sftpFile, err := sftp.OpenFile(filepath.Join(*directory, *file), os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
	if err != nil {
		log.Fatalln(err)
	}
	defer sftpFile.Close()
	sftpFile.ReadFrom(reader)
}
開發者ID:contactlab,項目名稱:CLABPush-Go,代碼行數:44,代碼來源:main.go

示例14: copyToRemote

func copyToRemote(c *cli.Context) {
	for _, flag := range []string{"keyfile", "to", "host", "user"} {
		if c.Generic(flag) == nil {
			log.Fatalf("flag %s is required\n", flag)
		}
	}
	// process the keyfile
	buf, err := ioutil.ReadFile(c.String("keyfile"))
	if err != nil {
		log.Fatalf("error in reading private key file %s\n", err)
	}
	key, err := ssh.ParsePrivateKey(buf)
	if err != nil {
		log.Fatalf("error in parsing private key %s\n", key)
	}
	// client config
	config := &ssh.ClientConfig{
		User: c.String("user"),
		Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
	}
	// connection
	client, err := ssh.Dial("tcp", c.String("host")+":22", config)
	if err != nil {
		log.Fatalf("error in ssh connection %s\n", err)
	}
	defer client.Close()
	// sftp handler
	sftp, err := sftp.NewClient(client)
	if err != nil {
		log.Fatalf("error in sftp connection %s\n", err)
	}
	defer sftp.Close()
	// Remote file
	r, err := sftp.Create(filepath.Join(c.String("to"), "go_sftp.txt"))
	if err != nil {
		log.Fatalf("error in creating remote file %s\n", err)
	}
	l := strings.NewReader("Writing through golang sftp")
	if _, err := io.Copy(r, l); err != nil {
		log.Fatalf("error in writing file to remote system %s\n", err)
	}
	log.Println("written new file to remote system")
}
開發者ID:dictyBase,項目名稱:webhooks,代碼行數:43,代碼來源:scp.go

示例15: UploadScript

func (i *jupiterBrainInstance) UploadScript(ctx context.Context, script []byte) error {
	client, err := i.sshClient()
	if err != nil {
		return err
	}
	defer client.Close()

	sftp, err := sftp.NewClient(client)
	if err != nil {
		return err
	}
	defer sftp.Close()

	_, err = sftp.Lstat("build.sh")
	if err == nil {
		return ErrStaleVM
	}

	f, err := sftp.Create("build.sh")
	if err != nil {
		return err
	}

	_, err = f.Write(script)
	if err != nil {
		return err
	}

	f, err = sftp.Create("wrapper.sh")
	if err != nil {
		return err
	}

	_, err = fmt.Fprintf(f, wrapperSh)

	return err
}
開發者ID:henrikhodne-abandoned,項目名稱:worker,代碼行數:37,代碼來源:jupiterbrain.go


注:本文中的github.com/pkg/sftp.Close函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。