cmd.add("bash -c "+BashStringEscapes.wrapBash(command));
/** * Merges the commands and env, into a single set of commands. Also escapes the commands as required. * * Not all ssh servers handle "env", so instead convert env into exported variables */ protected List<String> toCommandSequence(List<String> commands, Map<String,?> env) { List<String> result = new ArrayList<String>((env!=null ? env.size() : 0) + commands.size()); if (env!=null) { for (Entry<String,?> entry : env.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) { LOG.warn("env key-values must not be null; ignoring: key="+entry.getKey()+"; value="+entry.getValue()); continue; } String escapedVal = BashStringEscapes.escapeLiteralForDoubleQuotedBash(entry.getValue().toString()); result.add("export "+entry.getKey()+"=\""+escapedVal+"\""); } } for (CharSequence cmd : commands) { // objects in commands can be groovy GString so can't treat as String here result.add(cmd.toString()); } return result; }
public String getArgs(){ List<String> args = entity.getConfig(VanillaJavaApp.ARGS); StringBuffer sb = new StringBuffer(); for(Iterator<String> it = args.iterator();it.hasNext();){ String arg = it.next(); BashStringEscapes.assertValidForDoubleQuotingInBash(arg); sb.append(format("\"%s\"",arg)); if(it.hasNext()){ sb.append(" "); } } return sb.toString(); }
result.add(wrapBash(hostAndPort.getHostText())); Integer whichPort = knifeWhichPort(hostAndPort); if (whichPort!=null) result.add("-p "+whichPort); result.add("-x "+wrapBash(checkNotNull(machine.getUser(), "user"))); result.add("-r "+wrapBash(runList.apply(entity()))); result.add("-j "+wrapBash(new GsonBuilder().create() .toJson(knifeAttributes)));
/** * Sets all JVM options (-X.. -D..) in an environment var JAVA_OPTS. * <p> * That variable is constructed from getJavaOpts(), then wrapped _unescaped_ in double quotes. An * error is thrown if there is an unescaped double quote in the string. All other unescaped * characters are permitted, but unless $var expansion or `command` execution is desired (although * this is not confirmed as supported) the generally caller should escape any such characters, for * example using {@link BashStringEscapes#escapeLiteralForDoubleQuotedBash(String)}. */ @Override public Map<String, String> getShellEnvironment() { for (String it : getJavaOpts()) { BashStringEscapes.assertValidForDoubleQuotingInBash(it); } // do not double quote here; the env var is double quoted subsequently; // spaces should be preceded by double-quote // (if dbl quotes are needed we could pass on the command-line instead of in an env var) String sJavaOpts = Joiner.on(" ").join(getJavaOpts()); // println "using java opts: $sJavaOpts" return MutableMap.<String, String> builder().putAll(super.getShellEnvironment()).put("JAVA_OPTS", sJavaOpts).build(); }
public static boolean isValidForDoubleQuotingInBash(String x) { return (checkValidForDoubleQuotingInBash(x)==null); }
/** transforms e.g. [ "-Dname=Bob Johnson", "-Dnet.worth=$100" ] to * a java string "\"-Dname=Bob Johnson\" \"-Dnet.worth=\$100\"" -- * which can be inserted into a bash command where it will be picked up as 2 params */ public static String doubleQuoteLiteralsForBash(String... args) { StringBuilder result = new StringBuilder(); for (String arg: args) { if (!Strings.isEmpty(result)) result.append(" "); result.append("\""); result.append(escapeLiteralForDoubleQuotedBash(arg)); result.append("\""); } return result.toString(); }
public static String warn(String message) { return "( echo "+BashStringEscapes.wrapBash(message)+" | tee /dev/stderr )"; }
/** fails with nice error if the given file does not exist */ public static String requireExecutable(String command) { return require("which "+BashStringEscapes.wrapBash(command), "The required executable \""+command+"\" does not exist"); }
/** fails with nice error if the given file does not exist */ public static String requireFile(String file) { return requireTest("-f "+BashStringEscapes.wrapBash(file), "The required file \""+file+"\" does not exist"); }
public static void assertValidForDoubleQuotingInBash(String x) { String problem = checkValidForDoubleQuotingInBash(x); if (problem==null) return; throw new IllegalArgumentException("String \""+x+"\" not acceptable for bash argument (including double quotes): "+problem); }
/** performs replacements on a string so that it can be legally inserted into a double-quoted bash context * (without the surrounding double quotes; see also {@link #wrapBash(String)}) */ public static String escapeLiteralForDoubleQuotedBash(String unquotedInputToBeEscaped) { StringBuilder out = new StringBuilder(); try { escapeLiteralForDoubleQuotedBash(unquotedInputToBeEscaped, out); } catch (IOException e) { // shouldn't happen for StringBuilder throw Throwables.propagate(e); } return out.toString(); }
@Nullable protected String knifeConfigFileOption() { if (knifeConfigFile!=null) return "-c "+knifeConfigFile; String knifeConfigFileFromConfig = entityConfig(ChefConfig.KNIFE_CONFIG_FILE); if (knifeConfigFileFromConfig!=null) return "-c "+BashStringEscapes.wrapBash(knifeConfigFileFromConfig); // if not supplied will use global config return null; }
protected String knifeExecutable() { if (knifeExecutable!=null) return knifeExecutable; String knifeExecFromConfig = entityConfig(ChefConfig.KNIFE_EXECUTABLE); if (knifeExecFromConfig!=null) return BashStringEscapes.wrapBash(knifeExecFromConfig); // assume on the path, if executable not set return "knife"; }
/** wraps plain text in double quotes escaped for use in bash double-quoting */ public static String wrapBash(String value) { StringBuilder out = new StringBuilder(); try { wrapBash(value, out); } catch (IOException e) { //shouldn't happen for string buffer throw Throwables.propagate(e); } return out.toString(); }
/** @see #wrapBash(String) */ public static void wrapBash(String value, Appendable out) throws IOException { out.append('"'); escapeLiteralForDoubleQuotedBash(value, out); out.append('"'); }