171 lines
4.2 KiB
JavaScript
Raw Normal View History

2025-03-31 14:26:57 +02:00
export class ConfigMenu {
2025-04-01 14:33:44 +02:00
constructor(
uiService,
menuLoop,
configService,
pathSelector,
numberSelector,
2025-04-07 16:01:49 +02:00
dataDirMover,
2025-04-01 14:33:44 +02:00
) {
2025-03-31 14:26:57 +02:00
this.ui = uiService;
2025-04-01 14:33:44 +02:00
this.loop = menuLoop;
2025-03-31 14:26:57 +02:00
this.configService = configService;
this.pathSelector = pathSelector;
this.numberSelector = numberSelector;
2025-04-07 16:01:49 +02:00
this.dataDirMover = dataDirMover;
2025-04-01 14:33:44 +02:00
this.loop.initialize(this.showConfigMenu);
2025-03-31 14:26:57 +02:00
}
2025-04-01 14:33:44 +02:00
show = async () => {
2025-03-31 14:26:57 +02:00
this.config = this.configService.get();
2025-04-07 16:01:49 +02:00
this.originalDataDir = this.config.dataDir;
2025-04-01 14:33:44 +02:00
this.ui.showInfoMessage("Codex Configuration");
await this.loop.showLoop();
};
showConfigMenu = async () => {
await this.ui.askMultipleChoice("Select to edit:", [
{
label: `Data path = "${this.config.dataDir}"`,
action: this.editDataDir,
},
{
label: `Logs path = "${this.config.logsDir}"`,
action: this.editLogsDir,
},
{
label: `Storage quota = ${this.bytesAmountToString(this.config.storageQuota)}`,
action: this.editStorageQuota,
},
{
label: `Discovery port = ${this.config.ports.discPort}`,
action: this.editDiscPort,
},
{
label: `P2P listen port = ${this.config.ports.listenPort}`,
action: this.editListenPort,
},
{
label: `API port = ${this.config.ports.apiPort}`,
action: this.editApiPort,
},
{
label: "Save changes and exit",
action: this.saveChangesAndExit,
},
{
label: "Discard changes and exit",
action: this.discardChangesAndExit,
},
]);
};
// this and the byte-format handling in
// numberSelector should be extracted to
// their own util.
2025-03-31 14:26:57 +02:00
bytesAmountToString = (numBytes) => {
const units = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
var value = numBytes;
var index = 0;
while (value > 1024) {
index = index + 1;
value = value / 1024;
}
if (index == 0) return `${numBytes} Bytes`;
return `${numBytes} Bytes (${value} ${units[index]})`;
2025-04-01 14:33:44 +02:00
};
2025-03-31 14:26:57 +02:00
editDataDir = async () => {
2025-04-07 16:01:49 +02:00
this.config.dataDir = await this.pathSelector.show(
this.config.dataDir,
false,
);
2025-04-01 14:33:44 +02:00
};
2025-03-31 14:26:57 +02:00
editLogsDir = async () => {
2025-04-01 14:33:44 +02:00
this.config.logsDir = await this.pathSelector.show(
this.config.logsDir,
true,
);
};
2025-03-31 14:26:57 +02:00
editStorageQuota = async () => {
this.ui.showInfoMessage("You can use: 'GB' or 'gb', etc.");
2025-04-01 14:33:44 +02:00
const newQuota = await this.numberSelector.show(
this.config.storageQuota,
"Storage quota",
true,
);
2025-03-31 14:26:57 +02:00
if (newQuota < 100 * 1024 * 1024) {
this.ui.showErrorMessage("Storage quote should be >= 100mb.");
} else {
this.config.storageQuota = newQuota;
}
2025-04-01 14:33:44 +02:00
};
2025-03-31 14:26:57 +02:00
editDiscPort = async () => {
2025-04-01 14:33:44 +02:00
const newPort = await this.numberSelector.show(
this.config.ports.discPort,
"Discovery port",
false,
);
2025-03-31 14:26:57 +02:00
if (this.isInPortRange(newPort)) {
this.config.ports.discPort = newPort;
}
2025-04-01 14:33:44 +02:00
};
2025-03-31 14:26:57 +02:00
editListenPort = async () => {
2025-04-01 14:33:44 +02:00
const newPort = await this.numberSelector.show(
this.config.ports.listenPort,
"P2P listen port",
false,
);
2025-03-31 14:26:57 +02:00
if (this.isInPortRange(newPort)) {
this.config.ports.listenPort = newPort;
}
2025-04-01 14:33:44 +02:00
};
2025-03-31 14:26:57 +02:00
editApiPort = async () => {
2025-04-01 14:33:44 +02:00
const newPort = await this.numberSelector.show(
this.config.ports.apiPort,
"API port",
false,
);
2025-03-31 14:26:57 +02:00
if (this.isInPortRange(newPort)) {
this.config.ports.apiPort = newPort;
}
2025-04-01 14:33:44 +02:00
};
2025-03-31 14:26:57 +02:00
isInPortRange = (number) => {
if (number < 1024 || number > 65535) {
this.ui.showErrorMessage("Port should be between 1024 and 65535.");
return false;
2025-04-01 14:33:44 +02:00
}
2025-03-31 14:26:57 +02:00
return true;
2025-04-01 14:33:44 +02:00
};
2025-03-31 14:26:57 +02:00
saveChangesAndExit = async () => {
2025-04-07 16:01:49 +02:00
if (this.config.dataDir !== this.originalDataDir) {
// The Codex data-dir is a little special.
// Use a dedicated module to move it.
await this.dataDirMover.moveDataDir(
this.originalDataDir,
this.config.dataDir,
);
}
2025-03-31 14:26:57 +02:00
this.configService.saveConfig();
this.ui.showInfoMessage("Configuration changes saved.");
2025-04-01 14:33:44 +02:00
this.loop.stopLoop();
};
2025-03-31 14:26:57 +02:00
discardChangesAndExit = async () => {
this.configService.loadConfig();
this.ui.showInfoMessage("Changes discarded.");
2025-04-01 14:33:44 +02:00
this.loop.stopLoop();
};
2025-03-31 14:26:57 +02:00
}