options: --skip-errors and --skip-errors-silently + exit codes
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 23:08:26 +0100
changeset 18a5a36526ff71
parent 17 505031965440
child 19 cda209705642
options: --skip-errors and --skip-errors-silently + exit codes
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java
scripts/copy-image-resizer.sh
     1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 21:59:20 2014 +0100
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 23:08:26 2014 +0100
     1.3 @@ -126,7 +126,9 @@
     1.4  			if (entry.isDirectory()) {
     1.5  				resizeDirectory(entry);
     1.6  			} else {
     1.7 -				if (options.isSkipErrors()) {
     1.8 +				if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.FAIL_EARLY) {
     1.9 +					resizeFile(entry);
    1.10 +				} else {
    1.11  					try {
    1.12  						resizeFile(entry);
    1.13  					} catch (Exception e) {
    1.14 @@ -136,8 +138,6 @@
    1.15  						record.setThrown(e);
    1.16  						log.log(record);
    1.17  					}
    1.18 -				} else {
    1.19 -					resizeFile(entry);
    1.20  				}
    1.21  
    1.22  			}
     2.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java	Mon Nov 17 21:59:20 2014 +0100
     2.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java	Mon Nov 17 23:08:26 2014 +0100
     2.3 @@ -42,7 +42,7 @@
     2.4  	/**
     2.5  	 * Whether errors (while resizing/copying particular images) should be just logged and skipped.
     2.6  	 */
     2.7 -	private boolean skipErrors;
     2.8 +	private ERROR_MODE errorMode;
     2.9  
    2.10  	public File getInput() {
    2.11  		return input;
    2.12 @@ -68,12 +68,12 @@
    2.13  		sizes.add(size);
    2.14  	}
    2.15  
    2.16 -	public boolean isSkipErrors() {
    2.17 -		return skipErrors;
    2.18 +	public ERROR_MODE getErrorMode() {
    2.19 +		return errorMode;
    2.20  	}
    2.21  
    2.22 -	public void setSkipErrors(boolean skipErrors) {
    2.23 -		this.skipErrors = skipErrors;
    2.24 +	public void setErrorMode(ERROR_MODE errorMode) {
    2.25 +		this.errorMode = errorMode;
    2.26  	}
    2.27  
    2.28  	public void validate() throws InvalidOptionsException {
    2.29 @@ -97,4 +97,20 @@
    2.30  			throw e;
    2.31  		}
    2.32  	}
    2.33 +
    2.34 +	public static enum ERROR_MODE {
    2.35 +
    2.36 +		/**
    2.37 +		 * fail on first error
    2.38 +		 */
    2.39 +		FAIL_EARLY,
    2.40 +		/**
    2.41 +		 * just log errors, skip them and fail at the end
    2.42 +		 */
    2.43 +		FAIL_LATER,
    2.44 +		/**
    2.45 +		 * just log errors, skip them and report success
    2.46 +		 */
    2.47 +		SILENT_SKIP
    2.48 +	}
    2.49  }
     3.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java	Mon Nov 17 21:59:20 2014 +0100
     3.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java	Mon Nov 17 23:08:26 2014 +0100
     3.3 @@ -119,7 +119,16 @@
     3.4  
     3.5  					@Override
     3.6  					public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
     3.7 -						options.setSkipErrors(true);
     3.8 +						options.setErrorMode(RecursiveOptions.ERROR_MODE.FAIL_LATER);
     3.9 +						return 0;
    3.10 +					}
    3.11 +
    3.12 +				},
    3.13 +		SKIP_ERRORS_SILENTLY("--skip-errors-silently") { // bash-completion: option
    3.14 +
    3.15 +					@Override
    3.16 +					public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
    3.17 +						options.setErrorMode(RecursiveOptions.ERROR_MODE.SILENT_SKIP);
    3.18  						return 0;
    3.19  					}
    3.20  
     4.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java	Mon Nov 17 21:59:20 2014 +0100
     4.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIStarter.java	Mon Nov 17 23:08:26 2014 +0100
     4.3 @@ -35,17 +35,38 @@
     4.4  
     4.5  	private static final Logger log = Logger.getLogger(CLIStarter.class.getName());
     4.6  
     4.7 +	// help:exit-codes
     4.8 +	public static final int EXIT_SUCCESS = 0; // doc:success
     4.9 +	public static final int EXIT_UNEXPECTED_ERROR = 1; // doc:unexpected error (probably bug)
    4.10 +	// 2 is reserved: http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
    4.11 +	//public static final int EXIT_SQL_ERROR = 3; // ___:SQL error
    4.12 +	public static final int EXIT_CLI_PARSE_ERROR = 4; // doc:CLI options parse error
    4.13 +	public static final int EXIT_CLI_VALIDATE_ERROR = 5; // doc:CLI options validation error
    4.14 +	//public static final int EXIT_CONFIGURATION_ERROR = 6; // ___:configuration error
    4.15 +	public static final int EXIT_IMAGE_ERROR = 7; // doc:image error
    4.16 +	public static final int EXIT_RECURSIVE_ERROR = 8; // doc:recursive error
    4.17 +
    4.18  	public static void main(String[] args) {
    4.19  
    4.20 +		int exitCode;
    4.21 +
    4.22  		try {
    4.23  			CLIParser parser = new CLIParser();
    4.24  			RecursiveOptions options = parser.parseOptions(args);
    4.25  			options.validate();
    4.26  			RecursiveImageResizer resizer = new RecursiveImageResizer(options);
    4.27  			Counters counters = resizer.resize();
    4.28 -			log.log(Level.INFO, "Recursive copy/resize finished. {0}", counters);
    4.29 +
    4.30 +			if (options.getErrorMode() == RecursiveOptions.ERROR_MODE.SILENT_SKIP || counters.get(Counters.COUNTER_TYPE.SKIPPED_ERROR) == 0) {
    4.31 +				log.log(Level.INFO, "Recursive copy/resize succesfully finished. {0}", counters);
    4.32 +				exitCode = EXIT_SUCCESS;
    4.33 +			} else {
    4.34 +				log.log(Level.SEVERE, "Recursive copy/resize finished with errors. {0}", counters);
    4.35 +				exitCode = EXIT_IMAGE_ERROR;
    4.36 +			}
    4.37  		} catch (CLIParserException e) {
    4.38  			log.log(Level.SEVERE, "Unable to parse CLI options", e);
    4.39 +			exitCode = EXIT_CLI_PARSE_ERROR;
    4.40  		} catch (InvalidOptionsException e) {
    4.41  			log.log(Level.SEVERE, "Invalid CLI options", e);
    4.42  			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
    4.43 @@ -54,11 +75,16 @@
    4.44  				r.setParameters(new Object[]{p.getDescription()});
    4.45  				log.log(r);
    4.46  			}
    4.47 +			exitCode = EXIT_CLI_VALIDATE_ERROR;
    4.48  		} catch (RecursiveException e) {
    4.49  			log.log(Level.SEVERE, "Error while processing filesystem hierarchy", e);
    4.50 +			exitCode = EXIT_RECURSIVE_ERROR;
    4.51  		} catch (ResizeException e) {
    4.52  			log.log(Level.SEVERE, "Error while resizing image", e);
    4.53 +			exitCode = EXIT_IMAGE_ERROR;
    4.54  		}
    4.55 +
    4.56 +		System.exit(exitCode);
    4.57  	}
    4.58  
    4.59  }
     5.1 --- a/scripts/copy-image-resizer.sh	Mon Nov 17 21:59:20 2014 +0100
     5.2 +++ b/scripts/copy-image-resizer.sh	Mon Nov 17 23:08:26 2014 +0100
     5.3 @@ -29,8 +29,8 @@
     5.4  MAIN_CLASS="cz.frantovo.copyImageResizer.cli.CLIStarter";
     5.5  
     5.6  # Customize logger output:
     5.7 +LOGGER_INITIALIZER_CLASS="cz.frantovo.copyImageResizer.logging.SimpleLoggerInitializer";  # simple log for redirecting STDOUT to a file (machine readable)
     5.8  LOGGER_INITIALIZER_CLASS="cz.frantovo.copyImageResizer.logging.ConsoleLoggerInitializer"; # colorful log for interactive work (human readable)
     5.9 -LOGGER_INITIALIZER_CLASS="cz.frantovo.copyImageResizer.logging.SimpleLoggerInitializer";  # simple log for redirecting STDOUT to a file (machine readable)
    5.10  
    5.11  LOGGER="-Djava.util.logging.config.class=$LOGGER_INITIALIZER_CLASS";
    5.12