]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - cli/fs/FileSync.java
1 package org
.argeo
.cli
.fs
;
4 import java
.net
.URISyntaxException
;
5 import java
.nio
.file
.Path
;
6 import java
.nio
.file
.Paths
;
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
;
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")
20 final static Option progressOption
= Option
.builder().longOpt("progress").hasArg(false).desc("show progress")
24 public SyncResult
<Path
> apply(List
<String
> t
) {
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");
31 URI sourceUri
= new URI(remaining
.get(0));
33 if (remaining
.size() == 1) {
34 targetUri
= Paths
.get(System
.getProperty("user.dir")).toUri();
36 targetUri
= new URI(remaining
.get(1));
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
);
48 public Options
getOptions() {
49 Options options
= new Options();
50 options
.addOption(recursiveOption
);
51 options
.addOption(deleteOption
);
52 options
.addOption(progressOption
);
57 public String
getUsage() {
58 return "[source URI] [target URI]";
61 public static void main(String
[] args
) {
62 DescribedCommand
.mainImpl(new FileSync(), args
);
63 // Options options = new Options();
64 // options.addOption("r", "recursive", false, "recurse into directories");
65 // options.addOption(Option.builder().longOpt("progress").hasArg(false).desc("show progress").build());
67 // CommandLineParser parser = new DefaultParser();
69 // CommandLine line = parser.parse(options, args);
70 // List<String> remaining = line.getArgList();
71 // if (remaining.size() == 0) {
72 // System.err.println("There must be at least one argument");
73 // printHelp(options);
76 // URI sourceUri = new URI(remaining.get(0));
78 // if (remaining.size() == 1) {
79 // targetUri = Paths.get(System.getProperty("user.dir")).toUri();
81 // targetUri = new URI(remaining.get(1));
83 // PathSync pathSync = new PathSync(sourceUri, targetUri);
85 // } catch (Exception exp) {
86 // exp.printStackTrace();
87 // printHelp(options);
92 // public static void printHelp(Options options) {
93 // HelpFormatter formatter = new HelpFormatter();
94 // formatter.printHelp("sync SRC [DEST]", options, true);
98 public String
getDescription() {
99 return "Synchronises files";