4343import java .nio .file .Paths ;
4444import java .sql .SQLException ;
4545import java .util .List ;
46+ import java .util .function .Consumer ;
4647import java .util .stream .Collectors ;
4748
4849/**
@@ -66,27 +67,26 @@ public static void main(String[] args) {
6667 * @return success value
6768 */
6869 static boolean process (String [] args ) {
69- PrintWriter writer = new PrintWriter (System .out , true );
7070 CliArgs arguments = new CliArgs ();
7171 try {
72- if (!arguments .parse (writer , args )) {
72+ if (!arguments .parse (args )) {
7373 return true ;
7474 }
7575 if (arguments .isClearLibCache ()) {
7676 clearCache ();
7777 }
7878
7979 return switch (arguments .getMode ()) {
80- case INSERT -> insert (writer , arguments );
80+ case INSERT -> insert (arguments );
8181 case PARSE -> parse (arguments );
82- case GRAPH -> graph (writer , arguments );
83- case VERIFY -> verify (writer , arguments );
82+ case GRAPH -> graph (arguments );
83+ case VERIFY -> verify (arguments );
8484 default -> {
8585 if (arguments .getOldSrc () == null || arguments .getNewSrc () == null ) {
8686 // clear cache
8787 yield true ;
8888 }
89- yield diff (writer , arguments );
89+ yield diff (arguments );
9090 }
9191 };
9292 } catch (CmdLineException ex ) {
@@ -105,7 +105,7 @@ static boolean process(String[] args) {
105105 }
106106 }
107107
108- private static boolean diff (PrintWriter writer , CliArgs arguments )
108+ private static boolean diff (CliArgs arguments )
109109 throws InterruptedException , IOException , SQLException {
110110 try (PrintWriter encodedWriter = getDiffWriter (arguments )) {
111111 var diff = new PgDiffCli (arguments );
@@ -129,7 +129,7 @@ private static boolean diff(PrintWriter writer, CliArgs arguments)
129129 var logMsg = Messages .Main_log_contains_dangerous_statements .formatted (dangerStmt );
130130 LOG .warn (logMsg );
131131 String msg = Messages .Main_danger_statements .formatted (dangerStmt );
132- writer . println (msg );
132+ writeToConsole (msg );
133133 if (encodedWriter != null ) {
134134 encodedWriter .println ("-- " + msg ); //$NON-NLS-1$
135135 }
@@ -150,7 +150,7 @@ private static boolean diff(PrintWriter writer, CliArgs arguments)
150150 LOG .info (Messages .Main_log_apply_migration_script );
151151 new JdbcRunner ().runBatches (new UrlJdbcConnector (url ), parser .batch (), null );
152152 } else if (encodedWriter == null ) {
153- writer . println (text );
153+ writeToConsole (text );
154154 }
155155 }
156156
@@ -183,7 +183,7 @@ private static boolean parse(CliArgs arguments) throws IOException, InterruptedE
183183 return true ;
184184 }
185185
186- private static boolean insert (PrintWriter writer , CliArgs arguments )
186+ private static boolean insert (CliArgs arguments )
187187 throws IOException , InterruptedException , SQLException {
188188 var diff = new PgDiffCli (arguments );
189189 AbstractDatabase db ;
@@ -208,15 +208,15 @@ private static boolean insert(PrintWriter writer, CliArgs arguments)
208208 new JdbcRunner ().runBatches (new UrlJdbcConnector (url ),
209209 new ScriptParser ("CLI" , script , arguments ).batch (), null ); //$NON-NLS-1$
210210 } else if (pw == null ) {
211- writer . println (script );
211+ writeToConsole (script );
212212 }
213213 }
214214
215215 LOG .info (Messages .Main_log_succes_finish );
216216 return true ;
217217 }
218218
219- private static boolean graph (PrintWriter writer , CliArgs arguments ) throws IOException , InterruptedException {
219+ private static boolean graph (CliArgs arguments ) throws IOException , InterruptedException {
220220 var diff = new PgDiffCli (arguments );
221221 AbstractDatabase d ;
222222 try {
@@ -230,27 +230,28 @@ private static boolean graph(PrintWriter writer, CliArgs arguments) throws IOExc
230230 arguments .getGraphFilterTypes (), arguments .isGraphInvertFilter (), d , arguments .getGraphNames ());
231231
232232 try (PrintWriter pw = getDiffWriter (arguments )) {
233- var w = pw != null ? pw : writer ;
234- for (String dep : dependencies ) {
235- w .println (dep );
236- }
233+ Consumer <String > consumer = pw != null ? pw ::println : Application ::writeToConsole ;
234+ dependencies .forEach (consumer );
237235 }
238236
239237 LOG .info (Messages .Main_log_succes_finish );
240238 return true ;
241239 }
242240
243- private static boolean verify (PrintWriter writer , CliArgs arguments )
241+ private static boolean verify (CliArgs arguments )
244242 throws IOException , InterruptedException {
245243 Path path = Paths .get (arguments .getVerifyRuleSetPath ());
246244 LOG .info (Messages .Main_log_start_code_verify );
247245 List <Object > errors = TokenLoader .verify (arguments , path , arguments .getVerifySources ());
248- if (! errors .isEmpty ()) {
249- errors . forEach ( writer :: println );
250- return false ;
246+ if (errors .isEmpty ()) {
247+ LOG . info ( Messages . Main_log_finish_code_verify );
248+ return true ;
251249 }
252- LOG .info (Messages .Main_log_finish_code_verify );
253- return true ;
250+
251+ for (Object error : errors ) {
252+ writeToConsole (error .toString ());
253+ }
254+ return false ;
254255 }
255256
256257 private static void clearCache () throws IOException {
@@ -265,10 +266,14 @@ private static void printError(PgDiffCli diff) {
265266 }
266267 }
267268
269+ private static void writeToConsole (String message ) {
270+ System .out .println (message );
271+ }
272+
268273 private static void writeMessage (Object message ) {
269274 String msg = message .toString ();
270275 LOG .info (msg );
271- System . out . println (msg );
276+ writeToConsole (msg );
272277 }
273278
274279 private static void writeError (Object message ) {
0 commit comments