1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Fri Dec 27 16:54:10 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Fri Dec 27 17:40:27 2013 +0100
1.3 @@ -23,12 +23,17 @@
1.4 import info.globalcode.sql.dk.configuration.ConfigurationException;
1.5 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
1.6 import info.globalcode.sql.dk.configuration.FormatterDefinition;
1.7 +import info.globalcode.sql.dk.configuration.NameIdentified;
1.8 import info.globalcode.sql.dk.formatting.Formatter;
1.9 import info.globalcode.sql.dk.formatting.FormatterContext;
1.10 import info.globalcode.sql.dk.formatting.FormatterException;
1.11 +import java.io.File;
1.12 +import java.io.FileNotFoundException;
1.13 import java.io.IOException;
1.14 import java.io.PrintStream;
1.15 +import java.io.PrintWriter;
1.16 import java.sql.SQLException;
1.17 +import java.util.Collection;
1.18 import java.util.logging.Level;
1.19 import java.util.logging.LogRecord;
1.20 import java.util.logging.Logger;
1.21 @@ -118,6 +123,8 @@
1.22 log.log(Level.SEVERE, "Unsupported mode: {0}", mode);
1.23 break;
1.24 }
1.25 +
1.26 + generateBashCompletion();
1.27 }
1.28
1.29 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException {
1.30 @@ -168,4 +175,33 @@
1.31 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e);
1.32 }
1.33 }
1.34 +
1.35 + private void generateBashCompletion() {
1.36 + if (configuration == null) {
1.37 + log.log(Level.FINER, "Not writing Bash completion helper files. In order to generate these files please run some command which requires configuration.");
1.38 + } else {
1.39 + try {
1.40 + File dir = new File(Constants.DIR, "bash-completion");
1.41 + dir.mkdir();
1.42 + writeBashCompletionHelperFile(configuration.getDatabases(), new File(dir, "databases"));
1.43 + writeBashCompletionHelperFile(configuration.getAllFormatters(), new File(dir, "formatters"));
1.44 + } catch (Exception e) {
1.45 + log.log(Level.WARNING, "Unable to generate Bash completion helper files", e);
1.46 + }
1.47 + }
1.48 + }
1.49 +
1.50 + private void writeBashCompletionHelperFile(Collection<? extends NameIdentified> items, File target) throws FileNotFoundException {
1.51 + if (Constants.CONFIG_FILE.lastModified() > target.lastModified()) {
1.52 + try (PrintWriter fw = new PrintWriter(target)) {
1.53 + for (NameIdentified dd : items) {
1.54 + fw.println(dd.getName());
1.55 + }
1.56 + fw.close();
1.57 + log.log(Level.FINE, "Bash completion helper file was written: {0}", target);
1.58 + }
1.59 + } else {
1.60 + log.log(Level.FINER, "Not writing Bash completion helper file: {0} because configuration {1} has not been changed", new Object[]{target, Constants.CONFIG_FILE});
1.61 + }
1.62 + }
1.63 }