github.com/pkg/sftp@v1.13.6/server_standalone/main.go (about)

     1  package main
     2  
     3  // small wrapper around sftp server that allows it to be used as a separate process subsystem call by the ssh server.
     4  // in practice this will statically link; however this allows unit testing from the sftp client.
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"os"
    12  
    13  	"github.com/pkg/sftp"
    14  )
    15  
    16  func main() {
    17  	var (
    18  		readOnly    bool
    19  		debugStderr bool
    20  		debugLevel  string
    21  		options     []sftp.ServerOption
    22  	)
    23  
    24  	flag.BoolVar(&readOnly, "R", false, "read-only server")
    25  	flag.BoolVar(&debugStderr, "e", false, "debug to stderr")
    26  	flag.StringVar(&debugLevel, "l", "none", "debug level (ignored)")
    27  	flag.Parse()
    28  
    29  	debugStream := ioutil.Discard
    30  	if debugStderr {
    31  		debugStream = os.Stderr
    32  	}
    33  	options = append(options, sftp.WithDebug(debugStream))
    34  
    35  	if readOnly {
    36  		options = append(options, sftp.ReadOnly())
    37  	}
    38  
    39  	svr, _ := sftp.NewServer(
    40  		struct {
    41  			io.Reader
    42  			io.WriteCloser
    43  		}{os.Stdin,
    44  			os.Stdout,
    45  		},
    46  		options...,
    47  	)
    48  	if err := svr.Serve(); err != nil {
    49  		fmt.Fprintf(debugStream, "sftp server completed with error: %v", err)
    50  		os.Exit(1)
    51  	}
    52  }