/** * Prompts the user for a yes or no answer to the specified question until they provide a valid * response (y/n/yes/no case insensitive) and reports the result. If yesNoPrompt is called in * non-interactive mode, an IllegalStateException is thrown. * * @param question The question to which a yes or no is expected in response. * @return <code>true</code> if the user answer yes, <code>false</code> if the user answered no. * @throws IOException if there is a problem reading from the terminal. */ protected final boolean yesNoPrompt(String question) throws IOException { return confirmationPrompt(question, YES_OR_NO_HINT, YES_PATTERN, NO_PATTERN); }
/** {@inheritDoc} */ @Override public String getUsageString() { return String.format("Usage:%n" + " fiji %s [flags...]%n", getName()); }
/** * Checks with the user whether the specified operation may proceed. * * @param format String format with a question describing the operation about to be executed. * @param arguments String format arguments. * @return whether the operation may proceed, or not. * @throws IOException on I/O error. */ protected boolean mayProceed(String format, Object...arguments) throws IOException { if (!isInteractive()) { return true; } if (yesNoPrompt(String.format(format, arguments))) { return true; } getPrintStream().println("Aborted."); return false; }
/** Prints the tool usage message. */ private void printUsage() { final PrintStream ps = getPrintStream(); ps.println(getUsageString()); ps.println("Flags:"); FlagParser.printUsage(this, ps); }
printUsage(); return SUCCESS; validateFlags(); boolean exceptionThrown = false; int returnFlag = FAILURE; setup(); returnFlag = run(nonFlagArgs); return returnFlag; } catch (Exception exn) { cleanup(); } catch (Exception nestedExn) { LOG.error("Nested error in tool cleanup(), " cleanup(); prettyPrintUserInputError(te); return FAILURE; getPrintStream().println(knie.getMessage()); getPrintStream().println("Try: fiji install --fiji=" + knie.getURI()); return FAILURE;
/** {@inheritDoc} */ @Override protected void validateFlags() throws Exception { super.validateFlags(); Preconditions.checkArgument((mFijiURIFlag != null) && !mFijiURIFlag.isEmpty(), "Specify a Fiji instance with --fiji=fiji://hbase-address/fiji-instance"); mFijiURI = FijiURI.newBuilder(mFijiURIFlag).build(); }
) throws IOException { Preconditions.checkState(mInteractiveFlag); BufferedReader reader = new BufferedReader(new InputStreamReader(getInputStream(), "UTF-8")); Boolean yesOrNo = null; try { while (yesOrNo == null) { getPrintStream().println(question); if (null != hint) { getPrintStream().println(hint);
/** {@inheritDoc} */ @Override protected void cleanup() throws IOException { mFiji.release(); mFiji = null; super.cleanup(); }
/** * Set the input stream the tool should read from. If you don't set it, * it will default to STDIN. * * @param inputStream The input stream to use. */ public void setInputStream(InputStream inputStream) { if (null == mInputStream) { mInputStream = inputStream; } else { getPrintStream().println("Inputstream is already set."); } }
/** {@inheritDoc} */ @Override protected void validateFlags() throws Exception { super.validateFlags(); Preconditions.checkArgument((mTargetURIFlag != null) && !mTargetURIFlag.isEmpty(), "Specify a target Fiji instance or table " + "with --target=fiji://hbase-adress/fiji-instance[/table]."); mTargetURI = FijiURI.newBuilder(mTargetURIFlag).build(); Preconditions.checkArgument(mFlushMeta || (mTargetURI.getTable() != null), "Specify a table with --fiji=fiji://hbase-cluster/fiji-instance/table" + " and/or specify a flush of metadata with --meta."); }
/** {@inheritDoc} */ @Override protected void cleanup() throws IOException { ResourceUtils.closeOrLog(mHBaseAdmin); super.cleanup(); }
/** * Set the output print stream the tool should write to. If you don't set it, * it will default to STDOUT. * * @param printStream The output print stream to use. */ public void setPrintStream(PrintStream printStream) { if (null == mPrintStream) { mPrintStream = printStream; } else { getPrintStream().println("Printstream is already set."); } }
/** {@inheritDoc} */ @Override protected void validateFlags() throws Exception { super.validateFlags(); Preconditions.checkArgument((mTableURIFlag != null) && !mTableURIFlag.isEmpty(), "Specify the table to create with --table=fiji://hbase-address/fiji-instance/table"); mTableURI = FijiURI.newBuilder(mTableURIFlag).build(); Preconditions.checkArgument((mLayout != null) && !mLayout.isEmpty(), "Specify the table layout with --layout=/path/to/table-layout.json"); if ((mNumRegionsFlag != null) && !mNumRegionsFlag.isEmpty()) { mNumRegions = Integer.parseInt(mNumRegionsFlag); Preconditions.checkArgument(mNumRegions >= 1, "Invalid initial number of regions {}, must be >= 1.", mNumRegions); } else if ((mSplitKeyFilePath == null) || mSplitKeyFilePath.isEmpty()) { // No region split specified, defaults to 1: mNumRegions = 1; } }
/** {@inheritDoc} */ @Override protected void cleanup() throws IOException { ResourceUtils.releaseOrLog(mFiji); mFiji = null; super.cleanup(); }
/** * Pretty print a tool error. * * @param toolError ToolError to pretty print. */ private void prettyPrintUserInputError( final ToolError toolError ) { if (mDebugFlag) { toolError.printStackTrace(getPrintStream()); } else { getPrintStream().println(toolError.getErrorMessage()); } }
/** * Prompts the user to specifically type in a specified user string for a specified question * and reports the result. This is primarily used for dangerous operations such as deleting or * uninstalling a Fiji table or instance. If inputConfirmation is called in non-interactive * mode, an IllegalStateException is thrown. * * @param question The question to which the user is expected to respond to. * @param confirm The requested input(such as instance or table name) to validate against. * @return <code>true</code> if the string was entered successfully, <code>false</code> if not. * @throws IOException if there is a problem reading from the terminal. */ protected final boolean inputConfirmation(String question, String confirm) throws IOException { String hint = String.format("Type '%s' without the quotes to confirm(or nothing to cancel):", confirm); Pattern confirmPattern = Pattern.compile(Pattern.quote(confirm), Pattern.CASE_INSENSITIVE); return confirmationPrompt(question, hint, confirmPattern, ALL_PATTERN); }
/** {@inheritDoc} */ @Override protected void validateFlags() throws Exception { super.validateFlags(); Preconditions.checkArgument((mTableURIFlag != null) && !mTableURIFlag.isEmpty(), "Specify the table to bulk-load into with " + "--table=fiji://hbase-address/fiji-instance/table"); mTableURI = FijiURI.newBuilder(mTableURIFlag).build(); Preconditions.checkArgument(mTableURI.getTable() != null, "Specify the table to bulk-load into with " + "--table=fiji://hbase-address/fiji-instance/table"); Preconditions.checkArgument((mHFileFlag != null) && !mHFileFlag.isEmpty(), "Specify the HFiles to bulk-load. " + "E.g. --hfile=hdfs://hdfs-cluster-address/path/to/hfile.dir/"); mHFile = new Path(mHFileFlag); }
@Override protected void validateFlags() throws Exception { super.validateFlags(); mTableURI = FijiURI.newBuilder(mFijiURIFlag).build(); }
/** {@inheritDoc} */ @Override protected void validateFlags() throws Exception { super.validateFlags(); Preconditions.checkArgument((mTableURIFlag != null) && !mTableURIFlag.isEmpty(), "Specify a target table to write synthesized data to with " + "--table=fiji://hbase-address/fiji-instance/table"); mTableURI = FijiURI.newBuilder(mTableURIFlag).build(); Preconditions.checkArgument(mTableURI.getTable() != null, "No table specified in target URI '%s'. " + "Specify a target table to write synthesized data to with " + "--table=fiji://hbase-address/fiji-instance/table", mTableURI); }