bash completion: generate helper files with databases and formatters from configuration v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 27 Dec 2013 17:40:27 +0100
branchv_0
changeset 80c4635ab3a7af
parent 79 e19a13ed19a9
child 81 847c83288d01
bash completion: generate helper files with databases and formatters from configuration
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java
     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  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Fri Dec 27 16:54:10 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Fri Dec 27 17:40:27 2013 +0100
     2.3 @@ -80,6 +80,11 @@
     2.4  		}
     2.5  	}
     2.6  
     2.7 +	/**
     2.8 +	 * @return only configured formatters
     2.9 +	 * @see #getBuildInFormatters()
    2.10 +	 * @see #getAllFormatters()
    2.11 +	 */
    2.12  	@XmlElement(name = "formatter", namespace = CONFIGURATION)
    2.13  	public List<FormatterDefinition> getFormatters() {
    2.14  		return formatters;
    2.15 @@ -110,12 +115,29 @@
    2.16  		}
    2.17  	}
    2.18  
    2.19 +	/**
    2.20 +	 * @return only built-in formatters
    2.21 +	 * @see #getAllFormatters()
    2.22 +	 * @see #getFormatters()
    2.23 +	 */
    2.24  	@XmlTransient
    2.25  	public Collection<FormatterDefinition> getBuildInFormatters() {
    2.26  		return buildInFormatters;
    2.27  	}
    2.28  
    2.29  	/**
    2.30 +	 * @return built-in + configured formatters
    2.31 +	 * @see #getFormatters()
    2.32 +	 */
    2.33 +	@XmlTransient
    2.34 +	public Collection<FormatterDefinition> getAllFormatters() {
    2.35 +		Collection<FormatterDefinition> allFormatters = new ArrayList<>();
    2.36 +		allFormatters.addAll(buildInFormatters);
    2.37 +		allFormatters.addAll(formatters);
    2.38 +		return allFormatters;
    2.39 +	}
    2.40 +
    2.41 +	/**
    2.42  	 * @return name of default formatter, is used if name is not specified on CLI
    2.43  	 */
    2.44  	@XmlElement(name = "defaultFormatter", namespace = CONFIGURATION)