Skip to content

Commit

Permalink
package sftpfs
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Aug 28, 2023
1 parent e80d166 commit 6ca7989
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
7 changes: 3 additions & 4 deletions sftpfs/sftpfs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package ftpfs implements a (S)FTP client file system.
package ftpfs
// Package sftpfs implements a SFTP client file system.
package sftpfs

import (
"context"
Expand Down Expand Up @@ -32,13 +32,12 @@ type SFTPFileSystem struct {
func Dial(addr, user, password string) (*SFTPFileSystem, error) {
addr = strings.TrimSuffix(strings.TrimPrefix(addr, "sftp://"), "/")

var hostKey ssh.PublicKey
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.FixedHostKey(hostKey),
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
conn, err := ssh.Dial("tcp", addr, config)
if err != nil {
Expand Down
31 changes: 30 additions & 1 deletion sftpfs/sftpfs_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
package ftpfs
package sftpfs

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ungerik/go-fs"
)

func TestDial(t *testing.T) {
// https://www.sftp.net/public-online-sftp-servers
ftpFS, err := Dial("test.rebex.net:22", "demo", "password")
require.NoError(t, err, "Dial")

f := fs.File("sftp://test.rebex.net:22/readme.txt")
assert.True(t, f.Exists(), "Exists")
assert.False(t, f.IsDir(), "not IsDir")
data, err := f.ReadAll()
assert.NoError(t, err)
assert.True(t, len(data) > 0)

// files, err := fs.File("sftp://test.rebex.net:22/").ListDirMax(-1)
// fmt.Println(files)
// t.Fatal("todo")

err = ftpFS.Close()
require.NoError(t, err, "Close")
}

0 comments on commit 6ca7989

Please sign in to comment.