/** * returns "true if the command has the flag --help in it. This is a cue from the user to show * the help for a given function. So it the function is called "X" and its help is in the function * "showXHelp" then a value of true from this should simply invoke "showXHelp" and return. * * @param inputLine * @return */ protected boolean showHelp(InputLine inputLine) { if ((1 < inputLine.size()) && inputLine.getArg(1).equals("--help")) return true; return false; }
/** * This will take an input line and search for the arg, returning the next value. * E.g. if the input line is "-file y -id z -v" * then supplying "-file" as the key here will return "y". * If there is no such key, then a null is returned. * * @param inputLine * @param key * @return */ protected String getArgValue(InputLine inputLine, String key) { int index = inputLine.indexOf(key); if (index == -1) return null; // Remember that the input line has the name of the method as the zeroth argument, so // it is always at least 1 in length if (inputLine.size() + 1 == index) { //then this is the final argument and nothing follows return null; } return inputLine.getArg(index + 1); }
/** * Resolves the first argument of a command line into either a unique identifier * * @param inputLine * @return */ protected Identifiable findItem(InputLine inputLine) { // first case is one in which this does not apply since there is no argument. if (inputLine.size() <= 1) { return null; } if (inputLine.getLastArg().startsWith("/")) { // then try to process this as a unique identifier. String arg = inputLine.getLastArg().substring(1); Identifier id = BasicIdentifier.newID(arg); return (Identifiable) getStore().get(id); } int choice = inputLine.getIntArg(inputLine.size() - 1); if (allEntries == null || allEntries.isEmpty()) { loadAllEntries(); // just in case... } return allEntries.get(choice); }
/** * This will find the index for the "key" and return the very next argument. This is a * very, very use idiom for retrieving arguments for options.<br/><br/> * E.g. If the command line were * <pre> * myfunc -x foo -y fnord -blarg * </pre> * Then * <pre> * getNextArgFor("-x"); * </pre> * would return "foo". On the other hand * <pre> * getNextArgFor("-blarg"); * </pre> * would return a null, since there is no possible argument for it. * * @param key * @return */ public String getNextArgFor(String key){ int index = indexOf(key); // NOTE that the indexOf command starts at 1, since the zeroth index is always omitted if(index ==getArgs().size()){ // so it is the last arg in the string and there cannot be another return null; } return getArg(1 + index); // finally, a result! } }
public void create_hash(InputLine inputLine) { if (showHelp(inputLine)) { showCreateHashHelp(); return; } String secret = null; if (inputLine.hasArg("-file")) { try { FileReader fis = new FileReader(inputLine.getArg(1 + inputLine.indexOf("-file"))); StringBuffer sb = new StringBuffer(); int i; while ((i = fis.read()) != -1) { sb.append((char) i); } fis.close(); secret = sb.toString(); } catch (Throwable t) { say("Error: could not read file: " + t.getMessage()); return; } } else { secret = inputLine.getLastArg(); } say("creating hash of " + secret); say(DigestUtils.sha1Hex(secret)); }
OutputStream os = System.out; boolean hasFile = false; if (inputLine.hasArg("-file")) { try { os = new FileOutputStream(inputLine.getNextArgFor("-file")); hasFile = true; } catch (FileNotFoundException e) { say("warning, could not find file at path " + inputLine.getArg(inputLine.indexOf("-file")));
return; if (1 == inputLine.size()) { if (!inputLine.getLastArg().startsWith("-")) { Identifiable identifiable = findItem(inputLine); if (identifiable == null) { if (inputLine.getArg(1).contains("l")) longForm = true; listAll(true, inputLine.getArg(1)); } else { listAll(false, inputLine.getArg(1));
public void validate_token(InputLine inputLine) throws Exception { if (showHelp(inputLine)) { printValidateTokenHelp(); return; } String token = null; if (1 == inputLine.size()) { say("Sorry, no argument"); return; } if (inputLine.hasArg("-file")) { token = getArgValue(inputLine, "-file"); } else { token = inputLine.getArg(1); } String[] x = decat(token); JSONObject h = JSONObject.fromObject(new String(Base64.decodeBase64(x[0]))); JSONObject p = JSONObject.fromObject(new String(Base64.decodeBase64(x[1]))); say("header=" + h); say("payload=" + p); if (JWTUtil.verify(h, p, x[2], keys.get(defaultKeyID))) { say("token valid!"); } else { say("could not validate token"); } } }
return; if(inputLine.hasArg("-file")) { File f = new File(inputLine.getArg(1)); if (!f.exists()) { say("Sorry, the file you specified, \"" + (inputLine.getArg(1)) + "\" does not exist."); return; wellKnown = inputLine.getArg(1); try { keys = JWTUtil.getJsonWebKeys(new ServiceClient(URI.create("https://scitokens.org")), wellKnown);
if (inputLine.hasArg(LIST_KEYS_FLAG)) { if (getStore().getXMLConverter() instanceof MapConverter) { say("keys"); if (inputLine.hasArg(KEY_FLAG)) { String key = inputLine.getNextArgFor(KEY_FLAG); if (!keys.allKeys().contains(key)) { say("Sorry, but the key \"" + key + "\" is not a known key for this component."); values = getStore().search(key, inputLine.getLastArg(), inputLine.hasArg(REGEX_FLAG)); }catch(Throwable t){ say("Sorry, that didn't work:" + t.getMessage()); say("no matches"); if (inputLine.hasArg(SIZE_FLAG)) { say("Got " + values.size() + " results"); return; if (inputLine.hasArg(LONG_LIST_FLAG)) { longFormat(identifiable); } else {
/** * If this command line has arguments at all. * * @return */ public boolean hasArgs() { return 1 < size(); }
@Override public boolean use(InputLine inputLine) throws Exception { CommonCommands commands = null; if (inputLine.hasArg(CLIENTS)) { commands = getNewClientStoreCommands(); } if (inputLine.hasArg(CLIENT_APPROVALS)) { commands = getNewClientApprovalStoreCommands(); } if (inputLine.hasArg(COPY)) { commands = getNewCopyCommands(); } if (commands != null) { CLIDriver cli = new CLIDriver(commands); cli.start(); return true; } if (super.use(inputLine)) { return true; } return false; }
/** * If the state supports this, it will save the current cert to a file. The complete filename must be supplied, * including any path. * * @param inputLine * @throws Exception */ public void savecert(InputLine inputLine) throws Exception { if (showHelp(inputLine)) { saveCertHelp(); return; } if (assetResponse == null) { say("Sorry, but there is no cert to save. Please do a successful getcert call first."); return; } String cert = CertUtil.toPEM(assetResponse.getX509Certificates()); if (!inputLine.hasArgs()) { say("Sorry. You did not specify a file so the cert cannot be saved."); return; } String fileName = inputLine.getArg(1); FileWriter fileWriter = new FileWriter(fileName); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(cert + "\n"); bufferedWriter.flush(); bufferedWriter.close(); say("File \"" + fileName + "\" saved successfully."); }
/** * Check if the input line has the given argument. False is returned otherwise. * * @param arg * @return */ public boolean hasArg(String arg) { return -1 != indexOf(arg); }
boolean isNew = inputLine.hasArg("-new"); if (inputLine.hasArg("-file")) { try { is = new FileInputStream(inputLine.getNextArgFor("-file")); XMLMap map = new XMLMap(); map.fromXML(is); say("warning, could not find file at path \"" + inputLine.getNextArgFor("-file") + "\", " + e.getMessage());
/** * Returns the index of the target in the input line or a -1 if it does not occur. * * @param arg * @return */ public int indexOf(String arg) { int index = 1; // zero-th argument is the name of the method, so getArgs start at index 1. for (String x : getArgs()) { if (x.equals(arg)) return index; index++; } return -1; }
InputLine cliAV = new InputLine(cmdV); for (int i = 0; i < getCLICommands().length; i++) { try {
/** * Override this to invoke the specific components that make up your CLI. * @param inputLine * @return * @throws Exception */ public boolean use(InputLine inputLine) throws Exception { if (showHelp(inputLine)) { useHelp(); return true; } if (1 == inputLine.size()) { say("Sorry, you need to give the name of the component to invoke it."); return true; } return false; } }
@Override public boolean use(InputLine inputLine) throws Exception { CommonCommands commands = null; if (inputLine.hasArg(ADMINS)) { commands = getAdminClientCommands(); } if (inputLine.hasArg(KEYS)) { commands = new SigningCommands(getOA2SE()); } if (inputLine.hasArg(PERMISSIONS)) { commands = getPermissionCommands(); } if (commands != null) { CLIDriver cli = new CLIDriver(commands); cli.start(); return true; } if (super.use(inputLine)) { return true; } return false; } }