sftp

package
v0.2.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// StoreType = "sftp" this is used to define the storage type to create
	// from cloudstorage.NewStore(config)
	StoreType = "sftp"

	AuthUserKey  cloudstorage.AuthMethod = "userkey"
	AuthUserPass cloudstorage.AuthMethod = "userpass"

	// ConfKeyUser config key name of the username
	ConfKeyUser = "user"
	// ConfKeyPassword config key name of the password
	ConfKeyPassword = "password"
	// ConfKeyPrivateKey config key name of the privatekey
	ConfKeyPrivateKey = "privatekey"
	// ConfKeyHost config key name of the server host
	ConfKeyHost = "host"
	// ConfKeyPort config key name of the sftp port
	ConfKeyPort = "port"
	// ConfKeyFolder config key name of the sftp folder
	ConfKeyFolder = "folder"
)

Variables

This section is empty.

Functions

func Concat

func Concat(strs ...string) string

Concat concats strings with "/" but ignores empty strings so an input of "portland", "", would yield "portland" instead of "portland/"

func ConcatSlash

func ConcatSlash(strs ...string) string

ConcatSlash concats strings and ensures ends with "/"

func ConfigUserKey

func ConfigUserKey(user, keyString string) (*ssh.ClientConfig, error)

ConfigUserKey creates ssh config with ssh/private rsa key

func ConfigUserPass

func ConfigUserPass(user, password string) *ssh.ClientConfig

ConfigUserPass creates ssh config with user/password HostKeyCallback was added here https://github.com/golang/crypto/commit/e4e2799dd7aab89f583e1d898300d96367750991 currently we don't check hostkey, but in the future (todo) we could store the hostkey and check on future logins if there is a match.

func NewStore

func NewStore(conf *cloudstorage.Config) (cloudstorage.Store, error)

Types

type ByModTime

type ByModTime []os.FileInfo

func (ByModTime) Len

func (a ByModTime) Len() int

func (ByModTime) Less

func (a ByModTime) Less(i, j int) bool

func (ByModTime) Swap

func (a ByModTime) Swap(i, j int)

type Client

type Client struct {
	ID string
	// contains filtered or unexported fields
}

Client is the sftp client

func NewClient

func NewClient(clientCtx context.Context, conf *cloudstorage.Config, host string, port int, folder string, config *ssh.ClientConfig) (*Client, error)

NewClient returns a new SFTP Client Make sure to close SFTP connection when done

func NewClientFromConfig

func NewClientFromConfig(clientCtx context.Context, conf *cloudstorage.Config) (*Client, error)

NewClientFromConfig validates configuration then creates new client from token

func (*Client) Client

func (m *Client) Client() interface{}

Client return underlying client

func (*Client) Close

func (m *Client) Close()

// MkDir creates new folder in base dir

func (m *Client) MkDir(dir string) error {
	dirs, err := m.listDirs("", "", false)
	if err != nil {
		gou.WarnCtx(m.clientCtx, "error listing dirs: %v", err)
		return err
	}
	for _, d := range dirs {
		if d == dir {
			return nil
		}
	}
	return m.client.Mkdir(Concat(m.Folder, dir))
}

// Cd changes the base dir

func (m *Client) Cd(dir string) {
	m.Folder = Concat(m.Folder, dir)
}
func (m *Client) FilesAfter(t time.Time) ([]os.FileInfo, error) {
	fi, err := m.fetchFiles("")
	if err != nil {
		return nil, err
	}
	sort.Sort(ByModTime(fi))

	var files []os.FileInfo
	for _, f := range fi {
		if f.IsDir() {
			continue
		}
		if strings.Index(f.Name(), ".") != 0 && f.ModTime().After(t) {
			files = append(files, f)
		}
	}

	return files, nil
}

Close closes underlying client connection

func (*Client) Delete

func (m *Client) Delete(ctx context.Context, filename string) error

Delete deletes a file

func (*Client) Exists

func (m *Client) Exists(filename string) bool

// Rename renames a file

func (m *Client) Rename(oldname, newname string) error {
	if !m.Exists(oldname) {
		return os.ErrNotExist
	}
	o := Concat(m.bucket, oldname)
	n := Concat(m.bucket, newname)

	gou.InfoCtx(m.clientCtx, "renaming file %q to %q", o, n)

	return m.client.Rename(o, n)
}

Exists checks to see if files exists

func (*Client) Folders

func (m *Client) Folders(ctx context.Context, q cloudstorage.Query) ([]string, error)

// ListFiles lists files in a directory

func (m *Client) ListFiles(folder string, hidden bool) ([]string, error) {
	return m.filterFileNames(folder, false, true, hidden)
}

Folders lists directories in a directory

func (*Client) Get

func (m *Client) Get(ctx context.Context, name string) (cloudstorage.Object, error)

Get opens a file for read or writing

func (*Client) List

List lists files in a directory

func (*Client) NewObject

func (m *Client) NewObject(objectname string) (cloudstorage.Object, error)

NewObject create a new object with given name. Will not write to remote sftp until Close is called.

func (*Client) NewReader

func (m *Client) NewReader(o string) (io.ReadCloser, error)

NewReader create file reader.

func (*Client) NewReaderWithContext

func (m *Client) NewReaderWithContext(ctx context.Context, name string) (io.ReadCloser, error)

NewReaderWithContext create new File reader with context.

func (*Client) NewWriter

func (m *Client) NewWriter(objectName string, metadata map[string]string) (io.WriteCloser, error)

NewWriter create Object Writer.

func (*Client) NewWriterWithContext

func (m *Client) NewWriterWithContext(ctx context.Context, name string, metadata map[string]string, opts ...cloudstorage.Opts) (io.WriteCloser, error)

NewWriterWithContext create writer with provided context and metadata.

func (*Client) Objects

// Open opens a file for read or writing

func (m *Client) Open(prefix, filename string) (io.ReadCloser, error) {
	fn := Concat(prefix, filename)
	if !m.Exists(fn) {
		return nil, os.ErrNotExist
	}
	get := Concat(m.bucket, fn)
	gou.InfoCtx(m.clientCtx, "getting file %q", get)

	return m.client.Open(get)
}

Objects returns an iterator over the objects in the google bucket that match the Query q. If q is nil, no filtering is done.

func (*Client) String

func (m *Client) String() string

func (*Client) Type

func (m *Client) Type() string

Type of store = "sftp"

type Uploader

type Uploader interface {
	Upload(io.Reader) (int64, error)
}
Store interface {
	Open(prefix, filename string) (io.ReadCloser, error)
	NewFile(filename string) (Uploader, error)
	Remove(filename string) error
	Rename(old, new string) error
	Exists(filename string) bool
	Files(folder string) ([]os.FileInfo, error)
	ListFiles(folder string, hidden bool) ([]string, error)
	ListDirs(folder string, hidden bool) ([]string, error)
	Cd(dir string)
	FilesAfter(t time.Time) ([]os.FileInfo, error)
	Close()
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL