mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 23:53:13 +09:00
Some fixes to config setting
This commit is contained in:
6 files changed
+28
-19
No files matched your search
@@ -315,23 +315,24 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handleInstanceConfigSetRequest(request: lib.InstanceConfigSetRequest) {
|
async handleInstanceConfigSetRequest(request: lib.InstanceConfigSetRequest) {
|
||||||
|
const instance = this._controller.instances.getForRequest(request.instanceId);
|
||||||
try {
|
try {
|
||||||
for (const [fieldName, fieldValue] of Object.entries(request.fields)) {
|
for (const [fieldName, fieldValue] of Object.entries(request.fields)) {
|
||||||
ControlConnection.validateInstanceConfigSetField(fieldName);
|
ControlConnection.validateInstanceConfigSetField(fieldName);
|
||||||
if (typeof fieldValue === "object") {
|
if (typeof fieldValue === "object") {
|
||||||
for (const [propName, propValue] of Object.entries(fieldValue)) {
|
for (const [propName, propValue] of Object.entries(fieldValue)) {
|
||||||
this._controller.config.stageProp(
|
instance.config.stageProp(
|
||||||
fieldName as keyof lib.ControllerConfigFields, propName, propValue, "control"
|
fieldName as keyof lib.InstanceConfigFields, propName, propValue, "control"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this._controller.config.stage(fieldName as keyof lib.ControllerConfigFields, fieldValue, "control");
|
instance.config.stage(fieldName as keyof lib.InstanceConfigFields, fieldValue, "control");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this._controller.config.commitStaging();
|
instance.config.commitStaging();
|
||||||
} finally {
|
} finally {
|
||||||
this._controller.config.revertStaging();
|
instance.config.revertStaging();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import * as libSchema from "../schema";
|
|||||||
import { StringEnum } from "../data/composites";
|
import { StringEnum } from "../data/composites";
|
||||||
import { safeOutputFile } from "../file_ops";
|
import { safeOutputFile } from "../file_ops";
|
||||||
import * as validators from "./validators";
|
import * as validators from "./validators";
|
||||||
import { string } from "yargs";
|
|
||||||
|
|
||||||
const ConfigLocation = StringEnum(["controller", "host", "control"]);
|
const ConfigLocation = StringEnum(["controller", "host", "control"]);
|
||||||
export type ConfigLocation = Static<typeof ConfigLocation>;
|
export type ConfigLocation = Static<typeof ConfigLocation>;
|
||||||
@@ -208,7 +207,7 @@ export class Config<
|
|||||||
* Note that mutating this object will lead to unexpected behaviour.
|
* Note that mutating this object will lead to unexpected behaviour.
|
||||||
*/
|
*/
|
||||||
declare static fieldDefinitions: ConfigDefs<any>;
|
declare static fieldDefinitions: ConfigDefs<any>;
|
||||||
declare ["constructor"]: typeof Config;
|
declare ["constructor"]: typeof Config<Fields>;
|
||||||
|
|
||||||
/** A set of common validators and validator factories */
|
/** A set of common validators and validator factories */
|
||||||
static validators = validators;
|
static validators = validators;
|
||||||
@@ -272,14 +271,14 @@ export class Config<
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** A staging version of the config with changes tracked, invalided by revertStaging */
|
/** A staging version of the config with changes tracked, invalided by revertStaging */
|
||||||
private _staging?: { config: Config<Fields>, changes: Set<keyof Fields & string> };
|
private _staging?: { config: Config<Fields>, changes: Set<string>};
|
||||||
|
|
||||||
/** Get the latest version of staging */
|
/** Get the latest version of staging */
|
||||||
get staging(): Config<Fields> {
|
get staging(): Config<Fields> {
|
||||||
if (this._staging) { return this._staging.config; }
|
if (this._staging) { return this._staging.config; }
|
||||||
const config = new this.constructor(this.location, this.fields) as this;
|
const config = new this.constructor(this.location, this.fields);
|
||||||
const changes = new Set<keyof Fields & string>();
|
const changes = new Set<string>();
|
||||||
config.on("fieldChanged", name => changes.add(name as keyof Fields & string));
|
config.on("fieldChanged", name => changes.add(name));
|
||||||
this._staging = { config, changes };
|
this._staging = { config, changes };
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
@@ -832,7 +831,8 @@ export class Config<
|
|||||||
|
|
||||||
// Validation success, set all values which have changed
|
// Validation success, set all values which have changed
|
||||||
for (const fieldName of staging.changes) {
|
for (const fieldName of staging.changes) {
|
||||||
this._set(fieldName, config.fields[fieldName]);
|
const field = fieldName as keyof Fields & string;
|
||||||
|
this._set(field, config.fields[field]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,12 +45,12 @@ export class ControllerConfigSetRequest {
|
|||||||
static permission = "core.controller.update_config" as const;
|
static permission = "core.controller.update_config" as const;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public fields: Record<string, string | Record<string, string>>,
|
public fields: Record<string, string | Record<string, unknown>>,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
static jsonSchema = Type.Object({
|
static jsonSchema = Type.Object({
|
||||||
"fields": Type.Record(Type.String(), Type.Union([
|
"fields": Type.Record(Type.String(), Type.Union([
|
||||||
Type.String(), Type.Record(Type.String(), Type.String()),
|
Type.String(), Type.Record(Type.String(), Type.Unknown()),
|
||||||
])),
|
])),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -45,12 +45,12 @@ export class HostConfigSetRequest {
|
|||||||
static permission = "core.host.update_config" as const;
|
static permission = "core.host.update_config" as const;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public fields: Record<string, string | Record<string, string>>,
|
public fields: Record<string, string | Record<string, unknown>>,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
static jsonSchema = Type.Object({
|
static jsonSchema = Type.Object({
|
||||||
"fields": Type.Record(Type.String(), Type.Union([
|
"fields": Type.Record(Type.String(), Type.Union([
|
||||||
Type.String(), Type.Record(Type.String(), Type.String()),
|
Type.String(), Type.Record(Type.String(), Type.Unknown()),
|
||||||
])),
|
])),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -111,13 +111,13 @@ export class InstanceConfigSetRequest {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public instanceId: number,
|
public instanceId: number,
|
||||||
public fields: Record<string, string | Record<string, string>>,
|
public fields: Record<string, string | Record<string, unknown>>,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
static jsonSchema = Type.Object({
|
static jsonSchema = Type.Object({
|
||||||
"instanceId": Type.Integer(),
|
"instanceId": Type.Integer(),
|
||||||
"fields": Type.Record(Type.String(), Type.Union([
|
"fields": Type.Record(Type.String(), Type.Union([
|
||||||
Type.String(), Type.Record(Type.String(), Type.String()),
|
Type.String(), Type.Record(Type.String(), Type.Unknown()),
|
||||||
])),
|
])),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -164,7 +164,15 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
const [fieldName, prop] = propsMap.get(field)!;
|
const [fieldName, prop] = propsMap.get(field)!;
|
||||||
const fieldValue = (fields[fieldName] ?? {}) as Record<string, string>;
|
const fieldValue = (fields[fieldName] ?? {}) as Record<string, string>;
|
||||||
fields[fieldName] = fieldValue;
|
fields[fieldName] = fieldValue;
|
||||||
fieldValue[prop] = value ? JSON.parse(value) : undefined;
|
try {
|
||||||
|
fieldValue[prop] = value ? JSON.parse(value) : undefined;
|
||||||
|
} catch (err: any) {
|
||||||
|
setErrorFields(errors => {
|
||||||
|
const newErrors = new Map(errors);
|
||||||
|
newErrors.set(fieldName, err.message);
|
||||||
|
return newErrors;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fields[field] = value === null ? "" : String(value);
|
fields[field] = value === null ? "" : String(value);
|
||||||
|
|||||||
Reference in new issue
Block a user