Assume bad JSON is string in set-prop

Assume value is supposed to be a string if it fails to parse as JSON and
it does not start and end in a [], {}, or "" pair.
This commit is contained in:
Hornwitser committed 2021-01-16 11:01:15 +01:00
1 parent 1b901f5d1c
commit 201eb89dcd
1 file changed
+22 -1
+22 -1
View File
@@ -158,11 +158,32 @@ instanceConfigCommands.add(new libCommand.Command({
}],
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
let value = args.value;
try {
value = JSON.parse(value);
} catch (err) {
// If this is looks like an array, object or string literal
// throw the parse error, otherwise assume this is a string.
// The resoning behind this is that correctly quoting the string
// with the all the layers of quote removal at play is difficult.
// See the following table for how to pass "That's a \" quote" in
// different environments:
// cmd : """""That's a \\"" quote"""""
// cmd + npx : """""""""""That's a \\\\"""" quote"""""""""""
// PowerShell : '"""""That''s a \\"" quote"""""'
// PowerShell + npx : '"""""""""""That''s a \\\\"""" quote"""""""""""'
// bash : '""That'\''s a \" quote""'
// bash + npx : '""That'\''s a \" quote""'
// bash + npx -s sh : "'\"\"That'\\''s a \\\" quote\"\"'"
if (/^(\[.*]|{.*}|".*")$/.test(value)) {
throw new libErrors.CommandError(`In parsing value '${value}': ${err.message}`);
}
}
await libLink.messages.setInstanceConfigProp.send(control, {
instance_id: instanceId,
field: args.field,
prop: args.prop,
value: JSON.parse(args.value),
value,
});
},
}));