package ftp import ( "log" "path/filepath" filedriver "github.com/goftp/file-driver" "github.com/goftp/server" "github.com/spf13/cobra" ) var ports int var username, pwd string var path, ip string // ftpCmd represents the ftp command var Cmd = &cobra.Command{ Use: "ftp", Short: `FTP文件服务器`, Long: `FTP文件服务器`, Run: func(cmd *cobra.Command, args []string) { path, _ = filepath.Abs(path) factory := &filedriver.FileDriverFactory{ RootPath: path, Perm: server.NewSimplePerm("user", "group"), } opts := &server.ServerOpts{ Factory: factory, Port: ports, Hostname: ip, Auth: &server.SimpleAuth{Name: username, Password: pwd}, } log.Printf("Starting ftp server on %v:%v", opts.Hostname, opts.Port) log.Printf("Username %v, Password %v", username, pwd) server := server.NewServer(opts) err := server.ListenAndServe() if err != nil { log.Fatal("Error starting server:", err) } }, } func init() { Cmd.Flags().IntVarP(&ports, "port", "p", 21, "监听端口") Cmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "监听地址") Cmd.Flags().StringVarP(&username, "user", "u", "1", "用户名,默认为1") Cmd.Flags().StringVarP(&pwd, "pwd", "k", "1", "密码,默认为1") Cmd.Flags().StringVarP(&path, "folder", "f", "./", "本地文件地址") }