|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "gopkg.in/acd.v0/internal/constants" |
| 11 | + "gopkg.in/acd.v0/internal/log" |
| 12 | + |
| 13 | + "github.com/codegangsta/cli" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + cpCommand = cli.Command{ |
| 18 | + Name: "cp", |
| 19 | + Usage: "copy files", |
| 20 | + Description: "cp copy files, multiple files can be given. It follows the usage of cp whereas the last entry is the destination and has to be a folder if multiple files were given", |
| 21 | + Action: cpAction, |
| 22 | + BashComplete: cpBashComplete, |
| 23 | + Before: cpBefore, |
| 24 | + Flags: []cli.Flag{ |
| 25 | + cli.BoolFlag{ |
| 26 | + Name: "recursive, R", |
| 27 | + Usage: "cp recursively", |
| 28 | + }, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + action string |
| 33 | +) |
| 34 | + |
| 35 | +func init() { |
| 36 | + registerCommand(cpCommand) |
| 37 | +} |
| 38 | + |
| 39 | +func cpAction(c *cli.Context) { |
| 40 | + if strings.HasPrefix(c.Args()[len(c.Args())-1], "acd://") { |
| 41 | + cpUpload(c) |
| 42 | + } else { |
| 43 | + cpDownload(c) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func cpUpload(c *cli.Context) { |
| 48 | + // make sure the destination is a folder if it exists upstream and more than |
| 49 | + // one file is scheduled to be copied. |
| 50 | + dest := strings.TrimPrefix(c.Args()[len(c.Args())-1], "acd://") |
| 51 | + destNode, err := acdClient.NodeTree.FindNode(dest) |
| 52 | + if err == nil { |
| 53 | + // make sure if the remote node exists, it is a folder. |
| 54 | + if len(c.Args()) > 2 { |
| 55 | + if !destNode.IsDir() { |
| 56 | + log.Fatalf("cp: target %q is not a directory", dest) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + for _, src := range c.Args()[:len(c.Args())-1] { |
| 62 | + if strings.HasPrefix(src, "acd://") { |
| 63 | + fmt.Printf("cp: target %q is amazon, src cannot be amazon when destination is amazon. Skipping\n", src) |
| 64 | + continue |
| 65 | + } |
| 66 | + stat, err := os.Stat(src) |
| 67 | + if err != nil { |
| 68 | + if os.IsNotExist(err) { |
| 69 | + log.Fatalf("cp: %s: %s", constants.ErrFileNotFound, src) |
| 70 | + } |
| 71 | + |
| 72 | + log.Fatalf("cp: %s: %s", constants.ErrStatFile, src) |
| 73 | + } |
| 74 | + if stat.IsDir() { |
| 75 | + if !c.Bool("recursive") { |
| 76 | + fmt.Printf("cp: %q is a directory (not copied).", src) |
| 77 | + continue |
| 78 | + } |
| 79 | + destFile := dest |
| 80 | + if destNode != nil { |
| 81 | + if !destNode.IsDir() { |
| 82 | + log.Fatalf("cp: target %q is not a directory", dest) |
| 83 | + } |
| 84 | + destFile = fmt.Sprintf("%s/%s", dest, path.Base(src)) |
| 85 | + } |
| 86 | + acdClient.UploadFolder(src, destFile, true, true) |
| 87 | + continue |
| 88 | + } |
| 89 | + f, err := os.Open(src) |
| 90 | + if err != nil { |
| 91 | + log.Fatalf("%s: %s -- %s", constants.ErrOpenFile, err, src) |
| 92 | + } |
| 93 | + err = acdClient.Upload(dest, true, f) |
| 94 | + f.Close() |
| 95 | + if err != nil { |
| 96 | + log.Fatalf("%s: %s", err, dest) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func cpDownload(c *cli.Context) { |
| 102 | + dest := c.Args()[len(c.Args())-1] |
| 103 | + destDir := false |
| 104 | + destStat, err := os.Stat(dest) |
| 105 | + if err == nil && destStat.IsDir() { |
| 106 | + destDir = true |
| 107 | + } |
| 108 | + if len(c.Args()) > 2 { |
| 109 | + if err == nil && !destDir { |
| 110 | + log.Fatalf("cp: target %q is not a directory", dest) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + for _, src := range c.Args()[:len(c.Args())-1] { |
| 115 | + if !strings.HasPrefix(src, "acd://") { |
| 116 | + fmt.Printf("cp: source %q is local, src cannot be local when destination is local. Skipping\n", src) |
| 117 | + continue |
| 118 | + } |
| 119 | + srcPath := strings.TrimPrefix(src, "acd://") |
| 120 | + destPath := dest |
| 121 | + if destDir { |
| 122 | + destPath = path.Join(destPath, path.Base(srcPath)) |
| 123 | + } |
| 124 | + srcNode, err := acdClient.GetNodeTree().FindNode(srcPath) |
| 125 | + if err != nil { |
| 126 | + fmt.Printf("cp: source %q not found. Skipping", src) |
| 127 | + continue |
| 128 | + } |
| 129 | + if srcNode.IsDir() { |
| 130 | + acdClient.DownloadFolder(destPath, srcPath, c.Bool("recursive")) |
| 131 | + } else { |
| 132 | + content, err := acdClient.Download(srcPath) |
| 133 | + if err != nil { |
| 134 | + fmt.Printf("cp: error downloading source %q. Skipping", src) |
| 135 | + } |
| 136 | + // TODO: respect umask |
| 137 | + if err := os.MkdirAll(path.Dir(destPath), os.FileMode(0755)); err != nil { |
| 138 | + fmt.Printf("cp: error creating the parents folders of %q: %s. Skipping", destPath, err) |
| 139 | + continue |
| 140 | + } |
| 141 | + // TODO: respect umask |
| 142 | + f, err := os.Create(destPath) |
| 143 | + if err != nil { |
| 144 | + fmt.Printf("cp: error writing %q: %s. Skipping", destPath, err) |
| 145 | + continue |
| 146 | + } |
| 147 | + io.Copy(f, content) |
| 148 | + f.Close() |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func cpBashComplete(c *cli.Context) { |
| 154 | +} |
| 155 | + |
| 156 | +func cpBefore(c *cli.Context) error { |
| 157 | + foundRemote := false |
| 158 | + for _, arg := range c.Args() { |
| 159 | + if strings.HasPrefix(arg, "acd://") { |
| 160 | + foundRemote = true |
| 161 | + break |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if !foundRemote { |
| 166 | + return fmt.Errorf("cp: at least one path prefixed by acd:// is required. Given: %v", c.Args()) |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments