From 8dcd09248633282c9ec641b56c00ea3d95d790e7 Mon Sep 17 00:00:00 2001 From: crawshaw Date: Tue, 7 Dec 2004 19:13:06 +0000 Subject: [PATCH] add mkdir and replace functions darcs-hash:20041207191306-2eb37-1e9e60c0c0ac48a4bdaf273efea684b8a4e6adf8.gz --- src/java/org/ibex/xt/shell/Command.java | 108 +++++++++++++++++++++++++++++- src/java/org/ibex/xt/shell/JSRemote.java | 3 +- src/java/org/ibex/xt/shell/Shell.java | 2 + 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/src/java/org/ibex/xt/shell/Command.java b/src/java/org/ibex/xt/shell/Command.java index 04ca7b1..c7f3025 100644 --- a/src/java/org/ibex/xt/shell/Command.java +++ b/src/java/org/ibex/xt/shell/Command.java @@ -160,12 +160,118 @@ public abstract class Command { } } + 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"); + } + } + } + + public static class Mkdir extends Command { + public Mkdir(Shell s) { super(s); } + public String name() { return "mkdir"; } + public String params() { return "[path]"; } + public String summary() { return "Creates a new object ready to handle keys."; } + public String help() { return + "Creates a new object ready to handle keys. This function " + + "is similar to calling replace [path] {}, only it will not " + + "overwrite an existing key."; + } + public void execute(Writer w, String[] c) throws IOException { + if (c.length != 2) { 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; + if (parent.containsKey(key)) + throw new Shell.BadPathException("already exists"); + + String func = "prevalent."; + for (int i=0; i < path.size(); i++) + func += path.get(i) + "."; + func += key + " = {};\n"; + + shell.transaction(JS.fromReader( + "mkdir-transaction", 0, new StringReader(func))); + + } catch (JSExn e) { + w.write("error: cannot create '"); + w.write(c[1]); + w.write("': "); + w.write(e.getMessage()); + w.write("\n"); + } catch (Shell.BadPathException e) { + w.write("error: cannot create '"); + w.write(c[1]); + w.write("': "); + w.write(e.getMessage() == null ? "no such path" : e.getMessage()); + w.write("\n"); + } + } + } + public static class Rm extends Command { public Rm(Shell s) { super(s); } public String name() { return "rm"; } public String params() { return "[options] [path]"; } public String summary() { return "Removes objects."; } - public String help() { return "Removes objects."; } // FIXME info + public String help() { return + "Removes objects. If any one of the specified paths does " + + "not exist, the entire remove process is cancelled."; } public void execute(Writer w, String[] c) throws IOException { if (c.length == 1) { usage(w); return; } diff --git a/src/java/org/ibex/xt/shell/JSRemote.java b/src/java/org/ibex/xt/shell/JSRemote.java index 4b3e475..e54a9f5 100644 --- a/src/java/org/ibex/xt/shell/JSRemote.java +++ b/src/java/org/ibex/xt/shell/JSRemote.java @@ -176,6 +176,7 @@ public class JSRemote extends JS { } int pos = k.lastIndexOf('.'); if (pos >= 0) { + if (path == null) path = ""; path += k.substring(0, pos); k = k.substring(pos + 1); } @@ -196,7 +197,7 @@ public class JSRemote extends JS { if (s.length() > 0 && !s.equals(".")) { Object ob = scope.get(s); if (ob == null || !(ob instanceof JS)) - throw new JSExn("path not found"); + throw new JSExn("path not found "); scope = (JS)ob; } } diff --git a/src/java/org/ibex/xt/shell/Shell.java b/src/java/org/ibex/xt/shell/Shell.java index da77e94..ceef6bf 100644 --- a/src/java/org/ibex/xt/shell/Shell.java +++ b/src/java/org/ibex/xt/shell/Shell.java @@ -122,6 +122,8 @@ public class Shell { new Command.Pwd(this), new Command.Cd(this), new Command.Rm(this), + new Command.Replace(this), + new Command.Mkdir(this), new Command.Help(this) }; } -- 1.7.10.4