-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.java
executable file
·1078 lines (921 loc) · 35.8 KB
/
rest.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.6.3
//DEPS com.google.code.gson:gson:2.9.0
//DEPS org.yaml:snakeyaml:1.30
//DEPS com.konghq:unirest-java:3.13.10
package scripts;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.yaml.snakeyaml.Yaml;
import kong.unirest.GetRequest;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.RequestBodyEntity;
import kong.unirest.Unirest;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.IExecutionExceptionHandler;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParentCommand;
import picocli.CommandLine.ParseResult;
@Command(name = "rest",
mixinStandardHelpOptions = true,
version = "rest 0.1",
description = "REST API access commands",
subcommands = {
Config.class,
Create.class,
Delete.class,
Get.class,
Filter.class,
Mapp.class,
Post.class,
Print.class,
Read.class,
Select.class,
ToArray.class,
ToObject.class,
Write.class
},
subcommandsRepeatable = true)
public class rest {
@Option(names = { "--url" }, description = "URL of REST API server")
String apiUrl;
@Option(names = { "--user", "-u" }, description = "Name of user to use for requests")
String user;
@Option(names = { "--password", "-p" }, description = "Password of user to use for requests")
String password;
@Option(names = { "--insecure", "-i" }, description = "Ignore SSL errors")
Boolean insecure;
@Option(names = { "--config", "-c" }, description = "Connection configuration to use")
String config;
@Option(names = { "--verbose", "-v" }, description = "Enable verbose output")
boolean[] verboses;
AppConfig appConfig;
Server activeServer = new Server();
ArrayDeque<List<Object>> resultsStack = new ArrayDeque<>(
Collections.singleton(Collections.singletonList(Util.newObject())));
List<Object> results() {
return resultsStack.peek();
}
boolean isVerbose() {
return isVerbose(0);
}
boolean isVerbose(int level) {
return verboses != null && verboses.length > level;
}
List<Object> elems(int index) {
int idx = index >= 0 ? resultsStack.size() - index - 1 : -index - 1;
if (idx > 0) {
return Util.asArray(resultsStack.toArray()[idx]);
} else {
return results();
}
}
void pushResult(Object obj) {
resultsStack.push(Collections.singletonList(obj));
}
void pushResults(List<Object> obj) {
resultsStack.push(obj);
}
String getApiUrl(String api) {
if (activeServer.url == null) {
throw new IllegalArgumentException(
"Missing API URL. Either specify it on the command line or in the configuration file.");
}
return activeServer.url + api;
}
HttpResponse<JsonNode> apiDelete(String api, Object body) {
String url = getApiUrl(api);
logApiRequest("DELETE", api, body);
RequestBodyEntity req = Unirest.delete(url).body(body);
if (activeServer.user != null && activeServer.password != null) {
req.basicAuth(activeServer.user, activeServer.password);
}
HttpResponse<JsonNode> res = req.asJson();
logApiResult(res);
return res;
}
HttpResponse<JsonNode> apiGet(String api) {
String url = getApiUrl(api);
logApiRequest("GET", api, null);
GetRequest req = Unirest.get(url);
if (activeServer.user != null && activeServer.password != null) {
req.basicAuth(activeServer.user, activeServer.password);
}
HttpResponse<JsonNode> res = req.asJson();
logApiResult(res);
return res;
}
HttpResponse<JsonNode> apiPost(String api, Object body) {
String url = getApiUrl(api);
logApiRequest("POST", api, body);
RequestBodyEntity req = Unirest.post(url).body(body);
if (activeServer.user != null && activeServer.password != null) {
req.basicAuth(activeServer.user, activeServer.password);
}
HttpResponse<JsonNode> res = req.asJson();
logApiResult(res);
return res;
}
void logApiRequest(String action, String api, Object body) {
if (isVerbose()) {
String url = getApiUrl(api);
System.err.println(action + " " + url);
if (isVerbose(1) && body != null) {
System.err.println("Body: " + body);
}
}
}
void logApiResult(HttpResponse<JsonNode> res) {
if (isVerbose()) {
System.err.println("Response: " + Util.status(res) + " " + Util.statusText(res));
if (isVerbose(2)) {
System.err.println("Result: " + res.getBody().toPrettyString());
}
}
}
void logApiResult() {
if (isVerbose()) {
System.err.println("Response: NONE (Dry Run)");
}
}
boolean match(Object entry, List<Pair<String, Pattern>> patterns) {
boolean result = patterns.stream().anyMatch(p -> {
List<Object> values = Util.select(entry, p.first).collect(Collectors.toList());
boolean res = values.stream().anyMatch(value -> p.second.matcher(value.toString()).find());
if (isVerbose(2)) {
System.err.println("match: " + values + " ~ " + p.second + " = " + res);
}
return res;
});
if (isVerbose(2)) {
System.err.println("final match result: " + result);
}
return result;
}
public static void main(String... args) {
rest app = new rest();
int exitCode = new CommandLine(app)
.setExecutionStrategy(app::executionStrategy)
.setExecutionExceptionHandler(app.new ExecHandler())
.execute(args);
System.exit(exitCode);
}
private int executionStrategy(ParseResult parseResult) {
if (Config.configFile.isFile()) {
Gson gson = new Gson();
try (JsonReader reader = new JsonReader(new FileReader(Config.configFile))) {
appConfig = gson.fromJson(reader, AppConfig.class);
} catch (IOException ex) {
System.err.println("Error reading configuration file. Ignoring. (" + ex.getMessage() + ")");
}
if (appConfig != null) {
String srv = config != null ? config : appConfig.defaultServer;
if (srv != null) {
if (appConfig.servers != null) {
activeServer = appConfig.servers.get(srv);
}
}
}
}
boolean insec = insecure != null ? insecure : activeServer.insecure == Boolean.TRUE;
Unirest.config().verifySsl(!insec);
String usr = user != null ? user : activeServer.user;
String pwd = password != null ? password : activeServer.password;
String url = apiUrl != null ? apiUrl : activeServer.url;
Server srv = new Server();
srv.url = url;
srv.user = usr;
srv.password = pwd;
activeServer = srv;
return new CommandLine.RunLast().execute(parseResult);
}
class ExecHandler implements IExecutionExceptionHandler {
@Override
public int handleExecutionException(Exception ex, CommandLine cmd, ParseResult parse) throws Exception {
if (isVerbose()) {
cmd.getErr().print("ERROR: ");
ex.printStackTrace(cmd.getErr());
} else {
cmd.getErr().println("ERROR: " + ex.toString());
}
return cmd.getExitCodeExceptionMapper() != null ? cmd.getExitCodeExceptionMapper().getExitCode(ex)
: cmd.getCommandSpec().exitCodeOnExecutionException();
}
}
}
@Command(name = "config", mixinStandardHelpOptions = true, description = "Configuration management", subcommands = {
ConfigDelete.class,
ConfigList.class,
ConfigSave.class,
ConfigUse.class
})
class Config {
@ParentCommand
protected rest app;
final static File configFile = new File(System.getProperty("user.home"), ".restcfg");
}
abstract class BaseConfigCmd implements Callable<Integer> {
@ParentCommand
protected Config cfg;
@Parameters(index = "0", description = "Configuration name", arity = "1")
protected String name;
protected void saveConfig(AppConfig appConfig) throws IOException {
Gson gson = new Gson();
try (JsonWriter writer = new JsonWriter(new FileWriter(Config.configFile))) {
gson.toJson(appConfig, AppConfig.class, writer);
}
}
}
@Command(name = "delete", mixinStandardHelpOptions = true, description = "Delete configuration")
class ConfigDelete extends BaseConfigCmd {
@Override
public Integer call() throws Exception {
AppConfig ac = cfg.app.appConfig;
if (ac == null || !ac.servers.containsKey(name)) {
throw new IllegalArgumentException("No configuration with that name exists");
}
ac.servers.remove(name);
if (name.equals(ac.defaultServer)) {
if (ac.servers.isEmpty()) {
ac.defaultServer = null;
} else {
// Just get any name from the list of servers
ac.defaultServer = ac.servers.keySet().iterator().next();
}
}
saveConfig(ac);
return 0;
}
}
@Command(name = "list", mixinStandardHelpOptions = true, description = "List configurations")
class ConfigList implements Callable<Integer> {
@ParentCommand
protected Config cfg;
@Override
public Integer call() throws Exception {
AppConfig ac = cfg.app.appConfig;
if (ac != null) {
boolean first = true;
for (Entry<String, Server> e : ac.servers.entrySet()) {
if (!first) {
System.out.println();
}
System.out.print("Name : " + e.getKey());
if (e.getKey().equals(ac.defaultServer)) {
System.out.print(" [active]");
}
System.out.println();
System.out.println("URL : " + e.getValue().url);
if (e.getValue().insecure != null) {
System.out.println("Insecure: " + e.getValue().insecure);
}
first = false;
}
}
return 0;
}
}
@Command(name = "save", mixinStandardHelpOptions = true, description = "Save configuration")
class ConfigSave extends BaseConfigCmd {
@Override
public Integer call() throws Exception {
// A bit of a hack to force argument validation
cfg.app.getApiUrl("");
AppConfig ac = cfg.app.appConfig;
if (ac == null) {
cfg.app.appConfig = ac = new AppConfig();
}
if (ac.servers == null) {
ac.servers = new HashMap<>();
}
Server srv = new Server();
srv.url = cfg.app.apiUrl;
srv.user = cfg.app.user;
srv.password = cfg.app.password;
srv.insecure = cfg.app.insecure;
ac.servers.put(name, srv);
if (ac.defaultServer == null) {
ac.defaultServer = name;
}
saveConfig(ac);
return 0;
}
}
@Command(name = "use", mixinStandardHelpOptions = true, description = "Set active configuration")
class ConfigUse extends BaseConfigCmd {
@Override
public Integer call() throws Exception {
AppConfig ac = cfg.app.appConfig;
if (ac == null || !ac.servers.containsKey(name)) {
throw new IllegalArgumentException("No configuration with that name exists");
}
ac.defaultServer = name;
saveConfig(ac);
return 0;
}
}
@Command(name = "create", mixinStandardHelpOptions = true, description = "Create new object")
class Create implements Callable<Integer> {
@ParentCommand
private rest app;
@Option(names = { "--literal", "-l" }, description = "Use literal value as starting point")
private String literal;
@Option(names = { "--value", "-v" }, description = "Set key=value properties on object where value is a literal")
private String[] props;
@Option(names = { "--select", "-s" }, description = "Set key=path properties on object where path selects a value form a previous result")
private String[] selects;
@Override
public Integer call() throws Exception {
List<Object> elems = Collections.singletonList(Util.newObject());
List<Object> results = elems.stream()
.map(elem -> {
Map<String, Object> obj;
if (literal != null) {
obj = Util.newObject(literal);
} else {
obj = Util.asObject(Util.jsonClone(elem));
}
if (props != null) {
for (String prop : props) {
String[] kv = prop.split("=", 2);
String key = kv[0];
String val = kv.length == 2 ? kv[1] : key;
Util.set(obj, key, val);
}
}
if (selects != null) {
for (String select : selects) {
String[] kp = select.split("=", 2);
String key = kp[0];
String path = kp.length == 2 ? kp[1] : key;
String val = Util.select(elem, path).map(Object::toString).collect(Collectors.joining());
Util.set(obj, key, val);
}
}
return obj;
})
.collect(Collectors.toList());
app.pushResults(results);
return 0;
}
}
@Command(name = "delete", mixinStandardHelpOptions = true, description = "Perform REST API delete request")
class Delete implements Callable<Integer> {
@ParentCommand
private rest app;
@Parameters(index = "0", description = "REST API resource path", arity = "1")
private String api;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Option(names = { "--dry-run" }, description = "Only shows what will happen but doesn't perform any changes")
private boolean dryRun;
@Option(names = { "--no-body" }, description = "No body will be sent")
private boolean noBody;
@Option(names = { "--ignore-result", "-i" }, description = "Ignores the result of the post")
private boolean ignoreResult;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
List<Object> results = elems.stream()
.map(elem -> {
String newapi = Util.replaceVars(api, elem);
Object result;
if (!dryRun) {
HttpResponse<JsonNode> response = app.apiDelete(newapi, noBody ? null : elem);
result = response.getBody().isArray() ? response.getBody().getArray().toList() : response.getBody().getObject().toMap();
if (!Util.ok(response)) {
System.err.println(Util.statusText(response));
}
} else {
app.logApiRequest("DELETE", newapi, noBody ? null : elem);
app.logApiResult();
// Not really logical but it might be useful
result = elem;
}
return result;
})
.collect(Collectors.toList());
if (!ignoreResult) {
app.pushResults(results);
}
return 0;
}
}
@Command(name = "get", mixinStandardHelpOptions = true, description = "Retrieve JSON from REST API request")
class Get implements Callable<Integer> {
@ParentCommand
private rest app;
@Parameters(index = "0", description = "REST API resource path", arity = "1")
private String api;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
List<Object> results = elems.stream()
.map(elem -> {
String newapi = Util.replaceVars(api, elem);
HttpResponse<JsonNode> response = app.apiGet(newapi);
Object result = response.getBody().isArray() ? response.getBody().getArray().toList() : response.getBody().getObject().toMap();
if (!Util.ok(response)) {
System.err.println(Util.statusText(response));
}
return result;
})
.collect(Collectors.toList());
app.pushResults(results);
return 0;
}
}
@Command(name = "filter", mixinStandardHelpOptions = true, description = "Filter values")
class Filter implements Callable<Integer> {
@ParentCommand
private rest app;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Parameters(description = "Condition to apply", arity = "*")
private String[] conditions;
@Override
public Integer call() throws Exception {
List<Pair<String, Pattern>> patterns = Arrays.stream(conditions)
.map(test -> test.split("[=:]", 2))
.filter(test -> test.length == 2)
.map(pop -> new Pair<String, Pattern>(pop[0], Pattern.compile(pop[1])))
.collect(Collectors.toList());
List<Object> elems = app.elems(fromIndex);
app.pushResults(elems.stream()
.filter(elem -> app.match(elem, patterns))
.collect(Collectors.toList()));
return 0;
}
}
@Command(name = "map", mixinStandardHelpOptions = true, description = "Transform values")
class Mapp implements Callable<Integer> {
@ParentCommand
private rest app;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Option(names = { "--literal", "-l" }, description = "Use literal value as starting point")
private String literal;
@Option(names = { "--value", "-v" }, description = "Set key=value properties on object where value is a literal")
private String[] props;
@Option(names = { "--select", "-s" }, description = "Set key=path properties on object where path selects a value form a previous result")
private String[] selects;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
List<Object> results = elems.stream()
.map(elem -> {
Map<String, Object> obj;
if (literal != null) {
obj = Util.newObject(literal);
} else {
obj = Util.asObject(Util.jsonClone(elem));
}
if (props != null) {
for (String prop : props) {
String[] kv = prop.split("=", 2);
String key = kv[0];
String val = kv.length == 2 ? kv[1] : key;
Util.set(obj, key, val);
}
}
if (selects != null) {
for (String select : selects) {
String[] kp = select.split("=", 2);
String key = kp[0];
String path = kp.length == 2 ? kp[1] : key;
String val = Util.select(elem, path).map(Object::toString).collect(Collectors.joining());
Util.set(obj, key, val);
}
}
return obj;
})
.collect(Collectors.toList());
app.pushResults(results);
return 0;
}
}
@Command(name = "post", mixinStandardHelpOptions = true, description = "Post values")
class Post implements Callable<Integer> {
@ParentCommand
private rest app;
@Parameters(index = "0", description = "REST API resource path", arity = "1")
private String api;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Option(names = { "--dry-run" }, description = "Only shows what will happen but doesn't perform any changes")
private boolean dryRun;
@Option(names = { "--no-body" }, description = "No body will be sent")
private boolean noBody;
@Option(names = { "--ignore-result", "-i" }, description = "Ignores the result of the post")
private boolean ignoreResult;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
List<Object> results = elems.stream()
.map(elem -> {
String newapi = Util.replaceVars(api, elem);
Object result;
if (!dryRun) {
HttpResponse<JsonNode> response = app.apiPost(newapi, noBody ? null : elem);
result = response.getBody().isArray() ? response.getBody().getArray().toList() : response.getBody().getObject().toMap();
if (!Util.ok(response)) {
System.err.println(Util.statusText(response));
}
} else {
app.logApiRequest("POST", newapi, noBody ? null : elem);
app.logApiResult();
// Not really logical but it might be useful
result = elem;
}
return result;
})
.collect(Collectors.toList());
if (!ignoreResult) {
app.pushResults(results);
}
return 0;
}
}
@Command(name = "print", mixinStandardHelpOptions = true, description = "Print result")
class Print implements Callable<Integer> {
@ParentCommand
private rest app;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@ArgGroup(exclusive = true)
Format format;
@Option(names = { "--raw", "-r" }, description = "Print raw output, don't prettify")
private boolean raw;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
for (Object obj : elems) {
if (format != null && format.yaml) {
Yaml yaml = new Yaml();
if (!raw && obj instanceof Map) {
System.out.println(yaml.dump(obj));
} else if (!raw && obj instanceof Collection) {
System.out.println(yaml.dump(obj));
} else if (obj != null) {
System.out.println(obj.toString());
}
} else {
if (!raw && obj instanceof Map) {
System.out.println(new JSONObject(Util.asObject(obj)).toString(2));
} else if (!raw && obj instanceof Collection) {
System.out.println(new JSONArray(Util.asArray(obj)).toString(2));
} else if (obj != null) {
System.out.println(obj.toString());
}
}
}
return 0;
}
}
@Command(name = "read", mixinStandardHelpOptions = true, description = "Read JSON from file")
class Read implements Callable<Integer> {
@ParentCommand
private rest app;
@Parameters(index = "0", description = "File to read, use '-' to read from stdin", arity = "1")
private String path;
@ArgGroup(exclusive = true)
Format format;
@Override
public Integer call() throws Exception {
Format fmt = Util.determineFormat(format, path);
if ("-".equals(path)) {
try (InputStreamReader reader = new InputStreamReader(System.in)) {
Object obj;
if (fmt.yaml) {
Yaml yaml = new Yaml();
obj = yaml.load(reader);
} else {
Gson gson = new Gson();
obj = gson.fromJson(reader, Object.class);
}
app.pushResult(obj);
}
} else {
try (FileReader reader = new FileReader(path)) {
Object obj;
if (fmt.yaml) {
Yaml yaml = new Yaml();
obj = yaml.load(reader);
} else {
Gson gson = new Gson();
obj = gson.fromJson(reader, Object.class);
}
app.pushResult(obj);
}
}
return 0;
}
}
@Command(name = "select", mixinStandardHelpOptions = true, description = "Select values")
class Select implements Callable<Integer> {
@ParentCommand
private rest app;
@Parameters(index = "0", description = "JSON value selector", arity = "?")
private String select;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
app.pushResults(elems.stream()
.flatMap(elem -> Util.select(elem, select))
.collect(Collectors.toList()));
return 0;
}
}
@Command(name = "to-array", mixinStandardHelpOptions = true, description = "Turn previous results into a single JSON array")
class ToArray implements Callable<Integer> {
@ParentCommand
private rest app;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
if (elems.size() == 1 && elems.get(0) instanceof List) {
app.pushResults(elems);
} else {
if (elems.isEmpty()) {
app.pushResult(Util.newArray());
} else {
app.pushResult(Util.newArray(elems));
}
}
return 0;
}
}
@Command(name = "to-object", mixinStandardHelpOptions = true, description = "Turn previous results into a single JSON object")
class ToObject implements Callable<Integer> {
@ParentCommand
private rest app;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@Option(names = { "--key" }, description = "The path to a property to use as key")
private String keyPath;
@Option(names = { "--value" }, description = "The path to a property to use as value", defaultValue = ".")
private String valuePath;
@Override
public Integer call() throws Exception {
List<Object> elems = app.elems(fromIndex);
if (elems.size() == 1 && elems.get(0) instanceof Map) {
app.pushResults(elems);
} else {
Map<String, Object> obj = Util.newObject();
if (!elems.isEmpty()) {
int idx = 0;
for (Object elem : elems) {
String key;
if (keyPath == null) {
key = Integer.toString(idx++);
} else {
key = Util.select(elem, keyPath)
.map(e -> e.toString())
.collect(Collectors.joining());
}
Object value = Util.select(elem, valuePath).findFirst().get();
obj.put(key, value);
}
}
app.pushResult(obj);
}
return 0;
}
}
@Command(name = "write", mixinStandardHelpOptions = true, description = "Write JSON to file")
class Write implements Callable<Integer> {
@ParentCommand
private rest app;
@Parameters(index = "0", description = "File to write", arity = "1")
private String path;
@Option(names = { "--from", "-f" }, description = "Use indicated result to operate on", defaultValue = "-1")
private int fromIndex;
@ArgGroup(exclusive = true)
Format format;
@Option(names = { "--raw", "-r" }, description = "Write raw output, don't prettify")
private boolean raw;
@Override
public Integer call() throws Exception {
Format fmt = Util.determineFormat(format, path);
if (fmt.yaml) {
Yaml yaml = new Yaml();
List<Object> elems = app.elems(fromIndex);
try (FileWriter writer = new FileWriter(path)) {
elems.forEach(elem -> yaml.dump(elem, writer));
}
} else {
GsonBuilder builder = new GsonBuilder();
if (!raw) {
builder.setPrettyPrinting();
}
Gson gson = builder.create();
List<Object> elems = app.elems(fromIndex);
try (FileWriter writer = new FileWriter(path)) {
elems.forEach(elem -> gson.toJson(elem, writer));
}
}
return 0;
}
}
class Format {
@Option(names = { "--json" }, description = "Set JSON format")
boolean json;
@Option(names = { "--yaml" }, description = "Set YAML format")
boolean yaml;
}
class Pair<U,V> {
public final U first;
public final V second;
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
}
class Server {
String url;
String user;
String password;
Boolean insecure;
}
class AppConfig {
Map<String,Server> servers;
String defaultServer;
}
class Util {
public static Map<String, Object> newObject() {
return new HashMap<>();
}
public static Format determineFormat(Format format, String path) {
if (format != null) {
return format;
} else {
Format fmt = new Format();
fmt.json = path.endsWith(".json");
fmt.yaml = path.endsWith(".yaml") || path.endsWith(".yml");
return fmt;
}
}
public static Map<String, Object> newObject(Map<String, Object> map) {
return new HashMap<>(map);
}
public static Map<String, Object> newObject(String literal) {
return asObject(new JSONObject(literal).toMap());
}
public static Map<String, Object> asObject(Object obj) {
return (Map<String, Object>)obj;
}
public static List<Object> newArray() {
return new ArrayList<>();
}
public static List<Object> newArray(List<Object> items) {
return new ArrayList<>(items);
}
public static List<Object> newArray(String literal) {
return asArray(new JSONArray(literal).toList());
}
public static List<Object> asArray(Object obj) {
return (List<Object>)obj;
}
static int status(HttpResponse<JsonNode> response) {
if (response.getStatus() >= 200 && response.getStatus() < 300 && response.getParsingError().isPresent()) {
return 400;
}
return response.getStatus();
}
static boolean ok(HttpResponse<JsonNode> response) {
return (Util.status(response) >= 200 && Util.status(response) < 300);
}
static String statusText(HttpResponse<JsonNode> response) {
if (response.getStatus() == 200 && response.getParsingError().isPresent()) {
return response.getParsingError().get().toString();
}
return response.getStatusText();
}
static Stream<Object> select(Object start, String path) {
Stream<Object> nodes;
Object node = start;
if (node == null || path == null || path.isEmpty()) {
nodes = node != null ? Stream.of(node) : Stream.empty();
} else {
String elems[] = path.split("/", 2);
String key = elems[0];
String index = null;
if (key.endsWith("]")) {
int p = key.indexOf("[");
if (p >= 0) {
index = key.substring(p + 1, key.length() - 1);
key = key.substring(0, p);
}
}
if (!key.isEmpty() && !key.equals(".")) {
if (!(node instanceof Map)) {
return Stream.empty();
}