-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_serve_ftp.go
58 lines (51 loc) · 1.27 KB
/
cmd_serve_ftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"github.com/spf13/cobra"
"goftp.io/server/v2"
"goftp.io/server/v2/driver/file"
)
type serveFTPOptions struct {
root string
port uint16
user string
passwd string
}
var (
serveFTPOpts serveFTPOptions
serveFTPCmd = &cobra.Command{
Use: "ftp",
Short: "spawn a FTP server",
RunE: func(cmd *cobra.Command, args []string) error {
driver, err := file.NewDriver(serveFTPOpts.root)
if err != nil {
return err
}
serverOpts := &server.Options{
Name: "gcat ftp server",
Driver: driver,
Port: int(serveFTPOpts.port),
Auth: &server.SimpleAuth{
Name: serveFTPOpts.user,
Password: serveFTPOpts.passwd,
},
Perm: server.NewSimplePerm("gcat", "gcat"),
}
ftpServer, err := server.NewServer(serverOpts)
if err != nil {
return err
}
if err := ftpServer.ListenAndServe(); err != nil {
return err
}
return nil
},
}
)
func init() {
serveCmd.AddCommand(serveFTPCmd)
f := serveFTPCmd.Flags()
f.StringVarP(&serveFTPOpts.root, "root", "r", ".", "FTP root directory")
f.StringVarP(&serveFTPOpts.user, "user", "u", "ftp", "FTP user")
f.StringVarP(&serveFTPOpts.passwd, "passwd", "P", "ftp", "FTP password")
f.Uint16VarP(&serveFTPOpts.port, "port", "p", 2121, "Listen TCP port")
}