-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
LiquibaseCommandLine.java
1235 lines (1030 loc) · 54.8 KB
/
LiquibaseCommandLine.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
package liquibase.integration.commandline;
import liquibase.Scope;
import liquibase.command.CommandArgumentDefinition;
import liquibase.command.CommandDefinition;
import liquibase.command.CommandFactory;
import liquibase.command.CommandFailedException;
import liquibase.command.core.*;
import liquibase.configuration.ConfigurationDefinition;
import liquibase.configuration.ConfigurationValueProvider;
import liquibase.configuration.ConfiguredValue;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.configuration.core.DefaultsFileValueProvider;
import liquibase.exception.CommandLineParsingException;
import liquibase.exception.CommandValidationException;
import liquibase.hub.HubConfiguration;
import liquibase.license.LicenseService;
import liquibase.license.LicenseServiceFactory;
import liquibase.logging.LogService;
import liquibase.logging.core.JavaLogService;
import liquibase.resource.*;
import liquibase.ui.ConsoleUIService;
import liquibase.ui.UIService;
import liquibase.util.*;
import picocli.CommandLine;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.Duration;
import java.util.*;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.logging.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.ResourceBundle.getBundle;
import static liquibase.util.SystemUtil.isWindows;
public class LiquibaseCommandLine {
private final Map<String, String> legacyPositionalArguments;
/**
* Arguments that used to be global arguments but are now command-level
*/
private final Set<String> legacyNoLongerGlobalArguments;
/**
* Arguments that used to be command arguments but are now global
*/
private final Set<String> legacyNoLongerCommandArguments;
private Level configuredLogLevel;
private final CommandLine commandLine;
private FileHandler fileHandler;
private final ResourceBundle coreBundle = getBundle("liquibase/i18n/liquibase-core");
/**
* Pico's defaultFactory does a lot of reflection, checking for classes we know we don't have.
* That is slow on older JVMs and impact initial startup time, so do our own factory for performance reasons.
* It is easy to configure pico to it's default factory, when profiling check for `CommandLine$DefaultFactory` usage
*/
private CommandLine.IFactory defaultFactory = new CommandLine.IFactory() {
@Override
public <K> K create(Class<K> cls) throws Exception {
return cls.newInstance();
}
};
public static void main(String[] args) {
//we don't ship jansi, so we know we can disable it without having to do the slow class checking
System.setProperty("org.fusesource.jansi.Ansi.disable", "true");
final LiquibaseCommandLine cli = new LiquibaseCommandLine();
int returnCode = cli.execute(args);
System.exit(returnCode);
}
private void cleanup() {
if (fileHandler != null) {
fileHandler.flush();
fileHandler.close();
}
}
public LiquibaseCommandLine() {
this.legacyPositionalArguments = new HashMap<>();
this.legacyPositionalArguments.put("calculatechecksum", CalculateChecksumCommandStep.CHANGESET_IDENTIFIER_ARG.getName());
this.legacyPositionalArguments.put("changelogsynctotag", ChangelogSyncToTagCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("changelogsynctotagsql", ChangelogSyncToTagSqlCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("dbdoc", DbDocCommandStep.OUTPUT_DIRECTORY_ARG.getName());
this.legacyPositionalArguments.put("futurerollbackcountsql", FutureRollbackCountSqlCommandStep.COUNT_ARG.getName());
this.legacyPositionalArguments.put("futurerollbackfromtagsql", FutureRollbackFromTagSqlCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("tag", TagCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("tagexists", TagExistsCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("rollback", RollbackCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("rollbacksql", RollbackSqlCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("rollbacktodate", RollbackToDateCommandStep.DATE_ARG.getName());
this.legacyPositionalArguments.put("rollbacktodatesql", RollbackToDateSqlCommandStep.DATE_ARG.getName());
this.legacyPositionalArguments.put("rollbackcount", RollbackCountCommandStep.COUNT_ARG.getName());
this.legacyPositionalArguments.put("rollbackcountsql", RollbackCountSqlCommandStep.COUNT_ARG.getName());
this.legacyPositionalArguments.put("updatecount", UpdateCountCommandStep.COUNT_ARG.getName());
this.legacyPositionalArguments.put("updatecountsql", UpdateCountSqlCommandStep.COUNT_ARG.getName());
this.legacyPositionalArguments.put("updatetotag", UpdateToTagCommandStep.TAG_ARG.getName());
this.legacyPositionalArguments.put("updatetotagsql", UpdateToTagSqlCommandStep.TAG_ARG.getName());
this.legacyNoLongerGlobalArguments = Stream.of(
"username",
"password",
"url",
"outputDefaultSchema",
"outputDefaultCatalog",
"changelogFile",
"hubConnectionId",
"hubProjectId",
"contexts",
"labels",
"diffTypes",
"changesetAuthor",
"changesetContext",
"dataOutputDirectory",
"referenceDriver",
"referenceUrl",
"referenceUsername",
"referencePassword",
"referenceDefaultCatalogName",
"referenceDefaultSchemaName",
"excludeObjects",
"includeCatalog",
"includeObjects",
"includeSchema",
"includeTablespace",
"outputSchemasAs",
"referenceSchemas",
"schemas",
"snapshotFormat",
"sqlFile",
"delimiter",
"rollbackScript",
"overwriteOutputFile",
"changeExecListenerClass",
"changeExecListenerPropertiesFile",
"defaultSchemaName",
"defaultCatalogName"
).collect(Collectors.toSet());
this.legacyNoLongerCommandArguments = Stream.of(
"databaseClass",
"liquibaseCatalogName",
"liquibaseSchemaName",
"databaseChangeLogTableName",
"databaseChangeLogLockTableName",
"classpath",
"propertyProviderClass",
"promptForNonLocalDatabase",
"includeSystemClasspath",
"defaultsFile",
"currentDateTimeFunction",
"logLevel",
"logFile",
"outputFile",
"liquibaseProLicenseKey",
"liquibaseHubApiKey",
"outputFileEncoding",
"outputLineSeparator"
).collect(Collectors.toSet());
this.commandLine = buildPicoCommandLine();
}
private CommandLine buildPicoCommandLine() {
final CommandLine.Model.CommandSpec rootCommandSpec = CommandLine.Model.CommandSpec.wrapWithoutInspection(null, defaultFactory);
rootCommandSpec.name("liquibase");
configureHelp(rootCommandSpec, true);
rootCommandSpec.subcommandsCaseInsensitive(true);
rootCommandSpec.usageMessage()
.customSynopsis("liquibase [GLOBAL OPTIONS] [COMMAND] [COMMAND OPTIONS]\nCommand-specific help: \"liquibase <command-name> --help\"")
.optionListHeading("\nGlobal Options\n")
.commandListHeading("\nCommands\n")
;
CommandLine commandLine = new CommandLine(rootCommandSpec, defaultFactory)
.setCaseInsensitiveEnumValuesAllowed(true)
.setOptionsCaseInsensitive(true)
.setUsageHelpAutoWidth(true);
addGlobalArguments(commandLine);
for (CommandDefinition commandDefinition : getCommands()) {
addSubcommand(commandDefinition, commandLine);
}
commandLine.setExecutionExceptionHandler((ex, commandLine1, parseResult) -> LiquibaseCommandLine.this.handleException(ex));
return commandLine;
}
private int handleException(Throwable exception) {
Throwable cause = exception;
String bestMessage = exception.getMessage();
while (cause.getCause() != null) {
if (StringUtil.trimToNull(cause.getMessage()) != null) {
bestMessage = cause.getMessage();
}
cause = cause.getCause();
}
if (bestMessage == null) {
bestMessage = exception.getClass().getName();
} else {
//clean up message
bestMessage = bestMessage.replaceFirst("^[\\w.]*exception[\\w.]*: ", "");
bestMessage = bestMessage.replace("Unexpected error running Liquibase: ", "");
}
if (cause instanceof CommandFailedException && ((CommandFailedException) cause).isExpected()) {
Scope.getCurrentScope().getLog(getClass()).severe(bestMessage);
} else {
Scope.getCurrentScope().getLog(getClass()).severe(bestMessage, exception);
}
boolean printUsage = false;
try (final StringWriter suggestionWriter = new StringWriter();
PrintWriter suggestionsPrintWriter = new PrintWriter(suggestionWriter)) {
if (exception instanceof CommandLine.ParameterException) {
if (exception instanceof CommandLine.UnmatchedArgumentException) {
System.err.println("Unexpected argument(s): " + StringUtil.join(((CommandLine.UnmatchedArgumentException) exception).getUnmatched(), ", "));
} else {
System.err.println("Error parsing command line: " + bestMessage);
}
CommandLine.UnmatchedArgumentException.printSuggestions((CommandLine.ParameterException) exception, suggestionsPrintWriter);
printUsage = true;
} else if (exception instanceof IllegalArgumentException
|| exception instanceof CommandValidationException
|| exception instanceof CommandLineParsingException) {
System.err.println("Error parsing command line: " + bestMessage);
printUsage = true;
} else if (exception.getCause() != null && exception.getCause() instanceof CommandFailedException) {
System.err.println(bestMessage);
} else {
System.err.println("\nUnexpected error running Liquibase: " + bestMessage);
System.err.println();
if (Level.OFF.equals(this.configuredLogLevel)) {
System.err.println("For more information, please use the --log-level flag");
} else {
if (LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentValue() == null) {
exception.printStackTrace(System.err);
}
}
}
if (printUsage) {
System.err.println();
System.err.println("For detailed help, try 'liquibase --help' or 'liquibase <command-name> --help'");
}
suggestionsPrintWriter.flush();
final String suggestions = suggestionWriter.toString();
if (suggestions.length() > 0) {
System.err.println();
System.err.println(suggestions);
}
} catch (IOException e) {
Scope.getCurrentScope().getLog(getClass()).warning("Error closing stream: " + e.getMessage(), e);
}
if (exception.getCause() != null && exception.getCause() instanceof CommandFailedException) {
CommandFailedException cfe = (CommandFailedException) exception.getCause();
return cfe.getExitCode();
}
return 1;
}
public int execute(String[] args) {
try {
final String[] finalArgs = adjustLegacyArgs(args);
configureLogging(Level.OFF, null);
Main.runningFromNewCli = true;
final List<ConfigurationValueProvider> valueProviders = registerValueProviders(finalArgs);
try {
return Scope.child(configureScope(), () -> {
if (!LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentValue()) {
Scope.getCurrentScope().getUI().sendErrorMessage((
String.format(coreBundle.getString("did.not.run.because.param.was.set.to.false"),
LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentConfiguredValue().getProvidedValue().getActualKey())));
return 0;
}
configureVersionInfo();
if (!wasHelpOrVersionRequested()) {
Scope.getCurrentScope().getUI().sendMessage(CommandLineUtils.getBanner());
Scope.getCurrentScope().getUI().sendMessage(String.format(coreBundle.getString("version.number"), LiquibaseUtil.getBuildVersionInfo()));
final LicenseService licenseService = Scope.getCurrentScope().getSingleton(LicenseServiceFactory.class).getLicenseService();
if (licenseService == null) {
Scope.getCurrentScope().getUI().sendMessage("WARNING: License service not loaded, cannot determine Liquibase Pro license status. Please consider re-installing Liquibase to include all dependencies. Continuing operation without Pro license.");
} else {
Scope.getCurrentScope().getUI().sendMessage(licenseService.getLicenseInfo());
}
}
CommandLine.ParseResult subcommandParseResult = commandLine.getParseResult();
while (subcommandParseResult.hasSubcommand()) {
subcommandParseResult = subcommandParseResult.subcommand();
}
Map<String, String> changelogParameters = subcommandParseResult.matchedOptionValue("-D", new HashMap<>());
if (changelogParameters.size() != 0) {
Main.newCliChangelogParameters = changelogParameters;
}
enableMonitoring();
int response = commandLine.execute(finalArgs);
if (!wasHelpOrVersionRequested()) {
final ConfiguredValue<File> logFile = LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentConfiguredValue();
if (logFile.found()) {
Scope.getCurrentScope().getUI().sendMessage("Logs saved to " + logFile.getValue().getAbsolutePath());
}
final ConfiguredValue<File> outputFile = LiquibaseCommandLineConfiguration.OUTPUT_FILE.getCurrentConfiguredValue();
if (outputFile.found()) {
Scope.getCurrentScope().getUI().sendMessage("Output saved to " + outputFile.getValue().getAbsolutePath());
}
if (response == 0) {
final List<CommandLine> commandList = commandLine.getParseResult().asCommandLineList();
final String commandName = StringUtil.join(getCommandNames(commandList.get(commandList.size() - 1)), " ");
Scope.getCurrentScope().getUI().sendMessage("Liquibase command '" + commandName + "' was executed successfully.");
}
}
return response;
});
} finally {
final LiquibaseConfiguration liquibaseConfiguration = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class);
for (ConfigurationValueProvider provider : valueProviders) {
liquibaseConfiguration.unregisterProvider(provider);
}
}
} catch (Throwable e) {
handleException(e);
return 1;
} finally {
cleanup();
}
}
protected void enableMonitoring() {
final liquibase.logging.Logger log = Scope.getCurrentScope().getLog(getClass());
try {
final String monitorPerformanceValue = LiquibaseCommandLineConfiguration.MONITOR_PERFORMANCE.getCurrentValue();
if (monitorPerformanceValue == null || monitorPerformanceValue.equalsIgnoreCase("false")) {
log.fine("Performance monitoring disabled");
return;
}
if (SystemUtil.getJavaMajorVersion() < 11) {
Scope.getCurrentScope().getUI().sendMessage("Performance monitoring requires Java 11 or greater. Version " + SystemUtil.getJavaVersion() + " is not supported.");
return;
}
String filename = monitorPerformanceValue;
if (filename.equalsIgnoreCase("true")) {
filename = "liquibase-" + new ISODateFormat().format(new Date()).replaceAll("\\W", "_") + ".jfr";
}
if (!filename.endsWith(".jfr")) {
filename = filename + ".jfr";
}
final Class<?> configurationClass = Class.forName("jdk.jfr.Configuration");
final Class<?> recordingClass = Class.forName("jdk.jfr.Recording");
Object configuration = configurationClass.getMethod("getConfiguration", String.class).invoke(null, "profile");
Object recording = recordingClass.getConstructor(configurationClass).newInstance(configuration);
recordingClass.getMethod("setMaxSize", long.class).invoke(recording, 0L);
recordingClass.getMethod("setMaxAge", Duration.class).invoke(recording, (Duration) null);
recordingClass.getMethod("setDumpOnExit", boolean.class).invoke(recording, true);
recordingClass.getMethod("setToDisk", boolean.class).invoke(recording, true);
final File filePath = new File(filename).getAbsoluteFile();
filePath.getParentFile().mkdirs();
recordingClass.getMethod("setDestination", Path.class).invoke(recording, filePath.toPath());
recordingClass.getMethod("start").invoke(recording);
Scope.getCurrentScope().getUI().sendMessage("Saving performance data to " + filePath.getAbsolutePath());
} catch (Throwable e) {
final String message = "Error enabling performance monitoring: " + e.getMessage();
Scope.getCurrentScope().getUI().sendMessage(message);
log.warning(message, e);
}
}
private boolean wasHelpOrVersionRequested() {
CommandLine.ParseResult parseResult = commandLine.getParseResult();
while (parseResult != null) {
if (parseResult.isUsageHelpRequested() || parseResult.isVersionHelpRequested()) {
return true;
}
parseResult = parseResult.subcommand();
}
return false;
}
protected String[] adjustLegacyArgs(String[] args) {
List<String> returnArgs = new ArrayList<>();
final ListIterator<String> iterator = Arrays.asList(args).listIterator();
while (iterator.hasNext()) {
String arg = iterator.next();
String argAsKey = arg.replace("-", "").toLowerCase();
if (arg.startsWith("-")) {
returnArgs.add(arg);
} else {
final String legacyTag = this.legacyPositionalArguments.get(argAsKey);
if (legacyTag == null) {
returnArgs.add(arg);
} else {
returnArgs.add(arg);
String value = " ";
while (iterator.hasNext()) {
arg = iterator.next();
if (arg.startsWith("-")) {
iterator.previous();
break;
} else {
value += arg + " ";
}
}
value = StringUtil.trimToNull(value);
if (value != null) {
returnArgs.add("--" + legacyTag);
returnArgs.add(value);
}
}
}
}
return returnArgs.toArray(new String[0]);
}
static String[] getCommandNames(CommandLine parseResult) {
List<String> returnList = new ArrayList<>();
while (!parseResult.getCommandName().equals("liquibase")) {
returnList.add(0, parseResult.getCommandName());
parseResult = parseResult.getParent();
}
return returnList.toArray(new String[0]);
}
private List<ConfigurationValueProvider> registerValueProviders(String[] args) throws IOException {
final LiquibaseConfiguration liquibaseConfiguration = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class);
List<ConfigurationValueProvider> returnList = new ArrayList<>();
final CommandLineArgumentValueProvider argumentProvider = new CommandLineArgumentValueProvider(commandLine.parseArgs(args));
liquibaseConfiguration.registerProvider(argumentProvider);
returnList.add(argumentProvider);
final ConfiguredValue<String> defaultsFileConfig = LiquibaseCommandLineConfiguration.DEFAULTS_FILE.getCurrentConfiguredValue();
/*
* The installed licenses are cleared from the license service. Clearing the licenses
* forces the license service to reinstall the licenses.
*
* The call to LiquibaseCommandLineConfiguration.DEFAULTS_FILE.getCurrentConfiguredValue() above will check
* the environment variables for a value, and that requires a valid license. Thus, if the user has a license
* key specified in both an environment variable and in their defaults file (using different property names),
* the value in the defaults file will not be seen, despite it possibly being more preferred than the value
* in the environment variable, because the DefaultsFileValueProvider hasn't been registered yet.
*/
LicenseServiceFactory licenseServiceFactory = Scope.getCurrentScope().getSingleton(LicenseServiceFactory.class);
if (licenseServiceFactory != null) {
LicenseService licenseService = licenseServiceFactory.getLicenseService();
if (licenseService != null) {
licenseService.reset();
}
}
final File defaultsFile = new File(defaultsFileConfig.getValue());
if (defaultsFile.exists()) {
final DefaultsFileValueProvider fileProvider = new DefaultsFileValueProvider(defaultsFile);
liquibaseConfiguration.registerProvider(fileProvider);
returnList.add(fileProvider);
} else {
final InputStream defaultsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(defaultsFileConfig.getValue());
if (defaultsStream == null) {
Scope.getCurrentScope().getLog(getClass()).fine("Cannot find defaultsFile " + defaultsFile.getAbsolutePath());
if (!defaultsFileConfig.wasDefaultValueUsed()) {
//can't use UI since it's not configured correctly yet
System.err.println("Could not find defaults file " + defaultsFileConfig.getValue());
}
} else {
final DefaultsFileValueProvider fileProvider = new DefaultsFileValueProvider(defaultsStream, "File in classpath " + defaultsFileConfig.getValue());
liquibaseConfiguration.registerProvider(fileProvider);
returnList.add(fileProvider);
}
}
File localDefaultsFile = new File(defaultsFile.getAbsolutePath().replaceFirst(".properties$", ".local.properties"));
if (localDefaultsFile.exists()) {
final DefaultsFileValueProvider fileProvider = new DefaultsFileValueProvider(localDefaultsFile) {
@Override
public int getPrecedence() {
return super.getPrecedence() + 1;
}
};
liquibaseConfiguration.registerProvider(fileProvider);
returnList.add(fileProvider);
} else {
Scope.getCurrentScope().getLog(getClass()).fine("Cannot find local defaultsFile " + defaultsFile.getAbsolutePath());
}
return returnList;
}
/**
* Configures the system, and returns values to add to Scope.
*
* @return values to set in the scope
*/
private Map<String, Object> configureScope() throws Exception {
Map<String, Object> returnMap = new HashMap<>();
final ClassLoader classLoader = configureClassLoader();
returnMap.putAll(configureLogging());
returnMap.putAll(configureResourceAccessor(classLoader));
ConsoleUIService ui = null;
List<UIService> uiServices = Scope.getCurrentScope().getServiceLocator().findInstances(UIService.class);
for (UIService uiService : uiServices) {
if (uiService instanceof ConsoleUIService) {
ui = (ConsoleUIService) uiService;
break;
}
}
if (ui == null) {
ui = new ConsoleUIService();
}
ui.setAllowPrompt(true);
ui.setOutputStream(System.err);
returnMap.put(Scope.Attr.ui.name(), ui);
returnMap.put(LiquibaseCommandLineConfiguration.ARGUMENT_CONVERTER.getKey(),
(LiquibaseCommandLineConfiguration.ArgumentConverter) argument -> "--" + StringUtil.toKabobCase(argument));
return returnMap;
}
private void configureVersionInfo() {
getRootCommand(this.commandLine).getCommandSpec().versionProvider(new LiquibaseVersionProvider());
}
protected Map<String, Object> configureLogging() throws IOException {
Map<String, Object> returnMap = new HashMap<>();
final ConfiguredValue<Level> currentConfiguredValue = LiquibaseCommandLineConfiguration.LOG_LEVEL.getCurrentConfiguredValue();
final File logFile = LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentValue();
Level logLevel = Level.OFF;
if (!currentConfiguredValue.wasDefaultValueUsed()) {
logLevel = currentConfiguredValue.getValue();
}
configureLogging(logLevel, logFile);
//
// Set the Liquibase Hub log level if logging is not OFF
//
if (logLevel != Level.OFF) {
returnMap.put(HubConfiguration.LIQUIBASE_HUB_LOGLEVEL.getKey(), logLevel);
}
return returnMap;
}
private void configureLogging(Level logLevel, File logFile) throws IOException {
configuredLogLevel = logLevel;
final JavaLogService logService = (JavaLogService) Scope.getCurrentScope().get(Scope.Attr.logService, LogService.class);
java.util.logging.Logger liquibaseLogger = java.util.logging.Logger.getLogger("liquibase");
logService.setParent(liquibaseLogger);
System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] %4$s [%2$s] %5$s%6$s%n");
java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
Level cliLogLevel = logLevel;
if (logFile != null) {
if (fileHandler == null) {
fileHandler = new FileHandler(logFile.getAbsolutePath(), true);
fileHandler.setFormatter(new SimpleFormatter());
rootLogger.addHandler(fileHandler);
}
fileHandler.setLevel(logLevel);
if (logLevel == Level.OFF) {
fileHandler.setLevel(Level.FINE);
}
cliLogLevel = Level.OFF;
}
final String configuredChannels = LiquibaseCommandLineConfiguration.LOG_CHANNELS.getCurrentValue();
List<String> channels;
if (configuredChannels.equalsIgnoreCase("all")) {
channels = new ArrayList<>(Arrays.asList("", "liquibase"));
} else {
channels = StringUtil.splitAndTrim(configuredChannels, ",");
if (logLevel == Level.OFF) {
channels.add("");
}
}
for (String channel : channels) {
if (channel.equalsIgnoreCase("all")) {
channel = "";
}
java.util.logging.Logger.getLogger(channel).setLevel(logLevel);
}
for (Handler handler : rootLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
handler.setLevel(cliLogLevel);
}
}
}
private CommandLine getRootCommand(CommandLine commandLine) {
while (commandLine.getParent() != null) {
commandLine = commandLine.getParent();
}
return commandLine;
}
private Map<String, Object> configureResourceAccessor(ClassLoader classLoader) {
Map<String, Object> returnMap = new HashMap<>();
returnMap.put(Scope.Attr.resourceAccessor.name(), new SearchPathResourceAccessor(
new FileSystemResourceAccessor(Paths.get(".").toAbsolutePath().toFile()),
new CommandLineResourceAccessor(classLoader)));
return returnMap;
}
protected ClassLoader configureClassLoader() throws IllegalArgumentException {
final String classpath = LiquibaseCommandLineConfiguration.CLASSPATH.getCurrentValue();
final List<URL> urls = new ArrayList<>();
if (classpath != null) {
String[] classpathSoFar;
if (isWindows()) {
classpathSoFar = classpath.split(";");
} else {
classpathSoFar = classpath.split(":");
}
for (String classpathEntry : classpathSoFar) {
File classPathFile = new File(classpathEntry);
if (!classPathFile.exists()) {
throw new IllegalArgumentException(classPathFile.getAbsolutePath() + " does not exist");
}
try {
URL newUrl = new File(classpathEntry).toURI().toURL();
Scope.getCurrentScope().getLog(getClass()).fine(newUrl.toExternalForm() + " added to class loader");
urls.add(newUrl);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
}
final ClassLoader classLoader;
if (LiquibaseCommandLineConfiguration.INCLUDE_SYSTEM_CLASSPATH.getCurrentValue()) {
classLoader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(urls.toArray(new URL[0]), Thread.currentThread()
.getContextClassLoader()));
} else {
classLoader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(urls.toArray(new URL[0]), null));
}
Thread.currentThread().setContextClassLoader(classLoader);
return classLoader;
}
private void addSubcommand(CommandDefinition commandDefinition, CommandLine rootCommand) {
List<String[]> commandNames = expandCommandNames(commandDefinition);
boolean showCommand = true;
for (String[] commandName : commandNames) {
final CommandRunner commandRunner = new CommandRunner();
final CommandLine.Model.CommandSpec subCommandSpec = CommandLine.Model.CommandSpec.wrapWithoutInspection(commandRunner, defaultFactory);
commandRunner.setSpec(subCommandSpec);
configureHelp(subCommandSpec, false);
//
// Add to the usageMessage footer if the CommandDefinition has a footer
//
if (commandDefinition.getHelpFooter() != null) {
String[] usageMessageFooter = subCommandSpec.usageMessage().footer();
List<String> list = new ArrayList<>(Arrays.asList(usageMessageFooter));
list.add(commandDefinition.getHelpFooter());
subCommandSpec.usageMessage().footer(list.toArray(new String[0]));
}
String shortDescription = commandDefinition.getShortDescription();
String displayDescription = shortDescription;
String legacyCommand = commandName[commandName.length - 1];
String camelCaseCommand = StringUtil.toCamelCase(legacyCommand);
if (!legacyCommand.equals(camelCaseCommand)) {
displayDescription = "\n" + shortDescription + "\n[deprecated: " + camelCaseCommand + "]";
}
subCommandSpec.usageMessage()
.header(StringUtil.trimToEmpty(displayDescription) + "\n")
.description(StringUtil.trimToEmpty(commandDefinition.getLongDescription()));
subCommandSpec.optionsCaseInsensitive(true);
subCommandSpec.subcommandsCaseInsensitive(true);
if (!showCommand) {
subCommandSpec.usageMessage().hidden(true);
} else {
subCommandSpec.usageMessage().hidden(commandDefinition.getHidden());
}
showCommand = false;
for (CommandArgumentDefinition<?> def : commandDefinition.getArguments().values()) {
final String[] argNames = toArgNames(def);
for (int i = 0; i < argNames.length; i++) {
final CommandLine.Model.OptionSpec.Builder builder = createArgBuilder(def, argNames[i]);
String argDisplaySuffix = "";
String argName = argNames[i];
String camelCaseArg = StringUtil.toCamelCase(argName.substring(2));
if (!argName.equals("--" + camelCaseArg)) {
argDisplaySuffix = "\n[deprecated: --" + camelCaseArg + "]";
}
//
// Determine if this is a group command and set the property/environment display strings accordingly
//
String description;
if (commandDefinition.getName().length > 1) {
String propertyStringToPresent = "\n(liquibase.command." +
StringUtil.join(commandDefinition.getName(), ".") + "." + def.getName() + ")";
String envStringToPresent =
toEnvVariable("\n(liquibase.command." + StringUtil.join(commandDefinition.getName(), ".") +
"." + def.getName()) + ")" + argDisplaySuffix;
description = propertyStringToPresent + envStringToPresent;
} else {
description =
"\n(liquibase.command." + def.getName() + " OR liquibase.command." +
StringUtil.join(commandDefinition.getName(), ".") + "." + def.getName() + ")\n" +
"(" + toEnvVariable("liquibase.command." + def.getName()) + " OR " +
toEnvVariable("liquibase.command." + StringUtil.join(commandDefinition.getName(), ".") +
"." + def.getName()) + ")" + argDisplaySuffix;
}
if (def.getDefaultValue() != null) {
if (def.getDefaultValueDescription() == null) {
description = "\nDEFAULT: " + def.getDefaultValue() + "\n" + description;
} else {
description = "\nDEFAULT: " + def.getDefaultValueDescription() + "\n" + description;
}
}
if (def.getDescription() != null) {
description = def.getDescription() + description;
}
if (def.isRequired()) {
description = "[REQUIRED] " + description;
}
builder.description(description + "\n");
if (def.getDataType().equals(Boolean.class)) {
builder.arity("0..1");
}
if (i > 0) {
builder.hidden(true);
} else {
builder.hidden(def.getHidden());
}
subCommandSpec.addOption(builder.build());
if (argName.equals("--changelog-file")) {
final CommandLine.Model.OptionSpec.Builder paramBuilder = (CommandLine.Model.OptionSpec.Builder) CommandLine.Model.OptionSpec.builder("-D")
.required(false)
.type(HashMap.class)
.description("Pass a name/value pair for substitution in the changelog(s)\nPass as -D<property.name>=<property.value>\n[deprecated: set changelog properties in defaults file or environment variables]")
.mapFallbackValue("");
subCommandSpec.add(paramBuilder.build());
}
}
}
for (String legacyArg : legacyNoLongerCommandArguments) {
final CommandLine.Model.OptionSpec.Builder builder = CommandLine.Model.OptionSpec.builder("--" + legacyArg)
.required(false)
.type(String.class)
.description("Legacy CLI argument")
.hidden(true);
subCommandSpec.addOption(builder.build());
String kabobArg = StringUtil.toKabobCase(legacyArg);
if (!kabobArg.equals(legacyArg)) {
final CommandLine.Model.OptionSpec.Builder kabobOptionBuilder =
CommandLine.Model.OptionSpec.builder("--" + kabobArg)
.required(false)
.type(String.class)
.hidden(true)
.description("Legacy CLI argument");
subCommandSpec.addOption(kabobOptionBuilder.build());
}
}
getParentCommandSpec(commandDefinition, rootCommand).addSubcommand(commandName[commandName.length - 1], new CommandLine(subCommandSpec, defaultFactory));
}
}
private CommandLine.Model.OptionSpec.Builder createArgBuilder(CommandArgumentDefinition<?> def, String argName) {
return CommandLine.Model.OptionSpec.builder(argName)
.required(false)
.converters(value -> {
if (def.getDataType().equals(Boolean.class)) {
if (value.equals("")) {
return "true";
}
}
return value;
})
.type(String.class);
}
private List<String[]> expandCommandNames(CommandDefinition commandDefinition) {
List<String[]> returnList = new ArrayList<>();
//create standard version first
final String[] standardName = commandDefinition.getName().clone();
for (int i = 0; i < standardName.length; i++) {
standardName[i] = StringUtil.toKabobCase(commandDefinition.getName()[i]);
}
returnList.add(standardName);
if (!StringUtil.join(standardName, " ").equals(StringUtil.join(commandDefinition.getName(), " "))) {
returnList.add(commandDefinition.getName());
}
return returnList;
}
private CommandLine.Model.CommandSpec getParentCommandSpec(CommandDefinition commandDefinition, CommandLine rootCommand) {
final String[] commandName = commandDefinition.getName();
CommandLine.Model.CommandSpec parent = rootCommand.getCommandSpec();
//length-1 to not include the actual command name
for (int i = 0; i < commandName.length - 1; i++) {
final CommandLine commandGroup = parent.subcommands().get(commandName[i]);
final String[] groupName = Arrays.copyOfRange(commandName, 0, i + 1);
if (commandGroup == null) {
parent = addSubcommandGroup(groupName, commandDefinition, parent);
} else {
parent = commandGroup.getCommandSpec();
if (commandDefinition.getGroupHelpFooter() != null) {
List<String> list = new ArrayList<>();
list.add(commandDefinition.getHelpFooter());
parent.usageMessage().footer(list.toArray(new String[0]));
}
}
configureSubcommandGroup(parent, groupName, commandDefinition);
}
return parent;
}
private void configureSubcommandGroup(CommandLine.Model.CommandSpec groupSpec, String[] groupName, CommandDefinition commandDefinition) {
final String header = StringUtil.trimToEmpty(commandDefinition.getGroupShortDescription(groupName));
final String description = StringUtil.trimToEmpty(commandDefinition.getGroupLongDescription(groupName));
if (!header.equals("")) {
groupSpec.usageMessage().header("< " + header + " >\n");
}
if (!description.equals("")) {
groupSpec.usageMessage().description(description + "\n");
}
}
private CommandLine.Model.CommandSpec addSubcommandGroup(String[] groupName, CommandDefinition commandDefinition, CommandLine.Model.CommandSpec parent) {
final CommandLine.Model.CommandSpec groupSpec = CommandLine.Model.CommandSpec.wrapWithoutInspection(null, defaultFactory);
configureHelp(groupSpec, false);
if (commandDefinition.getHelpFooter() != null) {
String[] usageMessageFooter = groupSpec.usageMessage().footer();
List<String> list = new ArrayList<>(Arrays.asList(usageMessageFooter));
list.add(commandDefinition.getHelpFooter());
groupSpec.usageMessage().footer(list.toArray(new String[0]));
}
groupSpec.optionsCaseInsensitive(true);
groupSpec.subcommandsCaseInsensitive(true);
parent.addSubcommand(groupName[groupName.length - 1], groupSpec);
return groupSpec;
}
private String toEnvVariable(String property) {
return StringUtil.toKabobCase(property).replace(".", "_").replace("-", "_").toUpperCase();
}
private SortedSet<CommandDefinition> getCommands() {
final CommandFactory commandFactory = Scope.getCurrentScope().getSingleton(CommandFactory.class);
return commandFactory.getCommands(false);
}
private void addGlobalArguments(CommandLine commandLine) {
final CommandLine.Model.CommandSpec rootCommandSpec = commandLine.getCommandSpec();
final SortedSet<ConfigurationDefinition<?>> globalConfigurations = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getRegisteredDefinitions(false);
for (ConfigurationDefinition<?> def : globalConfigurations) {
final String[] argNames = toArgNames(def);
for (int i = 0; i < argNames.length; i++) {
final CommandLine.Model.OptionSpec.Builder optionBuilder = CommandLine.Model.OptionSpec.builder(argNames[i])
.required(false)
.type(String.class);
String description = "(" + def.getKey() + ")\n"
+ "(" + toEnvVariable(def.getKey()) + ")";
if (def.getDefaultValue() != null) {
if (def.getDefaultValueDescription() == null) {
description = "DEFAULT: " + def.getDefaultValue() + "\n" + description;
} else {
description = "DEFAULT: " + def.getDefaultValueDescription() + "\n" + description;
}
}
if (def.getDescription() != null) {
description = def.getDescription() + "\n" + description;
}
if (i == 0) {
String primaryArg = argNames[i];
String camelCaseArg = StringUtil.toCamelCase(primaryArg.substring(2));
if (!primaryArg.equals("--" + camelCaseArg)) {
description = "\n" + description +
"\n[deprecated: --" + camelCaseArg + "]";
}
}
optionBuilder.description(description + "\n");
if (def.getDataType().equals(Boolean.class)) {
optionBuilder.arity("1");
}