1 package org.argeo.cli.fs;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.List;
8
9 import org.apache.commons.cli.CommandLine;
10 import org.apache.commons.cli.Option;
11 import org.apache.commons.cli.Options;
12 import org.argeo.cli.CommandArgsException;
13 import org.argeo.cli.DescribedCommand;
14 import org.argeo.sync.SyncResult;
15
16 public class FileSync implements DescribedCommand<SyncResult<Path>> {
17 final static Option deleteOption = Option.builder().longOpt("delete").desc("delete from target").build();
18 final static Option recursiveOption = Option.builder("r").longOpt("recursive").desc("recurse into directories")
19 .build();
20 final static Option progressOption = Option.builder().longOpt("progress").hasArg(false).desc("show progress")
21 .build();
22
23 @Override
24 public SyncResult<Path> apply(List<String> t) {
25 try {
26 CommandLine line = toCommandLine(t);
27 List<String> remaining = line.getArgList();
28 if (remaining.size() == 0) {
29 throw new CommandArgsException("There must be at least one argument");
30 }
31 URI sourceUri = new URI(remaining.get(0));
32 URI targetUri;
33 if (remaining.size() == 1) {
34 targetUri = Paths.get(System.getProperty("user.dir")).toUri();
35 } else {
36 targetUri = new URI(remaining.get(1));
37 }
38 boolean delete = line.hasOption(deleteOption.getLongOpt());
39 boolean recursive = line.hasOption(recursiveOption.getLongOpt());
40 PathSync pathSync = new PathSync(sourceUri, targetUri, delete, recursive);
41 return pathSync.call();
42 } catch (URISyntaxException e) {
43 throw new CommandArgsException(e);
44 }
45 }
46
47 @Override
48 public Options getOptions() {
49 Options options = new Options();
50 options.addOption(recursiveOption);
51 options.addOption(deleteOption);
52 options.addOption(progressOption);
53 return options;
54 }
55
56 @Override
57 public String getUsage() {
58 return "[source URI] [target URI]";
59 }
60
61 public static void main(String[] args) {
62 DescribedCommand.mainImpl(new FileSync(), args);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 }
91
92
93
94
95
96
97 @Override
98 public String getDescription() {
99 return "Synchronises files";
100 }
101
102 }