Skip to content

Commit

Permalink
add an optional destination key for yaml copykey
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 22, 2019
1 parent 65dfd20 commit 332dc30
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Expand Up @@ -4315,7 +4315,7 @@ public void registerCoreMembers() {

// <--[command]
// @Name Yaml
// @Syntax yaml [create]/[load:<file> (fix_formatting)]/[loadtext:<text> (fix_formatting)]/[unload]/[savefile:<file>]/[copykey:<source key> <target key>]/[set <key>([<#>])(:<action>):<value>] [id:<name>]
// @Syntax yaml [create]/[load:<file> (fix_formatting)]/[loadtext:<text> (fix_formatting)]/[unload]/[savefile:<file>]/[copykey:<source key> <target key> (to_id:<name>)]/[set <key>([<#>])(:<action>):<value>] [id:<name>]
// @Required 2
// @Short Edits a YAML configuration file.
// @Group core
Expand Down Expand Up @@ -4370,9 +4370,13 @@ public void registerCoreMembers() {
// @Usage
// Use to modify a copy the contents of one YAML key to a new owning key.
// - yaml id:myfile copykey:my.first.key my.new.key
//
// @Usage
// Use to modify a copy the contents of one YAML key to a new owning key on a different YAML file.
// - yaml id:myfile copykey:my.first.key my.new.key to_id:myotherfile
// -->
registerCoreMember(YamlCommand.class,
"YAML", "yaml [create]/[load:<file> (fix_formatting)]/[loadtext:<text> (fix_formatting)]/[unload]/[savefile:<file>]/[copykey:<source key> <target key>]/[set <key>([<#>])(:<action>):<value>] [id:<name>]", 2);
"YAML", "yaml [create]/[load:<file> (fix_formatting)]/[loadtext:<text> (fix_formatting)]/[unload]/[savefile:<file>]/[copykey:<source key> <target key> (to_id:<name>)]/[set <key>([<#>])(:<action>):<value>] [id:<name>]", 2);

// <--[command]
// @Name Zap
Expand Down
Expand Up @@ -59,47 +59,47 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("LOAD")) {
arg.matchesPrefix("load")) {
scriptEntry.addObject("action", new Element("LOAD"));
scriptEntry.addObject("filename", arg.asElement());
}
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("LOADTEXT")) {
arg.matchesPrefix("loadtext")) {
scriptEntry.addObject("action", new Element("LOADTEXT"));
scriptEntry.addObject("raw_text", arg.asElement());
}
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("SAVEFILE", "FILESAVE")) {
arg.matchesPrefix("savefile", "filesave")) {
scriptEntry.addObject("action", new Element("SAVE"));
scriptEntry.addObject("filename", arg.asElement());
}
else if (!scriptEntry.hasObject("action") &&
arg.matches("CREATE")) {
arg.matches("create")) {
scriptEntry.addObject("action", new Element("CREATE"));
}
else if (!scriptEntry.hasObject("action") &&
arg.matches("SET")) {
arg.matches("set")) {
scriptEntry.addObject("action", new Element("SET"));
isSet = true;
}
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("COPYKEY")) {
arg.matchesPrefix("copykey")) {
scriptEntry.addObject("action", new Element("COPYKEY"));
scriptEntry.addObject("key", arg.asElement());
isSet = true;
}
else if (!scriptEntry.hasObject("action") &&
arg.matches("UNLOAD")) {
arg.matches("unload")) {
scriptEntry.addObject("action", new Element("UNLOAD"));
}
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("WRITE")) {
arg.matchesPrefix("write")) {
dB.echoError(scriptEntry.getResidingQueue(), "YAML write is deprecated, use YAML set!");
scriptEntry.addObject("action", new Element("WRITE"));
scriptEntry.addObject("key", arg.asElement());
}
else if (!scriptEntry.hasObject("value") &&
arg.matchesPrefix("VALUE")) {
arg.matchesPrefix("value")) {
if (arg.matchesArgumentType(dList.class)) {
scriptEntry.addObject("value", arg.asType(dList.class));
}
Expand All @@ -108,9 +108,13 @@ else if (!scriptEntry.hasObject("value") &&
}
}
else if (!scriptEntry.hasObject("id") &&
arg.matchesPrefix("ID")) {
arg.matchesPrefix("id")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (!scriptEntry.hasObject("to_id") &&
arg.matchesPrefix("to_id")) {
scriptEntry.addObject("to_id", arg.asElement());
}
else if (!scriptEntry.hasObject("split") &&
arg.matches("split_list")) {
scriptEntry.addObject("split", new Element("true"));
Expand Down Expand Up @@ -226,6 +230,7 @@ public void execute(final ScriptEntry scriptEntry) {
Element actionElement = scriptEntry.getElement("action");
Element idElement = scriptEntry.getElement("id");
Element fixFormatting = scriptEntry.getElement("fix_formatting");
Element toId = scriptEntry.getElement("to_id");

YamlConfiguration yamlConfiguration;

Expand All @@ -240,6 +245,7 @@ public void execute(final ScriptEntry scriptEntry) {
+ (value != null ? value.debug() : "")
+ (split != null ? split.debug() : "")
+ (rawText != null ? rawText.debug() : "")
+ (toId != null ? toId.debug() : "")
+ fixFormatting.debug());

}
Expand Down Expand Up @@ -404,14 +410,26 @@ else if (split != null && split.asBoolean()) {
}
break;

case COPYKEY:
if (yamls.containsKey(id)) {
YamlConfiguration yaml = yamls.get(id);
YamlConfiguration sourceSection = yaml.getConfigurationSection(key.asString());
YamlConfiguration newSection = copySection(sourceSection);
yaml.set(value.toString(), newSection);
case COPYKEY: {
if (!yamls.containsKey(id)) {
break;
}
YamlConfiguration yaml = yamls.get(id);
YamlConfiguration destYaml = yaml;
if (toId != null) {
if (yamls.containsKey(toId.toString().toUpperCase())) {
destYaml = yamls.get(toId.toString().toUpperCase());
}
else {
dB.echoError("Unknown YAML TO-ID '" + id + "'");
break;
}
}
YamlConfiguration sourceSection = yaml.getConfigurationSection(key.asString());
YamlConfiguration newSection = copySection(sourceSection);
destYaml.set(value.toString(), newSection);
break;
}

case SET:
if (yamls.containsKey(id)) {
Expand Down

0 comments on commit 332dc30

Please sign in to comment.