sftp

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package sftp packaging some api of "github.com/pkg/sftp".

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCli

func NewCli(username, passwd, host string, port int) (*sftp.Client, error)

NewCli init sftp client.

func Upload

func Upload(fromPath string, toPath string, cli *sftp.Client) error

Upload uploads file from local path (@fromPath) to home dir ~/foo.zip (@toPath = foo.zip).

Types

type IPoint

type IPoint interface {
	Upload(fromPath, toPath string) error
}

type Point

type Point struct {
	// contains filtered or unexported fields
}

Point is one available sft client handler.

func NewPoint

func NewPoint(username, passwd, host string, port int) (*Point, error)

NewPoint inits one sftp client point.

func (*Point) Upload

func (p *Point) Upload(fromPath, toPath string) error

Upload is used for top level call direct for upload file.

Example
package main

import (
	"github.com/carlsiry/point/pkg/sftp"
	"net/http"
)

func main() {
	// 1. init sftp point
	point, err := sftp.NewPoint("root", "pwd123456", "192.168.11.1", 22)
	if err != nil {
		// handle err
	}

	// 2. inject inner layer
	uc := UseCaseLayer(point)

	// 3. serve controller
	Serve(Controller(uc))
}

type UseCase func(from string, to string) error

func UseCaseLayer(point sftp.IPoint) UseCase {
	return func(from, to string) error {
		// ... handle sth.
		return point.Upload(from, to)
	}
}

func Controller(useCase UseCase) http.HandlerFunc {
	return func(writer http.ResponseWriter, request *http.Request) {
		from := request.URL.Query().Get("fromPath")
		to := request.URL.Query().Get("toPath")
		if from == "" || to == "" {
			writer.WriteHeader(http.StatusBadRequest)
			return
		}

		if err := useCase(from, to); err != nil {
			writer.WriteHeader(http.StatusInternalServerError)
			return
		}
	}
}

func Serve(handler http.HandlerFunc) {
	if err := http.ListenAndServe(":8080", handler); err != nil {
		panic(err)
	}
}
Output:

Jump to

Keyboard shortcuts

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