Repository navigation
Expand file tree
/
Copy pathCliContext.java
More file actions
380 lines (313 loc) · 9.87 KB
/
Copy pathCliContext.java
File metadata and controls
380 lines (313 loc) · 9.87 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
package act.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.util.CommandLineParser;
import act.util.ActContext;
import jline.Terminal2;
import jline.console.ConsoleReader;
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 java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class CliContext extends ActContext.ActContextBase<CliContext> implements IASCIITable {
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 boolean rawPrint;
public CliContext(String line, App app, ConsoleReader console, CliSession session) {
super(app);
this.session = session;
parser = new CommandLineParser(line);
evaluatorCache = app.cache();
this.console = $.NPE(console);
Terminal2 t2 = $.cast(console.getTerminal());
t2.setEchoEnabled(false);
this.pw = new PrintWriter(console.getOutput());
this.rawPrint = null == System.getenv("cli-no-raw-print");
saveLocal();
}
/**
* Set the console prompt
* @param prompt the prompt
*/
public void prompt(String prompt) {
console.setPrompt(prompt);
}
/**
* 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();
}
/**
* Associate a value to a key in this context
* @param key the key
* @param val the value
*/
public CliContext attribute(String key, Object val) {
session.setAttribute(key, val);
return this;
}
/**
* Fetch the value {@link #attribute(String, Object) attributed} with
* the key specified in this context
* @param key the key
* @param <T> the generic type of the value
* @return the value object associated with the key
*/
public <T> T attribute(String key) {
return session.getAttribute(key);
}
public CliContext removeAttribute(String key) {
session.removeAttribute(key);
return this;
}
/**
* Return the current working directory
* @return the current working directory
*/
public File curDir() {
File file = attribute(ATTR_PWD);;
if (null == file) {
file = new File(System.getProperty("user.dir"));
attribute(ATTR_PWD, file);
}
return file;
}
public CliContext chDir(File dir) {
E.illegalArgumentIf(!dir.isDirectory());
attribute(ATTR_PWD, dir);
return this;
}
@Override
public CliContext accept(H.Format fmt) {
throw E.unsupport();
}
@Override
public H.Format accept() {
throw E.unsupport();
}
private void print0(String template, Object... args) {
try {
console.print(S.fmt(template, args));
} catch (IOException e) {
throw E.ioException(e);
}
}
public void flush() {
pw.flush();
}
public void print(String template, Object ... args) {
if (rawPrint) {
print1(template, args);
} else {
print0(template, args);
}
}
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(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();
}
public String commandPath() {
return commandPath;
}
public CliContext commandPath(String path) {
commandPath = path;
return this;
}
public <T> T newInstance(String className) {
return app().newInstance(className, this);
}
public <T> T newInstance(Class<? extends T> clazz) {
if (clazz == CliContext.class) return $.cast(this);
return app().newInstance(clazz, this);
}
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);
}
/**
* If {@link #templatePath(String) template path has been set before} then return
* the template path. Otherwise returns the {@link #commandPath()}
* @return either template path or action path if template path not set before
*/
public String templatePath() {
String path = super.templatePath();
if (S.notBlank(path)) {
return path;
} else {
return commandPath().replace('.', '/');
}
}
@Override
public CliContext templatePath(String templatePath) {
return super.templatePath(templatePath);
}
@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
*/
public CliContext __appRenderArgNames(String names) {
return renderArg("__arg_names__", C.listOf(names.split(",")));
}
public List<String> __appRenderArgNames() {
return renderArg("__arg_names__");
}
public void saveLocal() {
_local.set(this);
}
@Override
public Locale locale() {
return config().locale();
}
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) {
File file = new File(path);
if (file.isAbsolute()) {
return file;
}
file = new File(curDir(), path);
return new File(file.getAbsolutePath());
}
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;
}
}