- public static class Pwd extends Command {
- public String name() { return "pwd"; }
- public String usage() { return ""; }
- public String summary() { return "Path to current object."; }
- public String help() { return "Print the path to the current object."; }
- public void execute(Writer w, String[] c, Env env) throws IOException {
- if (c.length != 1) { w.write(usage()); return; }
- w.write(env.path.equals("") ? "/" : env.path.replace('.', '/'));
- w.write("\n");
+ public static class Replace extends Command {
+ public Replace(Shell s) { super(s); }
+ public String name() { return "replace"; }
+ public String params() { return "[key] [value]"; }
+ public String summary() { return "Sets a key to a specific value."; }
+ public String help() { return
+ "Sets a key to a specific value. This function accepts " +
+ "an xt shell path for the key name (eg. /foo/bar), and a " +
+ "script object for the value (eg. {}, [], \"foo\").\n\n" +
+
+ "If the key does not already exist, it is created, but an " +
+ "error is thrown if the parent object of the key does not " +
+ "exist.\n\n" +
+
+ "WARNING: This function is dangerous. It gives you a " +
+ "quick and easy way to replace your entire data set with "+
+ "an empty object.";
+ }
+ public void execute(Writer w, String[] c) throws IOException {
+ if (c.length < 3) { usage(w); return; }
+ try {
+ List path = fromShellPath(c[1]);
+ Object key = path.remove(path.size() - 1);
+ Object po = shell.getFromPath(path.toArray());
+
+ if (po == null || !(po instanceof JS))
+ throw new Shell.BadPathException();
+ JS parent = (JS)po;
+
+ String func = "prevalent.";
+ for (int i=0; i < path.size(); i++)
+ func += path.get(i) + ".";
+ func += key + " = ";
+ for (int i=2; i < c.length; i++)
+ func += c[i];
+ func += ";\n";
+
+ shell.transaction(JS.fromReader(
+ "replace-transaction", 0, new StringReader(func)));
+
+ } catch (JSExn e) {
+ w.write("error: cannot replace '");
+ w.write(c[1]);
+ w.write("': ");
+ w.write(e.getMessage());
+ w.write("\n");
+ } catch (Shell.BadPathException e) {
+ w.write("error: cannot replace '");
+ w.write(c[1]);
+ w.write("': ");
+ w.write(e.getMessage() == null ? "no such path" : e.getMessage());
+ w.write("\n");
+ }