21 import info.globalcode.sql.dk.CLIOptions.MODE; |
21 import info.globalcode.sql.dk.CLIOptions.MODE; |
22 import info.globalcode.sql.dk.configuration.Configuration; |
22 import info.globalcode.sql.dk.configuration.Configuration; |
23 import info.globalcode.sql.dk.configuration.ConfigurationException; |
23 import info.globalcode.sql.dk.configuration.ConfigurationException; |
24 import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
24 import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
25 import info.globalcode.sql.dk.configuration.FormatterDefinition; |
25 import info.globalcode.sql.dk.configuration.FormatterDefinition; |
|
26 import info.globalcode.sql.dk.configuration.NameIdentified; |
26 import info.globalcode.sql.dk.formatting.Formatter; |
27 import info.globalcode.sql.dk.formatting.Formatter; |
27 import info.globalcode.sql.dk.formatting.FormatterContext; |
28 import info.globalcode.sql.dk.formatting.FormatterContext; |
28 import info.globalcode.sql.dk.formatting.FormatterException; |
29 import info.globalcode.sql.dk.formatting.FormatterException; |
|
30 import java.io.File; |
|
31 import java.io.FileNotFoundException; |
29 import java.io.IOException; |
32 import java.io.IOException; |
30 import java.io.PrintStream; |
33 import java.io.PrintStream; |
|
34 import java.io.PrintWriter; |
31 import java.sql.SQLException; |
35 import java.sql.SQLException; |
|
36 import java.util.Collection; |
32 import java.util.logging.Level; |
37 import java.util.logging.Level; |
33 import java.util.logging.LogRecord; |
38 import java.util.logging.LogRecord; |
34 import java.util.logging.Logger; |
39 import java.util.logging.Logger; |
35 import javax.xml.bind.JAXBContext; |
40 import javax.xml.bind.JAXBContext; |
36 import javax.xml.bind.Unmarshaller; |
41 import javax.xml.bind.Unmarshaller; |
116 break; |
121 break; |
117 default: |
122 default: |
118 log.log(Level.SEVERE, "Unsupported mode: {0}", mode); |
123 log.log(Level.SEVERE, "Unsupported mode: {0}", mode); |
119 break; |
124 break; |
120 } |
125 } |
|
126 |
|
127 generateBashCompletion(); |
121 } |
128 } |
122 |
129 |
123 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException { |
130 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException { |
124 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName()); |
131 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName()); |
125 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName()); |
132 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName()); |
166 return (Configuration) u.unmarshal(Constants.CONFIG_FILE); |
173 return (Configuration) u.unmarshal(Constants.CONFIG_FILE); |
167 } catch (Exception e) { |
174 } catch (Exception e) { |
168 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e); |
175 throw new ConfigurationException("Unable to load configuration from " + Constants.CONFIG_FILE, e); |
169 } |
176 } |
170 } |
177 } |
|
178 |
|
179 private void generateBashCompletion() { |
|
180 if (configuration == null) { |
|
181 log.log(Level.FINER, "Not writing Bash completion helper files. In order to generate these files please run some command which requires configuration."); |
|
182 } else { |
|
183 try { |
|
184 File dir = new File(Constants.DIR, "bash-completion"); |
|
185 dir.mkdir(); |
|
186 writeBashCompletionHelperFile(configuration.getDatabases(), new File(dir, "databases")); |
|
187 writeBashCompletionHelperFile(configuration.getAllFormatters(), new File(dir, "formatters")); |
|
188 } catch (Exception e) { |
|
189 log.log(Level.WARNING, "Unable to generate Bash completion helper files", e); |
|
190 } |
|
191 } |
|
192 } |
|
193 |
|
194 private void writeBashCompletionHelperFile(Collection<? extends NameIdentified> items, File target) throws FileNotFoundException { |
|
195 if (Constants.CONFIG_FILE.lastModified() > target.lastModified()) { |
|
196 try (PrintWriter fw = new PrintWriter(target)) { |
|
197 for (NameIdentified dd : items) { |
|
198 fw.println(dd.getName()); |
|
199 } |
|
200 fw.close(); |
|
201 log.log(Level.FINE, "Bash completion helper file was written: {0}", target); |
|
202 } |
|
203 } else { |
|
204 log.log(Level.FINER, "Not writing Bash completion helper file: {0} because configuration {1} has not been changed", new Object[]{target, Constants.CONFIG_FILE}); |
|
205 } |
|
206 } |
171 } |
207 } |