mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 15:24:52 +09:00
Ctl ended up calling its main class for "Control" for historical
reasons, along with its config file and plugin entrypoint.
The plugin entrypoint was renamed to ctl in 808eda0513. This
finishes that renaming by also renaming the main class, default config
file, config-control command, and main entrypoint.
The config-control command is renamed to config to be consistent with
the host and controller's config commands.
The term "control" is still kept in the config and message locations as
a common specifier for both ctl and web_ui.
101 lines
3.3 KiB
TypeScript
101 lines
3.3 KiB
TypeScript
import asTableModule from "as-table";
|
|
|
|
import * as lib from "@clusterio/lib";
|
|
import { logger } from "@clusterio/lib";
|
|
import type { Ctl } from "../ctl.js";
|
|
import { print } from "./command_ops.js";
|
|
|
|
const asTable = asTableModule.configure({ delimiter: " | " });
|
|
|
|
export const roleCommands = new lib.CommandTree({ name: "role", description: "Role management" });
|
|
roleCommands.add(new lib.Command({
|
|
definition: [["list", "l"], "List roles in the cluster"],
|
|
handler: async function(args: object, ctl: Ctl) {
|
|
let roles = await ctl.send(new lib.RoleListRequest());
|
|
print(asTable(roles));
|
|
},
|
|
}));
|
|
|
|
roleCommands.add(new lib.Command({
|
|
definition: ["create <name>", "Create a new role", (yargs) => {
|
|
yargs.positional("name", { describe: "Name of role to create", type: "string" });
|
|
yargs.options({
|
|
"description": { describe: "Description for role", nargs: 1, type: "string", default: "" },
|
|
"permissions": { describe: "Permissions role grants", nargs: 1, array: true, type: "string", default: [] },
|
|
});
|
|
}],
|
|
handler: async function(args: { name: string, description: string, permissions: string[] }, ctl: Ctl) {
|
|
let id = await ctl.send(new lib.RoleCreateRequest(
|
|
args.name,
|
|
args.description,
|
|
args.permissions,
|
|
));
|
|
logger.info(`Created role ID ${id}`);
|
|
},
|
|
}));
|
|
|
|
roleCommands.add(new lib.Command({
|
|
definition: ["edit <role>", "Edit existing role", (yargs) => {
|
|
yargs.positional("role", { describe: "Role to edit", type: "string" });
|
|
yargs.options({
|
|
"name": { describe: "New name for role", nargs: 1, type: "string" },
|
|
"description": { describe: "New description for role", nargs: 1, type: "string" },
|
|
"set-perms": { describe: "Set permissions for role", array: true, type: "string" },
|
|
"add-perms": { describe: "Add permissions to role", array: true, type: "string", conflicts: "set-perms" },
|
|
"remove-perms": {
|
|
describe: "Remove permissions from role", array: true, type: "string", conflicts: "set-perms",
|
|
},
|
|
"grant-default": { describe: "Add default permissions to role", nargs: 0, type: "boolean" },
|
|
});
|
|
}],
|
|
handler: async function(
|
|
args: {
|
|
role: string,
|
|
name?: string,
|
|
description?: string,
|
|
setPerms?: string[],
|
|
addPerms?: string[],
|
|
removePerms?: string[],
|
|
grantDefault?: boolean,
|
|
},
|
|
ctl: Ctl
|
|
) {
|
|
let role = await lib.retrieveRole(ctl, args.role);
|
|
|
|
if (args.name !== undefined) {
|
|
role.name = args.name;
|
|
}
|
|
if (args.description !== undefined) {
|
|
role.description = args.description;
|
|
}
|
|
if (args.addPerms) {
|
|
for (const perm of args.addPerms) {
|
|
role.permissions.add(perm);
|
|
}
|
|
}
|
|
if (args.removePerms) {
|
|
for (let perm of args.removePerms) {
|
|
role.permissions.delete(perm);
|
|
}
|
|
}
|
|
if (args.setPerms !== undefined) {
|
|
role.permissions = new Set(args.setPerms);
|
|
}
|
|
await ctl.send(new lib.RoleUpdateRequest(role.id, role.name, role.description, [...role.permissions]));
|
|
|
|
if (args.grantDefault) {
|
|
await ctl.send(new lib.RoleGrantDefaultPermissionsRequest(role.id));
|
|
}
|
|
},
|
|
}));
|
|
|
|
roleCommands.add(new lib.Command({
|
|
definition: ["delete <role>", "Delete role", (yargs) => {
|
|
yargs.positional("role", { describe: "Role to delete", type: "string" });
|
|
}],
|
|
handler: async function(args: { role: string }, ctl: Ctl) {
|
|
let role = await lib.retrieveRole(ctl, args.role);
|
|
await ctl.send(new lib.RoleDeleteRequest(role.id));
|
|
},
|
|
}));
|