本文整理汇总了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
}
示例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
}
示例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...)
}
示例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
}
示例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)
}
示例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)
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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)
}
}
}
示例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)
}
示例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)
}
示例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")
}
示例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
}