aboutsummaryrefslogtreecommitdiff
path: root/lib/ssh
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-09-24 15:25:09 +0700
committerShulhan <ms@kilabit.info>2023-09-24 15:25:09 +0700
commit858af0e5e4ad705f75819a171b7314b1aaa1dd6d (patch)
treede4e26b6eaf852344051ba862c925fc0b8388151 /lib/ssh
parent737ec1762f44322b0a45ee2ba9800b9ba8aad77b (diff)
downloadpakakeh.go-858af0e5e4ad705f75819a171b7314b1aaa1dd6d.tar.xz
ssh/sftp: add method to close client connection
The Close method close the client sftp session and release all its resources.
Diffstat (limited to 'lib/ssh')
-rw-r--r--lib/ssh/sftp/client.go12
-rw-r--r--lib/ssh/sftp/sftp_test.go9
2 files changed, 20 insertions, 1 deletions
diff --git a/lib/ssh/sftp/client.go b/lib/ssh/sftp/client.go
index e6462ce3..ea8c4ae8 100644
--- a/lib/ssh/sftp/client.go
+++ b/lib/ssh/sftp/client.go
@@ -79,6 +79,18 @@ func NewClient(sshc *ssh.Client) (cl *Client, err error) {
return cl, nil
}
+// Close the client sftp session and release all resources.
+func (cl *Client) Close() (err error) {
+ err = cl.sess.Close()
+
+ cl.requestId = 0
+ cl.pipeErr = nil
+ cl.pipeOut = nil
+ cl.pipeIn = nil
+
+ return fmt.Errorf(`Close: %w`, err)
+}
+
// CloseFile close the remote file handle.
func (cl *Client) CloseFile(fh *FileHandle) (err error) {
if fh == nil {
diff --git a/lib/ssh/sftp/sftp_test.go b/lib/ssh/sftp/sftp_test.go
index 38523b59..5aba892e 100644
--- a/lib/ssh/sftp/sftp_test.go
+++ b/lib/ssh/sftp/sftp_test.go
@@ -36,7 +36,7 @@ func TestMain(m *testing.M) {
cfg := &config.Section{
Field: map[string]string{
config.KeyUser: `ms`,
- config.KeyHostname: `127.0.0.1`,
+ config.KeyHostname: `localhost`,
config.KeyPort: `22`,
},
IdentityFile: []string{
@@ -57,6 +57,13 @@ func TestMain(m *testing.M) {
fmt.Printf("Server version: %d\n", testClient.version)
fmt.Printf("Server extensions: %v\n", testClient.exts)
+ defer func() {
+ var errClose = testClient.Close()
+ if errClose != nil {
+ log.Printf(`TestMain: %s`, errClose)
+ }
+ }()
+
os.Exit(m.Run())
}