@sap/cli-core
Version:
Command-Line Interface (CLI) Core Module
54 lines (53 loc) • 1.83 kB
JavaScript
import { get } from "../../logger/index.js";
import { get as getConfig, set } from "../../config/index.js";
import { AuthenticationMethod } from "../../constants.js";
const REGEX_LEADING_TRAILING_SINGLE_QUOTE = /^'.*'$/;
const getLogger = () => get("commands.handler.utils");
export const getBooleanOption = (value) => !!value;
const decodeURIComponentInt = (value) => {
const { error } = getLogger();
try {
return decodeURIComponent(value);
}
catch (err) {
error(`Failed to decode URI for value '${value}'`, err);
throw err;
}
};
export const replaceLeadingTrailingSingleQuotes = (value) => {
if (REGEX_LEADING_TRAILING_SINGLE_QUOTE.test(value)) {
return value.replace(/^'/, "").replace(/'$/, "");
}
return value;
};
export const parseOption = (value) => {
if (typeof value !== "string") {
return value;
}
let v = value.trim();
v = decodeURIComponentInt(v);
v = replaceLeadingTrailingSingleQuotes(v);
return v;
};
export const setAuthenticationMethod = (authenticationMethod) => {
set({ authenticationMethod });
};
export const getAuthenticationMethod = () => getConfig().authenticationMethod;
export const setTargetHost = (targetHost) => {
set({ targetHost });
};
export const getTargetHost = () => {
const config = getConfig();
if (!config.targetHost) {
throw new Error("no target host set");
}
if (config.hostSetFromCustomConfig) {
const authenticationMethod = getAuthenticationMethod();
if (authenticationMethod === AuthenticationMethod.oauth) {
const { warn } = getLogger();
warn("Are you sure you want to use a custom configuration in combination with OAuth-based authentication?");
}
return config.host;
}
return config.targetHost;
};