View Javadoc
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  //		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());
66  //
67  //		CommandLineParser parser = new DefaultParser();
68  //		try {
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);
74  //				System.exit(1);
75  //			}
76  //			URI sourceUri = new URI(remaining.get(0));
77  //			URI targetUri;
78  //			if (remaining.size() == 1) {
79  //				targetUri = Paths.get(System.getProperty("user.dir")).toUri();
80  //			} else {
81  //				targetUri = new URI(remaining.get(1));
82  //			}
83  //			PathSync pathSync = new PathSync(sourceUri, targetUri);
84  //			pathSync.run();
85  //		} catch (Exception exp) {
86  //			exp.printStackTrace();
87  //			printHelp(options);
88  //			System.exit(1);
89  //		}
90  	}
91  
92  //	public static void printHelp(Options options) {
93  //		HelpFormatter formatter = new HelpFormatter();
94  //		formatter.printHelp("sync SRC [DEST]", options, true);
95  //	}
96  
97  	@Override
98  	public String getDescription() {
99  		return "Synchronises files";
100 	}
101 
102 }