复制文件
(localFilePath string, remoteFilepath string)
| 730 | |
| 731 | // 复制文件 |
| 732 | func (instance *SSHRemote) CopyFile(localFilePath string, remoteFilepath string) error { |
| 733 | var ( |
| 734 | err error |
| 735 | sftpClient *sftp.Client |
| 736 | ) |
| 737 | |
| 738 | // create sftp client |
| 739 | if sftpClient, err = sftp.NewClient(instance.Connection); err != nil { |
| 740 | return err |
| 741 | } |
| 742 | defer sftpClient.Close() |
| 743 | |
| 744 | //Local file path and folder on remote machine for testing |
| 745 | srcFile, err := os.Open(localFilePath) |
| 746 | if err != nil { |
| 747 | log.Fatal(err) |
| 748 | } |
| 749 | defer srcFile.Close() |
| 750 | |
| 751 | // 创建目录 |
| 752 | parent := filepath.Dir(remoteFilepath) |
| 753 | path := string(filepath.Separator) |
| 754 | dirs := strings.Split(parent, path) |
| 755 | for _, dir := range dirs { |
| 756 | path = filepath.Join(path, dir) |
| 757 | _ = sftpClient.Mkdir(path) |
| 758 | } |
| 759 | |
| 760 | // |
| 761 | dstFile, err := sftpClient.Create(remoteFilepath) |
| 762 | if err != nil { |
| 763 | return err |
| 764 | } |
| 765 | defer dstFile.Close() |
| 766 | |
| 767 | // |
| 768 | _, err = io.Copy(dstFile, srcFile) |
| 769 | if err != nil { |
| 770 | return err |
| 771 | } |
| 772 | |
| 773 | SmartIDELog.Debug(fmt.Sprintf("copy file (%v) to remote server (%v) finished!", localFilePath, remoteFilepath)) |
| 774 | return nil |
| 775 | } |
| 776 | |
| 777 | // 执行ssh command,在session模式下,standard output 只能在执行结束的时候获取到 |
| 778 | func (instance *SSHRemote) ExeSSHCommandConsole(sshCommand string) (outContent string, err error) { |
no test coverage detected