@sap/cli-core
Version:
Command-Line Interface (CLI) Core Module
65 lines (64 loc) • 2.42 kB
JavaScript
import { get } from "../../config/index.js";
import { OPTION_HOST } from "../../constants.js";
import { get as getLoggerOrig } from "../../logger/index.js";
import { get as getSettings, remove, set as setSettings, } from "../../settings/index.js";
import { buildCommand } from "../../utils/commands.js";
import { getBin, getInfoFromTenant } from "../../utils/utils.js";
import { createNextHandler, createParseArgumentsHandler, } from "../handler/index.js";
const getLogger = () => getLoggerOrig("commands.host");
const set = async () => async () => {
const config = get();
try {
// throws if host is not valid
getInfoFromTenant(config.arguments.host, config.verbose, false);
await setSettings(OPTION_HOST.longName, config.arguments.host);
}
catch (err) {
const { debug, output } = getLogger();
debug("failed to set host", err);
output(`error: invalid value '${config.arguments.host}' for argument '<host>'`);
throw new Error("failed to set host");
}
};
const show = async () => async () => {
const { output } = getLogger();
const settings = await getSettings();
if (settings[OPTION_HOST.longName]) {
output(settings[OPTION_HOST.longName]);
}
else {
output(`no global host set. use ${getBin()} config host set <host> to set a global host`);
throw new Error("no global host set");
}
};
const clean = async () => async () => {
await remove(OPTION_HOST.longName);
};
export const addCommands = async (program) => {
await buildCommand(program, {
type: "topCommand",
command: "host",
description: "configure host properties",
subCommands: [
{
type: "command",
command: "set",
description: "set global host",
args: [{ argument: "host", description: "global host" }],
handler: createNextHandler("command.config.host.set", createParseArgumentsHandler([{ argument: "host" }]), set),
},
{
type: "command",
command: "show",
description: "show global host",
handler: show,
},
{
type: "command",
command: "clean",
description: "clean global host",
handler: clean,
},
],
});
};