|
| 1 | +package nfs |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + |
| 6 | + log "github.com/sirupsen/logrus" |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +func init() { |
| 11 | + NFSCommand.AddCommand(UploadCommand) |
| 12 | + NFSCommand.AddCommand(DownloadCommand) |
| 13 | +} |
| 14 | + |
| 15 | +var UploadCommand = &cobra.Command{ |
| 16 | + Use: "upload localfile remotepath", |
| 17 | + Aliases: []string{"put"}, |
| 18 | + Short: "upload file to nfs", |
| 19 | + Long: "upload file to nfs", |
| 20 | + PreRunE: NotInit, |
| 21 | + Run: func(cmd *cobra.Command, args []string) { |
| 22 | + var localfile, remotepath string |
| 23 | + switch len(args) { |
| 24 | + case 2: |
| 25 | + remotepath = args[1] |
| 26 | + fallthrough |
| 27 | + case 1: |
| 28 | + localfile = filepath.Clean(args[0]) |
| 29 | + if remotepath == "" { |
| 30 | + remotepath = localfile |
| 31 | + } |
| 32 | + default: |
| 33 | + log.Errorf("usage: nfs upload localfile [remotepath]") |
| 34 | + } |
| 35 | + if err := Client.Upload(localfile, remotepath); err != nil { |
| 36 | + log.Errorf("upload %s to %s failed: %v", localfile, remotepath, err) |
| 37 | + return |
| 38 | + } |
| 39 | + log.Infof("upload %s to %s success", localfile, remotepath) |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +var DownloadCommand = &cobra.Command{ |
| 44 | + Use: "download remotefile localpath", |
| 45 | + Aliases: []string{"get"}, |
| 46 | + Short: "download file from nfs", |
| 47 | + Long: "download file from nfs", |
| 48 | + PreRunE: NotInit, |
| 49 | + Run: func(cmd *cobra.Command, args []string) { |
| 50 | + var remotefile, localpath string |
| 51 | + switch len(args) { |
| 52 | + case 2: |
| 53 | + localpath = args[1] |
| 54 | + fallthrough |
| 55 | + case 1: |
| 56 | + remotefile = filepath.Clean(args[0]) |
| 57 | + if localpath == "" { |
| 58 | + localpath = filepath.Base(remotefile) |
| 59 | + } |
| 60 | + default: |
| 61 | + log.Errorf("usage: nfs download remotefile [localpath]") |
| 62 | + } |
| 63 | + if err := Client.Download(remotefile, localpath); err != nil { |
| 64 | + log.Errorf("download %s to %s failed: %v", remotefile, localpath, err) |
| 65 | + return |
| 66 | + } |
| 67 | + log.Infof("download %s to %s success", remotefile, localpath) |
| 68 | + }, |
| 69 | +} |
0 commit comments