# HG changeset patch # User František Kučera # Date 1388162427 -3600 # Node ID c4635ab3a7af6cdbe993c3f3403235da0ec1d7e6 # Parent e19a13ed19a97870ef1be7cecf754d325f396cbb bash completion: generate helper files with databases and formatters from configuration diff -r e19a13ed19a9 -r c4635ab3a7af java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Fri Dec 27 16:54:10 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Fri Dec 27 17:40:27 2013 +0100 @@ -23,12 +23,17 @@ import info.globalcode.sql.dk.configuration.ConfigurationException; import info.globalcode.sql.dk.configuration.DatabaseDefinition; import info.globalcode.sql.dk.configuration.FormatterDefinition; +import info.globalcode.sql.dk.configuration.NameIdentified; import info.globalcode.sql.dk.formatting.Formatter; import info.globalcode.sql.dk.formatting.FormatterContext; import info.globalcode.sql.dk.formatting.FormatterException; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; +import java.io.PrintWriter; import java.sql.SQLException; +import java.util.Collection; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; @@ -118,6 +123,8 @@ log.log(Level.SEVERE, "Unsupported mode: {0}", mode); break; } + + generateBashCompletion(); } private void processQueryNow() throws ConfigurationException, SQLException, FormatterException { @@ -168,4 +175,33 @@ throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e); } } + + private void generateBashCompletion() { + if (configuration == null) { + log.log(Level.FINER, "Not writing Bash completion helper files. In order to generate these files please run some command which requires configuration."); + } else { + try { + File dir = new File(Constants.DIR, "bash-completion"); + dir.mkdir(); + writeBashCompletionHelperFile(configuration.getDatabases(), new File(dir, "databases")); + writeBashCompletionHelperFile(configuration.getAllFormatters(), new File(dir, "formatters")); + } catch (Exception e) { + log.log(Level.WARNING, "Unable to generate Bash completion helper files", e); + } + } + } + + private void writeBashCompletionHelperFile(Collection items, File target) throws FileNotFoundException { + if (Constants.CONFIG_FILE.lastModified() > target.lastModified()) { + try (PrintWriter fw = new PrintWriter(target)) { + for (NameIdentified dd : items) { + fw.println(dd.getName()); + } + fw.close(); + log.log(Level.FINE, "Bash completion helper file was written: {0}", target); + } + } else { + log.log(Level.FINER, "Not writing Bash completion helper file: {0} because configuration {1} has not been changed", new Object[]{target, Constants.CONFIG_FILE}); + } + } } diff -r e19a13ed19a9 -r c4635ab3a7af java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java Fri Dec 27 16:54:10 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java Fri Dec 27 17:40:27 2013 +0100 @@ -80,6 +80,11 @@ } } + /** + * @return only configured formatters + * @see #getBuildInFormatters() + * @see #getAllFormatters() + */ @XmlElement(name = "formatter", namespace = CONFIGURATION) public List getFormatters() { return formatters; @@ -110,12 +115,29 @@ } } + /** + * @return only built-in formatters + * @see #getAllFormatters() + * @see #getFormatters() + */ @XmlTransient public Collection getBuildInFormatters() { return buildInFormatters; } /** + * @return built-in + configured formatters + * @see #getFormatters() + */ + @XmlTransient + public Collection getAllFormatters() { + Collection allFormatters = new ArrayList<>(); + allFormatters.addAll(buildInFormatters); + allFormatters.addAll(formatters); + return allFormatters; + } + + /** * @return name of default formatter, is used if name is not specified on CLI */ @XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)