mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 15:43:13 +09:00
Refactored string processing to function
Previously string processing was done the exact same way for both master and instance; this has now been refactored into a function.
This commit is contained in:
1 parent
c4f656996e
commit
e825f643e2
1 file changed
+36
-40
+36
-40
@@ -36,6 +36,30 @@ function print(...content) {
|
|||||||
console.log(...content);
|
console.log(...content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function configToKeyVal(data) {
|
||||||
|
let final = {};
|
||||||
|
let splitData = data.split(/\r?\n/);
|
||||||
|
// split on newlines
|
||||||
|
let filtered = splitData.filter((value) => !(value[0] === "#")).filter((a) => a);
|
||||||
|
// the last filter removes empty elements left by the first. Not done on one line due to readability.
|
||||||
|
for (let index in filtered) {
|
||||||
|
if (index in filtered) {
|
||||||
|
filtered[index] = filtered[index].split("=");
|
||||||
|
let finalIndex = filtered[index][0].trim();
|
||||||
|
// split on the = we added earlier
|
||||||
|
let part = "";
|
||||||
|
try {
|
||||||
|
part = filtered[index][1].trim();
|
||||||
|
// it's a string if we can read it
|
||||||
|
} catch (err) {
|
||||||
|
// if we can't read it, it's a empty field and therefor null
|
||||||
|
part = "";
|
||||||
|
}
|
||||||
|
final[finalIndex] = part;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return final;
|
||||||
|
}
|
||||||
|
|
||||||
const masterCommands = new libCommand.CommandTree({ name: "master", description: "Master management" });
|
const masterCommands = new libCommand.CommandTree({ name: "master", description: "Master management" });
|
||||||
const masterConfigCommands = new libCommand.CommandTree({
|
const masterConfigCommands = new libCommand.CommandTree({
|
||||||
@@ -161,31 +185,17 @@ masterConfigCommands.add(new libCommand.Command({
|
|||||||
let doneEmitter = new events.EventEmitter();
|
let doneEmitter = new events.EventEmitter();
|
||||||
editorSpawn.on("exit", async (exit) => {
|
editorSpawn.on("exit", async (exit) => {
|
||||||
const data = await fs.readFile(tmpFile, "utf8");
|
const data = await fs.readFile(tmpFile, "utf8");
|
||||||
let splitData = data.split(/\r?\n/);
|
const final = await configToKeyVal(data);
|
||||||
// split on newlines
|
for (let index in final) {
|
||||||
let filtered = splitData.filter((value) => !(value[0] === "#")).filter((a) => a);
|
if (index in final) {
|
||||||
// the last filter removes empty elements left by the first. Not done on one line due to readability.
|
|
||||||
for (let index in filtered) {
|
|
||||||
if (index in filtered) {
|
|
||||||
filtered[index] = filtered[index].split("=");
|
|
||||||
let finalIndex = filtered[index][0].trim();
|
|
||||||
// split on the = we added earlier
|
|
||||||
let part = "";
|
|
||||||
try {
|
|
||||||
part = filtered[index][1].trim();
|
|
||||||
// it's a string if we can read it
|
|
||||||
} catch (err) {
|
|
||||||
// if we can't read it, it's a empty field and therefor null
|
|
||||||
part = "";
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
await libLink.messages.setMasterConfigField.send(control, {
|
await libLink.messages.setMasterConfigField.send(control, {
|
||||||
field: finalIndex,
|
field: index,
|
||||||
value: part,
|
value: final[index],
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
print(`Attempt to set ${finalIndex} to ${part || String(null)} failed; set back to previous value.`);
|
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
|
||||||
print(err);
|
print(err);
|
||||||
// If the string is empty, it's better to just print "" instead of nothing
|
// If the string is empty, it's better to just print "" instead of nothing
|
||||||
}
|
}
|
||||||
@@ -466,32 +476,18 @@ instanceConfigCommands.add(new libCommand.Command({
|
|||||||
let doneEmitter = new events.EventEmitter();
|
let doneEmitter = new events.EventEmitter();
|
||||||
editorSpawn.on("exit", async (exit) => {
|
editorSpawn.on("exit", async (exit) => {
|
||||||
const data = await fs.readFile(tmpFile, "utf8");
|
const data = await fs.readFile(tmpFile, "utf8");
|
||||||
let splitData = data.split(/\r?\n/);
|
const final = await configToKeyVal(data);
|
||||||
// split on newlines
|
for (let index in final) {
|
||||||
let filtered = splitData.filter((value) => !(value[0] === "#")).filter((a) => a);
|
if (index in final) {
|
||||||
// the last filter removes empty elements left by the first. Not done on one line due to readability.
|
|
||||||
for (let index in filtered) {
|
|
||||||
if (index in filtered) {
|
|
||||||
filtered[index] = filtered[index].split("=");
|
|
||||||
let finalIndex = filtered[index][0].trim();
|
|
||||||
// split on the = we added earlier
|
|
||||||
let part = "";
|
|
||||||
try {
|
|
||||||
part = filtered[index][1].trim();
|
|
||||||
// it's a string if we can read it
|
|
||||||
} catch (err) {
|
|
||||||
// if we can't read it, it's a empty field and therefor null
|
|
||||||
part = "";
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
await libLink.messages.setInstanceConfigField.send(control, {
|
await libLink.messages.setInstanceConfigField.send(control, {
|
||||||
instance_id: instanceId,
|
instance_id: instanceId,
|
||||||
field: finalIndex,
|
field: index,
|
||||||
value: part,
|
value: final[index],
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
print(`\n\n\nAttempt to set ${finalIndex} to ${JSON.stringify(part) || String(null)} failed; set back to previous value.`);
|
print(`\n\n\nAttempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
|
||||||
// If the string is empty, it's better to just print "" instead of nothing
|
// If the string is empty, it's better to just print "" instead of nothing
|
||||||
print("This message shouldn't normally appear; if the below message does not indicate it");
|
print("This message shouldn't normally appear; if the below message does not indicate it");
|
||||||
print("was a user mistake, please report it to the clustorio devs.");
|
print("was a user mistake, please report it to the clustorio devs.");
|
||||||
|
|||||||
Reference in new issue
Block a user