This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
ZanataClient.java
224 lines (205 loc) · 6.88 KB
/
ZanataClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package org.zanata.client;
import java.io.PrintStream;
import java.lang.reflect.Field;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.SubCommand;
import org.kohsuke.args4j.spi.SubCommands;
import org.zanata.client.commands.AppAbortException;
import org.zanata.client.commands.AppAbortStrategy;
import org.zanata.client.commands.ArgsUtil;
import org.zanata.client.commands.BasicOptions;
import org.zanata.client.commands.BasicOptionsImpl;
import org.zanata.client.commands.ListRemoteOptionsImpl;
import org.zanata.client.commands.PutProjectOptionsImpl;
import org.zanata.client.commands.PutUserOptionsImpl;
import org.zanata.client.commands.PutVersionOptionsImpl;
import org.zanata.client.commands.SystemExitStrategy;
import org.zanata.client.commands.ZanataCommand;
import org.zanata.client.commands.pull.PullOptionsImpl;
import org.zanata.client.commands.push.PushOptionsImpl;
import org.zanata.client.commands.stats.GetStatisticsOptionsImpl;
import org.zanata.util.VersionUtility;
import com.google.common.collect.ImmutableMap;
/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public class ZanataClient extends BasicOptionsImpl
{
public static final String COMMAND_NAME = System.getProperty("app.name", "zanata-cli");
public static final String COMMAND_DESCRIPTION = "Zanata Java command-line client";
private boolean version;
private final CmdLineParser parser = new CmdLineParser(this);
private final AppAbortStrategy abortStrategy;
private final PrintStream out;
private final PrintStream err;
@Argument(handler=SubCommandHandler2.class, metaVar="<command>")
@SubCommands({
@SubCommand(name="help", impl=HelpOptions.class),
// @SubCommand(name="list-local", impl=ListLocalOptionsImpl.class),
@SubCommand(name="list-remote", impl=ListRemoteOptionsImpl.class),
@SubCommand(name="pull", impl=PullOptionsImpl.class),
@SubCommand(name="push", impl=PushOptionsImpl.class),
@SubCommand(name="put-project", impl=PutProjectOptionsImpl.class),
@SubCommand(name="put-user", impl=PutUserOptionsImpl.class),
@SubCommand(name="put-version", impl=PutVersionOptionsImpl.class),
@SubCommand(name="stats", impl=GetStatisticsOptionsImpl.class)
})
// if this field name changes, change COMMAND_FIELD too
private Object command;
private static final String COMMAND_FIELD = "command";
public static final ImmutableMap<String, Class<BasicOptions>> OPTIONS;
static
{
try
{
ImmutableMap.Builder<String, Class<BasicOptions>> m = ImmutableMap.builder();
Field cmdField = ZanataClient.class.getDeclaredField(COMMAND_FIELD);
SubCommands subCommands = cmdField.getAnnotation(SubCommands.class);
for (SubCommand sub : subCommands.value())
{
if (BasicOptions.class.isAssignableFrom(sub.impl()))
{
Class<BasicOptions> clazz = (Class<BasicOptions>) sub.impl();
m.put(sub.name(), clazz);
}
}
OPTIONS = m.build();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static void main(String[] args)
{
ZanataClient tool = new ZanataClient();
tool.processArgs(args);
}
@Override
public ZanataCommand initCommand()
{
return null;
}
ZanataClient()
{
this(new SystemExitStrategy(), System.out, System.err);
}
public ZanataClient(AppAbortStrategy strategy, PrintStream out, PrintStream err)
{
this.abortStrategy = strategy;
this.out = out;
this.err = err;
}
@Override
public String getCommandName()
{
return COMMAND_NAME;
}
@Override
public String getCommandDescription()
{
return COMMAND_DESCRIPTION;
}
protected void processArgs(String... args)
{
try
{
if (args.length == 2 && OPTIONS.containsKey(args[0]) && (args[1].equals("--help") || args[1].equals("-h")))
{
String cmdName = args[0];
BasicOptions opts = OPTIONS.get(cmdName).newInstance();
new ArgsUtil(abortStrategy, opts).printHelp(out, getCommandName());
return;
}
parser.parseArgument(args);
if (version)
{
out.println(getCommandName());
VersionUtility.printVersions(ZanataClient.class, out);
}
else if (getHelp() || command == null)
{
printHelp(out);
}
else if (command instanceof HelpOptions)
{
HelpOptions helpCmd = (HelpOptions) command;
if (helpCmd.getCommand() == null)
{
// generic help
printHelp(out);
}
else
{
// help for a sub-command
String cmdName = helpCmd.getCommand();
BasicOptions opts = OPTIONS.get(cmdName).newInstance();
new ArgsUtil(abortStrategy, opts).printHelp(out, getCommandName());
}
}
else if (command instanceof BasicOptions)
{
BasicOptions opts = (BasicOptions) command;
copyGlobalOptionsTo(opts);
new ArgsUtil(abortStrategy, opts).runCommand();
}
else
{
throw new RuntimeException("unexpected command type");
}
}
catch (CmdLineException e)
{
String msg = e.getMessage();
err.println(msg);
printHelp(err);
abortStrategy.abort(msg);
}
catch (AppAbortException e)
{
throw e;
}
catch (Exception e)
{
ArgsUtil.handleException(e, getErrors(), abortStrategy);
}
}
/**
* @param options
*/
private void copyGlobalOptionsTo(BasicOptions options)
{
options.setDebug(getDebug());
options.setErrors(getErrors());
options.setHelp(getHelp());
options.setInteractiveMode(isInteractiveMode());
options.setQuiet(getQuiet());
}
private void printHelp(PrintStream out)
{
out.print("Usage: " + getCommandName());
parser.printSingleLineUsage(out);
out.println();
out.println();
out.println(getCommandDescription());
out.println();
parser.printUsage(out);
out.println();
out.println("Type '" + getCommandName() + " help <command>' for help on a specific command.");
out.println();
out.println("Available commands:");
for (String cmd : OPTIONS.keySet())
{
out.println(" " + cmd /*+ ": " + OPTIONS.get(cmd).newInstance().getCommandDescription()*/);
}
}
@Option(name = "--version", aliases = { "-v" }, usage = "Output version information and exit")
public void setVersion(boolean version)
{
this.version = version;
}
}