@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
128 lines (127 loc) • 5.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getJobOnlyEnvVarsResolved = exports.getEnvVarsResolved = exports.getGitlabVar = exports.getEnvironment = exports.getAllPipelineContexts = exports.getAllComponentsWithAllEnvsHierarchical = exports.getAllComponentsWithAllEnvsFlat = exports.getPipelineContextByChoice = exports.getProjectComponents = exports.getProjectConfig = exports.parseChoice = void 0;
const pipeline_1 = require("../../../pipeline/src/index.js");
const gitlab_1 = require("../utils/gitlab");
const projects_1 = require("../utils/projects");
var parseChoice_1 = require("./parseChoice");
Object.defineProperty(exports, "parseChoice", {
enumerable: true,
get: function () {
return parseChoice_1.parseChoice;
}
});
let currentConfig = null;
const loadConfig = async () => {
const gitRoot = await (0, projects_1.getGitRoot)();
if (!gitRoot) {
return;
}
const result = await (0, pipeline_1.readConfig)(gitRoot);
if (!result) {
return;
}
currentConfig = result.config;
};
const getProjectConfig = async () => {
if (!currentConfig) {
await loadConfig();
}
return currentConfig;
};
exports.getProjectConfig = getProjectConfig;
const getProjectComponents = async () => {
const config = await (0, exports.getProjectConfig)();
if (!config) return [];
return Object.keys(config.components);
};
exports.getProjectComponents = getProjectComponents;
const getPipelineContextByChoice = async (env, componentName) => {
const config = await (0, exports.getProjectConfig)();
return await (0, pipeline_1.createComponentContext)({
config,
componentName,
env
});
};
exports.getPipelineContextByChoice = getPipelineContextByChoice;
const getAllComponentsWithAllEnvsFlat = async () => {
const config = await (0, exports.getProjectConfig)();
if (!config) {
return [];
}
return getAllComponentsWithAllEnvsFlatFromConfig(config);
};
exports.getAllComponentsWithAllEnvsFlat = getAllComponentsWithAllEnvsFlat;
const getAllComponentsWithAllEnvsHierarchical = async () => {
const config = await (0, exports.getProjectConfig)();
if (!config) {
return {};
}
return Object.fromEntries(Object.keys(config.components).map(componentName => [componentName, (0, pipeline_1.getAllEnvs)(config, componentName)]));
};
exports.getAllComponentsWithAllEnvsHierarchical = getAllComponentsWithAllEnvsHierarchical;
const getAllPipelineContexts = async (onlyComponent, {
includeLocal = false
} = {}) => {
const onlyComponentsArray = onlyComponent ? Array.isArray(onlyComponent) ? onlyComponent : [onlyComponent] : null;
return Promise.all((await (0, exports.getAllComponentsWithAllEnvsFlat)()).filter(c => includeLocal || c.env !== "local").filter(c => !onlyComponentsArray || onlyComponentsArray.includes(c.componentName)).map(({
env,
componentName
}) => (0, exports.getPipelineContextByChoice)(env, componentName)));
};
exports.getAllPipelineContexts = getAllPipelineContexts;
const getEnvironment = async (env, componentName) => {
const config = await (0, exports.getProjectConfig)();
return (0, pipeline_1.getEnvironment)({
config,
componentName,
env
});
};
exports.getEnvironment = getEnvironment;
const getGitlabVar = async (io, env, componentName, variableName) => {
const rawVariableName = (0, pipeline_1.getSecretVarName)(env, componentName, variableName);
return await (0, gitlab_1.getVariableValueByRawName)(io, rawVariableName);
};
exports.getGitlabVar = getGitlabVar;
const resolveSecrets = async (io, varSets) => {
const allVariablesInGitlab = await (0, gitlab_1.getAllVariables)(io);
return Object.fromEntries(varSets.flatMap(({
envVars,
secretEnvVarKeys
}) => Object.entries(envVars).filter(([key, value]) => {
var _a;
return value !== undefined && value !== null && !((_a = secretEnvVarKeys.find(k => k.key === key)) === null || _a === void 0 ? void 0 : _a.hidden);
}).map(([key, value]) => [key, allVariablesInGitlab.reduce((acc, curr) => acc.replace(new RegExp("\\$" + curr.key, "g"), curr.value), `${value}`)])));
};
const getEnvVarsResolved = async (io, env, componentName) => {
if (!componentName) {
return {};
}
const envionment = await (0, exports.getEnvironment)(env, componentName);
// in the pipeline the secrets alreadyy exists and bash will expand them
// but here we need to manually load them
return resolveSecrets(io, [{
envVars: envionment.envVars,
secretEnvVarKeys: envionment.secretEnvVarKeys
}]);
};
exports.getEnvVarsResolved = getEnvVarsResolved;
/**
*
* is used to get job only vars that should also be editable locally with catladder.
*/
const getJobOnlyEnvVarsResolved = async (io, env, componentName) => {
const envionment = await (0, exports.getEnvironment)(env, componentName);
return resolveSecrets(io, [envionment.jobOnlyVars.build, envionment.jobOnlyVars.deploy]);
};
exports.getJobOnlyEnvVarsResolved = getJobOnlyEnvVarsResolved;
function getAllComponentsWithAllEnvsFlatFromConfig(config) {
return Object.keys(config.components).flatMap(componentName => (0, pipeline_1.getAllEnvs)(config, componentName).map(env => ({
env,
componentName
})));
}