# HG changeset patch # User František Kučera # Date 1416262106 -3600 # Node ID a5a36526ff7106d022ab97b7afc42cd17d4fe301 # Parent 505031965440f4c7012baf0dfb04e734805543dc options: --skip-errors and --skip-errors-silently + exit codes diff -r 505031965440 -r a5a36526ff71 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 21:59:20 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 23:08:26 2014 +0100 @@ -126,7 +126,9 @@ if (entry.isDirectory()) { resizeDirectory(entry); } else { - if (options.isSkipErrors()) { + if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.FAIL_EARLY) { + resizeFile(entry); + } else { try { resizeFile(entry); } catch (Exception e) { @@ -136,8 +138,6 @@ record.setThrown(e); log.log(record); } - } else { - resizeFile(entry); } } diff -r 505031965440 -r a5a36526ff71 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Mon Nov 17 21:59:20 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Mon Nov 17 23:08:26 2014 +0100 @@ -42,7 +42,7 @@ /** * Whether errors (while resizing/copying particular images) should be just logged and skipped. */ - private boolean skipErrors; + private ERROR_MODE errorMode; public File getInput() { return input; @@ -68,12 +68,12 @@ sizes.add(size); } - public boolean isSkipErrors() { - return skipErrors; + public ERROR_MODE getErrorMode() { + return errorMode; } - public void setSkipErrors(boolean skipErrors) { - this.skipErrors = skipErrors; + public void setErrorMode(ERROR_MODE errorMode) { + this.errorMode = errorMode; } public void validate() throws InvalidOptionsException { @@ -97,4 +97,20 @@ throw e; } } + + public static enum ERROR_MODE { + + /** + * fail on first error + */ + FAIL_EARLY, + /** + * just log errors, skip them and fail at the end + */ + FAIL_LATER, + /** + * just log errors, skip them and report success + */ + SILENT_SKIP + } } diff -r 505031965440 -r a5a36526ff71 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java Mon Nov 17 21:59:20 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java Mon Nov 17 23:08:26 2014 +0100 @@ -119,7 +119,16 @@ @Override public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException { - options.setSkipErrors(true); + options.setErrorMode(RecursiveOptions.ERROR_MODE.FAIL_LATER); + return 0; + } + + }, + SKIP_ERRORS_SILENTLY("--skip-errors-silently") { // bash-completion: option + + @Override + public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException { + options.setErrorMode(RecursiveOptions.ERROR_MODE.SILENT_SKIP); return 0; } diff -r 505031965440 -r a5a36526ff71 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java Mon Nov 17 21:59:20 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java Mon Nov 17 23:08:26 2014 +0100 @@ -35,17 +35,38 @@ private static final Logger log = Logger.getLogger(CLIStarter.class.getName()); + // help:exit-codes + public static final int EXIT_SUCCESS = 0; // doc:success + public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug) + // 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF + //public static final int EXIT_SQL_ERROR = 3; // ___:SQL error + public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error + public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error + //public static final int EXIT_CONFIGURATION_ERROR = 6; // ___:configuration error + public static final int EXIT_IMAGE_ERROR = 7; // doc:image error + public static final int EXIT_RECURSIVE_ERROR = 8; // doc:recursive error + public static void main(String[] args) { + int exitCode; + try { CLIParser parser = new CLIParser(); RecursiveOptions options = parser.parseOptions(args); options.validate(); RecursiveImageResizer resizer = new RecursiveImageResizer(options); Counters counters = resizer.resize(); - log.log(Level.INFO, "Recursive copy/resize finished. {0}", counters); + + if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.SILENT_SKIP || counters.get(Counters.COUNTER_TYPE.SKIPPED_ERROR) == 0) { + log.log(Level.INFO, "Recursive copy/resize succesfully finished. {0}", counters); + exitCode = EXIT_SUCCESS; + } else { + log.log(Level.SEVERE, "Recursive copy/resize finished with errors. {0}", counters); + exitCode = EXIT_IMAGE_ERROR; + } } catch (CLIParserException e) { log.log(Level.SEVERE, "Unable to parse CLI options", e); + exitCode = EXIT_CLI_PARSE_ERROR; } catch (InvalidOptionsException e) { log.log(Level.SEVERE, "Invalid CLI options", e); for (InvalidOptionsException.OptionProblem p : e.getProblems()) { @@ -54,11 +75,16 @@ r.setParameters(new Object[]{p.getDescription()}); log.log(r); } + exitCode = EXIT_CLI_VALIDATE_ERROR; } catch (RecursiveException e) { log.log(Level.SEVERE, "Error while processing filesystem hierarchy", e); + exitCode = EXIT_RECURSIVE_ERROR; } catch (ResizeException e) { log.log(Level.SEVERE, "Error while resizing image", e); + exitCode = EXIT_IMAGE_ERROR; } + + System.exit(exitCode); } } diff -r 505031965440 -r a5a36526ff71 scripts/copy-image-resizer.sh --- a/scripts/copy-image-resizer.sh Mon Nov 17 21:59:20 2014 +0100 +++ b/scripts/copy-image-resizer.sh Mon Nov 17 23:08:26 2014 +0100 @@ -29,8 +29,8 @@ MAIN_CLASS="cz.frantovo.copyImageResizer.cli.CLIStarter"; # Customize logger output: +LOGGER_INITIALIZER_CLASS="cz.frantovo.copyImageResizer.logging.SimpleLoggerInitializer"; # simple log for redirecting STDOUT to a file (machine readable) LOGGER_INITIALIZER_CLASS="cz.frantovo.copyImageResizer.logging.ConsoleLoggerInitializer"; # colorful log for interactive work (human readable) -LOGGER_INITIALIZER_CLASS="cz.frantovo.copyImageResizer.logging.SimpleLoggerInitializer"; # simple log for redirecting STDOUT to a file (machine readable) LOGGER="-Djava.util.logging.config.class=$LOGGER_INITIALIZER_CLASS";