scp

package
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2019 License: Apache-2.0 Imports: 21 Imported by: 0

README

SCP - scp base storage

Usage
func main() {
	auth, err := scp.LocalhostKeyAuth("")
	if err != nil {
		log.Fatal(err)
	}
	service := afs.New()
	ctx := context.Background()
	reader, err := service.DownloadWithURL(ctx, "scp://127.0.0.1:22/etc/hosts", auth, option.NewTimeout(2000))
	if err != nil {
		log.Fatal(err)
	}
	defer reader.Close()
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("data: %s\n", data)

    //next call with the same base URL reuses auth unless Close is called
    reader, err := service.DownloadWithURL(ctx, "scp://127.0.0.1:22/etc/hosts")

}
func main() {

	var config *ssh.ClientConfig

	//load config ...
	
	
	timeoutMS := 15000
	service, err := scp.NewStorager("127.0.0.1:22", timeoutMS, config)
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	location := "/tmp/myfile"
	err = service.Upload(ctx,  location, 0644, []byte("somedata"))
	if err != nil {
		log.Fatal(err)
	}
	reader, err := service.Download(ctx,  location)
	if err != nil {
		log.Fatal(err)
	}
	data, err := ioutil.ReadAll(reader)
	fmt.Printf("data: %s\n", data)

	has, _ := service.Exists(ctx, location)
	fmt.Printf("%v %v", location, has)

	files, err  := service.List(ctx, location, 3)
	if err != nil {
		log.Fatal(err)
	}
	for _, file := range files {
		fmt.Printf("file: %v\n", file.Name())
	}
	err = service.Delete(ctx, location)
	if err != nil {
		log.Fatal(err)
	}

}	
Options
    var authProvider AuthProvider
    //... initialise AuthProvider

    service := afs.New()
    ctx := context.Background()
    reader, err := service.DownloadWithURL(ctx, "scp://127.0.0.1:22/etc/hosts", authProvider, option.NewTimeout(2000))
    if err != nil {
        log.Fatal(err)
    }
    
    auth := option.NewBasicAuth("myuser", "nypass")
	service := afs.New()
	ctx := context.Background()
	reader, err := service.DownloadWithURL(ctx, "scp://127.0.0.1:22/etc/hosts", auth, option.NewTimeout(2000))
	if err != nil {
		log.Fatal(err)
	}
    auth, err := scp.LocalhostKeyAuth("pathToMyAuthorizedKeyContainer_id_rsa")
	if err != nil {
		log.Fatal(err)
	}
	service := afs.New()
	ctx := context.Background()
	reader, err := service.DownloadWithURL(ctx, "scp://127.0.0.1:22/etc/hosts", auth)
	if err != nil {
		log.Fatal(err)
	}

Documentation

Overview

Package scp implements SSH scp storager and abstract file manager

Index

Constants

View Source
const (
	//FileToken crate file token
	FileToken = 'C'
	//DirToken create directory token
	DirToken = 'D'
	//TimestampToken timestamp token
	TimestampToken = 'T'
	//EndDirToken end of dir token
	EndDirToken = 'E'
	//WarningToken warning token
	WarningToken = 0x1
	//ErrorToken error token
	ErrorToken = 0x2
	//DefaultPort default SSH port
	DefaultPort = 22
)
View Source
const Scheme = "scp"

Scheme defines scp URL scheme

Variables

This section is empty.

Functions

func InfoToCreateCmd

func InfoToCreateCmd(info os.FileInfo) string

InfoToCreateCmd returns scp create command for supplied info

func InfoToTimestampCmd

func InfoToTimestampCmd(info os.FileInfo) string

InfoToTimestampCmd returns scp timestamp command for supplied info

func New

func New(options ...storage.Option) storage.Manager

New creates scp manager

func NewInfo

func NewInfo(createResponse string, modified *time.Time) (os.FileInfo, error)

NewInfo returns new info from SCP response

func NewStorager

func NewStorager(address string, timeout time.Duration, config *ssh.ClientConfig) (storage.Storager, error)

NewStorager returns a new storager

func ParseTimeResponse

func ParseTimeResponse(response string) (*time.Time, error)

ParseTimeResponse parases respons time

func Provider

func Provider(options ...storage.Option) (storage.Manager, error)

Provider returns a http manager

Types

type AuthProvider

type AuthProvider interface {
	ClientConfig() (*ssh.ClientConfig, error)
}

AuthProvider represents ssh client config authProvider

func NewAuthProvider

func NewAuthProvider(pemAuth KeyAuth, credAuth option.BasicAuth) AuthProvider

NewAuthProvider returns new auth provider

type KeyAuth

type KeyAuth interface {
	//Singer returns signer key
	Singer() (ssh.Signer, error)

	Username() string
}

KeyAuth represents a key based auth

func LocalhostKeyAuth

func LocalhostKeyAuth(keyPassword string, locations ...string) (KeyAuth, error)

LocalhostKeyAuth returns a localhost key authenticator with ~/.ssh/authorized_keys

func NewKeyAuth

func NewKeyAuth(keyLocation, username, keyPassword string) KeyAuth

NewKeyAuth returns a new private key authenticator

Jump to

Keyboard shortcuts

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