UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

64 lines (63 loc) 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTargetHost = exports.setTargetHost = exports.getAuthenticationMethod = exports.setAuthenticationMethod = exports.parseOption = exports.replaceLeadingTrailingSingleQuotes = exports.getBooleanOption = void 0; const logger_1 = require("../../logger"); const config_1 = require("../../config"); const constants_1 = require("../../constants"); const REGEX_LEADING_TRAILING_SINGLE_QUOTE = /^'.*'$/; const getLogger = () => (0, logger_1.get)("commands.handler.utils"); const getBooleanOption = (value) => !!value; exports.getBooleanOption = getBooleanOption; const decodeURIComponentInt = (value) => { const { error } = getLogger(); try { return decodeURIComponent(value); } catch (err) { error(`Failed to decode URI for value '${value}'`, err); throw err; } }; const replaceLeadingTrailingSingleQuotes = (value) => { if (REGEX_LEADING_TRAILING_SINGLE_QUOTE.test(value)) { return value.replace(/^'/, "").replace(/'$/, ""); } return value; }; exports.replaceLeadingTrailingSingleQuotes = replaceLeadingTrailingSingleQuotes; const parseOption = (value) => { if (typeof value !== "string") { return value; } let v = value.trim(); v = decodeURIComponentInt(v); v = (0, exports.replaceLeadingTrailingSingleQuotes)(v); return v; }; exports.parseOption = parseOption; const setAuthenticationMethod = (authenticationMethod) => { (0, config_1.set)({ authenticationMethod }); }; exports.setAuthenticationMethod = setAuthenticationMethod; const getAuthenticationMethod = () => (0, config_1.get)().authenticationMethod; exports.getAuthenticationMethod = getAuthenticationMethod; const setTargetHost = (targetHost) => { (0, config_1.set)({ targetHost }); }; exports.setTargetHost = setTargetHost; const getTargetHost = () => { const config = (0, config_1.get)(); if (!config.targetHost) { throw new Error("no target host set"); } if (config.hostSetFromCustomConfig) { const authenticationMethod = (0, exports.getAuthenticationMethod)(); if (authenticationMethod === constants_1.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; }; exports.getTargetHost = getTargetHost;