Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / cli / TerminalOptionsInputValidator.java
blobcac2112493e3a2e071d4686767563d16abcd8b81
1 package ch.cyberduck.cli;
3 /*
4 * Copyright (c) 2002-2014 David Kocher. All rights reserved.
5 * http://cyberduck.io/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.io
21 import ch.cyberduck.core.Host;
22 import ch.cyberduck.core.HostParser;
23 import ch.cyberduck.core.Protocol;
24 import ch.cyberduck.core.ProtocolFactory;
25 import ch.cyberduck.core.transfer.Transfer;
26 import ch.cyberduck.core.transfer.TransferAction;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.lang3.StringUtils;
32 import java.util.Arrays;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
37 /**
38 * @version $Id$
40 public class TerminalOptionsInputValidator {
42 private Console console = new Console();
44 public boolean validate(final CommandLine input) {
45 for(Option o : input.getOptions()) {
46 if(Option.UNINITIALIZED == o.getArgs()) {
47 continue;
49 if(o.hasOptionalArg()) {
50 continue;
52 if(o.getArgs() != o.getValuesList().size()) {
53 console.printf("Missing argument for option %s%n", o.getLongOpt());
54 return false;
57 final TerminalAction action = TerminalActionFinder.get(input);
58 if(null == action) {
59 console.printf("%s%n", "Missing argument");
60 return false;
62 if(input.hasOption(TerminalOptionsBuilder.Params.existing.name())) {
63 final String arg = input.getOptionValue(TerminalOptionsBuilder.Params.existing.name());
64 if(null == TransferAction.forName(arg)) {
65 final Set<TransferAction> actions = new HashSet<TransferAction>(TransferAction.forTransfer(Transfer.Type.download));
66 actions.add(TransferAction.cancel);
67 console.printf("Invalid argument '%s' for option %s. Must be one of %s%n",
68 arg, TerminalOptionsBuilder.Params.existing.name(), Arrays.toString(actions.toArray()));
69 return false;
71 switch(action) {
72 case download:
73 if(!validate(arg, Transfer.Type.download)) {
74 return false;
76 break;
77 case upload:
78 if(!validate(arg, Transfer.Type.upload)) {
79 return false;
81 break;
82 case synchronize:
83 if(!validate(arg, Transfer.Type.sync)) {
84 return false;
86 break;
87 case copy:
88 if(!validate(arg, Transfer.Type.copy)) {
89 return false;
91 break;
94 // Validate arguments
95 switch(action) {
96 case list:
97 case download:
98 if(!validate(input.getOptionValue(action.name()))) {
99 return false;
101 break;
102 case upload:
103 case copy:
104 case synchronize:
105 if(!validate(input.getOptionValue(action.name()))) {
106 return false;
108 break;
110 return true;
113 private boolean validate(final String arg, final Transfer.Type type) {
114 final List<TransferAction> actions = TransferAction.forTransfer(type);
115 if(!actions.contains(TransferAction.forName(arg))) {
116 console.printf("Invalid argument '%s' for option %s. Must be one of %s%n",
117 arg, TerminalOptionsBuilder.Params.existing.name(), Arrays.toString(actions.toArray()));
118 return false;
120 return true;
124 * Validate URI
126 protected boolean validate(final String uri) {
127 if(uri.indexOf("://", 0) != -1) {
128 final Protocol protocol = ProtocolFactory.forName(uri.substring(0, uri.indexOf("://", 0)));
129 if(null == protocol) {
130 console.printf("Missing protocol in URI %s%n", uri);
131 return false;
134 final Host host = HostParser.parse(uri);
135 switch(host.getProtocol().getType()) {
136 case s3:
137 case googlestorage:
138 case swift:
139 break;
140 default:
141 if(StringUtils.isBlank(host.getHostname())) {
142 console.printf("Missing hostname in URI %s%n", uri);
143 return false;
146 if(StringUtils.isBlank(host.getDefaultPath())) {
147 console.printf("Missing path in URI %s%n", uri);
148 return false;
150 return true;