forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliContext.java
More file actions
621 lines (518 loc) · 17.3 KB
/
CliContext.java
File metadata and controls
621 lines (518 loc) · 17.3 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package act.cli;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.app.ActionContext;
import act.app.App;
import act.cli.ascii_table.ASCIITableHeader;
import act.cli.ascii_table.impl.SimpleASCIITableImpl;
import act.cli.ascii_table.spec.IASCIITable;
import act.cli.ascii_table.spec.IASCIITableAware;
import act.cli.builtin.Exit;
import act.cli.builtin.Help;
import act.cli.util.CommandLineParser;
import act.handler.CliHandler;
import act.util.ActContext;
import act.util.ProgressGauge;
import act.util.PropertySpec;
import act.util.SimpleProgressGauge;
import jline.console.ConsoleReader;
import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarStyle;
import org.osgl.$;
import org.osgl.cache.CacheService;
import org.osgl.concurrent.ContextLocal;
import org.osgl.http.H;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import org.xnio.streams.WriterOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import static act.cli.ReportProgress.Type.BAR;
public class CliContext extends ActContext.Base<CliContext> implements IASCIITable {
public static class ParsingContext {
// The number of options plus arguments in
// the command executor method params.
// This does not include the provided params (e.g. those
// params that should be injected by framework, like. App etc)
private int optionArgumentsCnt;
// mark the current loading argument
private AtomicInteger curArgId;
// Keep track the number of options provided
// for a specific required group
Map<String, AtomicInteger> required;
private ParsingContext() {}
public AtomicInteger curArgId() {
return curArgId;
}
public boolean hasArguments(CommandLineParser command) {
return curArgId.get() < command.argumentCount();
}
public void foundRequired(String group) {
required.get(group).incrementAndGet();
}
public boolean hasMultipleOptionArguments() {
return optionArgumentsCnt > 1;
}
public Set<String> missingOptions() {
Set<String> set = new HashSet<String>();
for (Map.Entry<String, AtomicInteger> entry : required.entrySet()) {
if (entry.getValue().get() < 1) {
set.add(entry.getKey());
}
}
return set;
}
@SuppressWarnings("FallThrough")
public void raiseExceptionIfThereAreMissingOptions(CliContext context) {
Set<String> missings = missingOptions();
int missing = missings.size();
switch (missing) {
case 0:
return;
case 1:
// check if there are command argument
if (!context.arguments().isEmpty()) {
return;
}
default:
throw new CliException("Missing required options: %s", missings);
}
}
public ParsingContext copy() {
ParsingContext ctx = new ParsingContext();
ctx.optionArgumentsCnt = optionArgumentsCnt;
ctx.required = new HashMap<String, AtomicInteger>(required);
for (Map.Entry<String, AtomicInteger> entry : ctx.required.entrySet()) {
entry.setValue(new AtomicInteger(0));
}
ctx.curArgId = new AtomicInteger(0);
return ctx;
}
}
public static class ParsingContextBuilder {
private static final ThreadLocal<ParsingContext> ctx = new ThreadLocal<ParsingContext>();
public static void start() {
ParsingContext ctx0 = new ParsingContext();
ctx0.required = new HashMap<String, AtomicInteger>();
ctx.set(ctx0);
}
public static void foundOptional() {
ctx.get().optionArgumentsCnt++;
}
public static void foundArgument() {
ctx.get().optionArgumentsCnt++;
}
public static void foundRequired(String group) {
ParsingContext ctx0 = ctx.get();
ctx0.optionArgumentsCnt++;
ctx0.required.put(group, new AtomicInteger(0));
}
public static ParsingContext finish() {
ParsingContext ctx0 = ctx.get();
ctx.remove();
return ctx0;
}
}
public static final String ATTR_PWD = "__act_pwd__";
private static final ContextLocal<CliContext> _local = $.contextLocal();
private CliSession session;
private String commandPath; // e.g. myapp.cli.ListUser
private Map<String, Object> commanderInstances = C.newMap();
private ConsoleReader console;
// workaround of issue http://stackoverflow.com/questions/34467383/jline2-print-j-when-it-should-print-n-on-a-telnet-console
private PrintWriter pw;
private CommandLineParser parser;
private IASCIITable asciiTable;
private CacheService evaluatorCache;
private ParsingContext parsingContext;
private CliHandler handler;
private boolean rawPrint;
private Map<String, String> preparsedOptionValues;
public CliContext(String line, App app, ConsoleReader console, CliSession session) {
this(line, app, console, session, null == System.getenv("cli-no-raw-print"));
}
protected CliContext(String line, App app, ConsoleReader console, CliSession session, boolean rawPrint) {
super(app);
this.session = session;
this.parser = new CommandLineParser(line);
this.evaluatorCache = app.cache();
this.console = $.NPE(console);
this.pw = new PrintWriter(console.getOutput());
this.rawPrint = rawPrint;
this.handler = app.cliDispatcher().handler(command());
this.preparsedOptionValues = new HashMap<String, String>();
this.saveLocal();
}
/**
* Set the console prompt
* @param prompt the prompt
*/
public void prompt(String prompt) {
console.setPrompt(prompt);
}
public void prepare(ParsingContext ctx) {
this.parsingContext = ctx.copy();
}
public ParsingContext parsingContext() {
return this.parsingContext;
}
/**
* Reset the console prompt to "{@code act[<session-id>]>}"
*/
public void resetPrompt() {
prompt("act[" + session.id() + "]>");
}
public CacheService evaluatorCache() {
return evaluatorCache;
}
public CommandLineParser commandLine() {
return parser;
}
public String command() {
return parser.command();
}
public List<String> arguments() {
return parser.arguments();
}
public CliSession session() {
return session;
}
public CliContext session(String key, Object val) {
session().attribute(key, val);
return this;
}
public <T> T session(String key) {
return session().attribute(key);
}
/**
* Returns CLI session id
* @return CLI session id
*/
@Override
public String sessionId() {
return session().id();
}
@Override
public Set<String> paramKeys() {
return preparsedOptionValues.keySet();
}
public void param(String key, String val) {
this.preparsedOptionValues.put(key, val);
}
@Override
public String paramVal(String key) {
String s = this.preparsedOptionValues.get(key);
if (null == s) {
// try free options
s = parser.getOptions().get(S.concat("--", key));
}
return s;
}
@Override
public String[] paramVals(String key) {
return new String[]{paramVal(key)};
}
/**
* Return the current working directory
* @return the current working directory
*/
public File curDir() {
File file = session().attribute(ATTR_PWD);
if (null == file) {
file = new File(System.getProperty("user.dir"));
session().attribute(ATTR_PWD, file);
}
return file;
}
public CliContext chDir(File dir) {
E.illegalArgumentIf(!dir.isDirectory());
session().attribute(ATTR_PWD, dir);
return this;
}
@Override
public CliContext accept(H.Format fmt) {
throw E.unsupport();
}
@Override
public H.Format accept() {
throw E.unsupport();
}
public void flush() {
pw.flush();
}
public boolean disconnected() {
return pw.checkError();
}
public void print(ProgressGauge progressGauge) {
ReportProgress reportProgress = attribute(ReportProgress.CTX_ATTR_KEY);
if (null == reportProgress) {
reportProgress = org.osgl.inject.util.AnnotationUtil.createAnnotation(ReportProgress.class);
}
ReportProgress.Type type = reportProgress.type();
if (BAR == type) {
printBar(progressGauge);
} else {
printText(progressGauge);
}
}
public void printBar(ProgressGauge progressGauge) {
PrintStream os = new PrintStream(new WriterOutputStream(rawPrint ? pw : console.getOutput()));
String label = app().config().i18nEnabled() ? i18n("act.progress.capFirst") : "Progress";
ProgressBar pb = new ProgressBar(label, progressGauge.maxHint(), 200, os, ProgressBarStyle.UNICODE_BLOCK);
pb.start();
while (!progressGauge.done()) {
pb.maxHint(progressGauge.maxHint());
pb.stepTo(progressGauge.currentSteps());
flush();
}
pb.stepTo(pb.getMax());
pb.stop();
}
public void printText(ProgressGauge progressGauge) {
SimpleProgressGauge simpleProgressGauge = SimpleProgressGauge.wrap(progressGauge);
boolean i18n = app().config().i18nEnabled();
while (!progressGauge.done()) {
if (i18n) {
print("\r" + i18n("act.progress.report", simpleProgressGauge.currentProgressPercent()));
} else {
print("\rCurrent progress: " + simpleProgressGauge.currentProgressPercent() + "%");
}
flush();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
print("Interrupted");
break;
}
}
println();
}
public void print(String template, Object ... args) {
if (rawPrint) {
print1(template, args);
} else {
print0(template, args);
}
}
/**
* Run handler and return `false` if it needs to exit the CLI or `true` otherwise
*/
void handle() throws IOException {
if (null == handler) {
println("Command not recognized: %s", command());
return;
}
if (handler == Exit.INSTANCE) {
handler.handle(this);
}
CommandLineParser parser = commandLine();
boolean help = parser.getBoolean("-h", "--help");
if (help) {
Help.INSTANCE.showHelp(parser.command(), this);
} else {
try {
session.handler(handler);
handler.handle(this);
} catch ($.Break b) {
throw b;
} catch (Exception e) {
console.println("Error: " + e.getMessage());
}
}
}
private void print0(String template, Object... args) {
try {
console.print(S.fmt(template, args));
} catch (IOException e) {
throw E.ioException(e);
}
}
private void print1(String template, Object ... args) {
if (args.length == 0) {
pw.print(template);
} else {
pw.printf(osNative(template), args);
}
}
private void println0(String template, Object... args) {
try {
if (args.length > 0) {
template = S.fmt(template);
}
console.println(template);
} catch (IOException e) {
throw E.ioException(e);
}
}
public void println() {
if (rawPrint) {
println1("");
} else {
println0("");
}
}
public void println(String template, Object... args) {
if (rawPrint) {
println1(template, args);
} else {
println0(template, args);
}
}
private void println1(String template, Object... args) {
if (args.length == 0) {
pw.print(template);
} else {
pw.printf(osNative(template), args);
}
pw.println();
}
@Override
protected void releaseResources() {
super.releaseResources();
_local.remove();
PropertySpec.current.remove();
}
public String commandPath() {
return commandPath;
}
public CliContext commandPath(String path) {
commandPath = path;
return this;
}
@Override
public String methodPath() {
return commandPath;
}
public CliContext __commanderInstance(String className, Object instance) {
if (null == commanderInstances) {
commanderInstances = C.newMap();
}
commanderInstances.put(className, instance);
return this;
}
public Object __commanderInstance(String className) {
return null == commanderInstances ? null : commanderInstances.get(className);
}
@Override
public <T> T renderArg(String name) {
return super.renderArg(name);
}
@Override
public CliContext renderArg(String name, Object val) {
return super.renderArg(name, val);
}
@Override
public Map<String, Object> renderArgs() {
return super.renderArgs();
}
/**
* Called by bytecode enhancer to set the name list of the render arguments that is update
* by the enhancer
* @param names the render argument names separated by ","
* @return this AppContext
*/
@SuppressWarnings("unused")
public CliContext __appRenderArgNames(String names) {
return renderArg("__arg_names__", C.listOf(names.split(",")));
}
@SuppressWarnings("unused")
public List<String> __appRenderArgNames() {
return renderArg("__arg_names__");
}
private synchronized IASCIITable tbl() {
if (asciiTable == null) {
asciiTable = new SimpleASCIITableImpl(new PrintWriter(console.getOutput()));
}
return asciiTable;
}
@Override
public void printTable(String[] header, String[][] data) {
tbl().printTable(header, data);
}
@Override
public void printTable(String[] header, String[][] data, int dataAlign) {
tbl().printTable(header, data, dataAlign);
}
@Override
public void printTable(String[] header, int headerAlign, String[][] data, int dataAlign) {
tbl().printTable(header, headerAlign, data, dataAlign);
}
@Override
public void printTable(ASCIITableHeader[] headerObjs, String[][] data) {
tbl().printTable(headerObjs, data);
}
@Override
public void printTable(IASCIITableAware asciiTableAware) {
tbl().printTable(asciiTableAware);
}
@Override
public String getTable(String[] header, String[][] data) {
return tbl().getTable(header, data);
}
@Override
public String getTable(String[] header, String[][] data, int dataAlign) {
return tbl().getTable(header, data, dataAlign);
}
@Override
public String getTable(String[] header, int headerAlign, String[][] data, int dataAlign) {
return tbl().getTable(header, headerAlign, data, dataAlign);
}
@Override
public String getTable(ASCIITableHeader[] headerObjs, String[][] data) {
return tbl().getTable(headerObjs, data);
}
@Override
public String getTable(IASCIITableAware asciiTableAware) {
return tbl().getTable(asciiTableAware);
}
public File getFile(String path) {
if (path.startsWith("~/")) {
path = System.getProperty("user.home") + path.substring(1);
}
File file = new File(path);
if (file.isAbsolute()) {
return file;
}
file = new File(curDir(), path);
return new File(file.getAbsolutePath());
}
private void saveLocal() {
_local.set(this);
}
private void initOverHttp(ActionContext actionContext) {
}
public static CliContext current() {
return _local.get();
}
private static String osNative(String s) {
s = s.replace("\n\r", "\n");
s = s.replace("\r", "\n");
if ("\n".equals($.OS.lineSeparator())) {
return s;
}
s = s.replace("\n", $.OS.lineSeparator());
return s;
}
}