From 1a86d1e821ac90c868419785126bab190bc7d953 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 13:33:56 +0300 Subject: [PATCH 01/21] CR-17813 --- .../cli/commands/pipeline/run.base.js | 15 +++- .../cli/commands/pipeline/run.cmd.js | 10 ++- .../cli/commands/project/apply.cmd.js | 13 +--- .../cli/commands/project/create.cmd.js | 71 ++++++++----------- lib/interface/cli/helpers/general.js | 13 ++++ package.json | 4 +- 6 files changed, 70 insertions(+), 56 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index 8b40ed323..5ffd3cd6d 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -1,6 +1,6 @@ const _ = require('lodash'); const Promise = require('bluebird'); -const { prepareKeyValueFromCLIEnvOption } = require('../../helpers/general'); +const { prepareKeyValueFromCLIEnvOption, prepareEncryptedValues } = require('../../helpers/general'); const { validatePipelineYaml } = require('../../helpers/validation'); const { printResult } = require('../root/validate.cmd'); const CFError = require('cf-errors'); @@ -63,7 +63,9 @@ class RunBaseCommand { this.executionRequests.push(request); }); } else { - const variables = prepareKeyValueFromCLIEnvOption(this.argv.variable); + const variables = prepareEncryptedValues(this.argv.variable, this.argv.encrypted); + // const variables = prepareKeyValueFromCLIEnvOption(this.argv.variable); + // const variables = this.argv.variable; const request = _.cloneDeep(executionRequestTemplate); request.options.variables = variables; request.options.contexts = contexts; @@ -90,6 +92,15 @@ class RunBaseCommand { const pipelineName = this.argv.name; const userYamlDescriptor = this.argv.yaml; const contexts = this.argv.context; + // prepareEncryptedValues + // const variableMap = _.reduce(this.argv.variable, (acc, v) => _.assign(acc, { [v.key]: v }), {}); + // _.forEach(this.argv.encrypted, (varName) => { + // const variable = variableMap[varName]; + // if (!variable) { + // throw new CFError(`Variable is not provided: "${varName}"`); + // } + // variable.encrypted = true; + // }); if (!pipelineName) { if (!userYamlDescriptor) { throw new CFError({ diff --git a/lib/interface/cli/commands/pipeline/run.cmd.js b/lib/interface/cli/commands/pipeline/run.cmd.js index 7a63157ff..e69a9e53a 100644 --- a/lib/interface/cli/commands/pipeline/run.cmd.js +++ b/lib/interface/cli/commands/pipeline/run.cmd.js @@ -1,6 +1,5 @@ -const debug = require('debug')('codefresh:cli:run:pipeline'); const Command = require('../../Command'); -const { crudFilenameOption } = require('../../helpers/general'); +const { crudFilenameOption, prepareKeyValueObjectsFromCLIEnvOption } = require('../../helpers/general'); const RunLocalCommand = require('./run.local'); const RunExternalCommand = require('./run.cf'); @@ -85,6 +84,13 @@ const run = new Command({ describe: 'Set build variables', default: [], alias: 'v', + coerce: prepareKeyValueObjectsFromCLIEnvOption, + }) + .option('encrypted', { + array: true, + alias: 'e', + describe: 'Variable names to encrypt', + default: [], }) .option('detach', { alias: 'd', diff --git a/lib/interface/cli/commands/project/apply.cmd.js b/lib/interface/cli/commands/project/apply.cmd.js index 9e574c4f0..3185cf16c 100644 --- a/lib/interface/cli/commands/project/apply.cmd.js +++ b/lib/interface/cli/commands/project/apply.cmd.js @@ -4,7 +4,7 @@ const _ = require('lodash'); const { sdk } = require('../../../../logic'); const applyRoot = require('../root/apply.cmd'); -const { prepareKeyValueObjectsFromCLIEnvOption, ignoreHttpError } = require('../../helpers/general'); +const { prepareKeyValueObjectsFromCLIEnvOption, ignoreHttpError, prepareEncryptedValues} = require('../../helpers/general'); const command = new Command({ command: 'project [id|name]', @@ -57,18 +57,11 @@ const command = new Command({ const { newName: projectName, tag: tags, - variable: variables, + variable, encrypted, } = argv; - const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); - _.forEach(encrypted, (varName) => { - const variable = variableMap[varName]; - if (!variable) { - throw new CFError(`Variable is not provided: "${varName}"`); - } - variable.encrypted = true; - }); + const variables = prepareEncryptedValues(variable, encrypted); let project = await sdk.projects.get({ id }).catch(ignoreHttpError); project = project || await sdk.projects.getByName({ name }).catch(ignoreHttpError); diff --git a/lib/interface/cli/commands/project/create.cmd.js b/lib/interface/cli/commands/project/create.cmd.js index eb0371539..46e324468 100644 --- a/lib/interface/cli/commands/project/create.cmd.js +++ b/lib/interface/cli/commands/project/create.cmd.js @@ -1,10 +1,10 @@ -const Command = require('../../Command'); const CFError = require('cf-errors'); const _ = require('lodash'); +const Command = require('../../Command'); const { sdk } = require('../../../../logic'); const createRoot = require('../root/create.cmd'); const { checkOrProjectExists } = require('../../helpers/validation'); -const { prepareKeyValueObjectsFromCLIEnvOption } = require('../../helpers/general'); +const { prepareKeyValueObjectsFromCLIEnvOption, prepareEncryptedValues } = require('../../helpers/general'); const command = new Command({ command: 'project ', @@ -15,51 +15,42 @@ const command = new Command({ category: 'Projects', title: 'Create Project', }, - builder: (yargs) => { - return yargs - .positional('name', { - describe: 'Name of project', - }) - .option('tag', { - array: true, - alias: 't', - describe: 'Project tags', - default: [], - }) - .option('variable', { - array: true, - alias: 'v', - describe: 'Project variables', - default: [], - coerce: prepareKeyValueObjectsFromCLIEnvOption, - }) - .option('encrypted', { - array: true, - alias: 'e', - describe: 'Variable names to encrypt', - default: [], - }) - .example('codefresh create project NAME', 'Create a project') - .example('codefresh create project NAME -t test -t run', 'Create a project with tags: [ "test", "run"]') - .example('codefresh create project NAME -v test=true -v run=false', 'Create a project with specific variables') - .example('codefresh create project NAME -v secret=secret -e secret', 'Create a project with encrypted variables'); - }, + builder: (yargs) => yargs + .positional('name', { + describe: 'Name of project', + }) + .option('tag', { + array: true, + alias: 't', + describe: 'Project tags', + default: [], + }) + .option('variable', { + array: true, + alias: 'v', + describe: 'Project variables', + default: [], + coerce: prepareKeyValueObjectsFromCLIEnvOption, + }) + .option('encrypted', { + array: true, + alias: 'e', + describe: 'Variable names to encrypt', + default: [], + }) + .example('codefresh create project NAME', 'Create a project') + .example('codefresh create project NAME -t test -t run', 'Create a project with tags: [ "test", "run"]') + .example('codefresh create project NAME -v test=true -v run=false', 'Create a project with specific variables') + .example('codefresh create project NAME -v secret=secret -e secret', 'Create a project with encrypted variables'), handler: async (argv) => { const { name: projectName, tag: tags, - variable: variables, + variable, encrypted, } = argv; - const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); - _.forEach(encrypted, (varName) => { - const variable = variableMap[varName]; - if (!variable) { - throw new CFError(`Variable is not provided: "${varName}"`); - } - variable.encrypted = true; - }); + const variables = prepareEncryptedValues(variable, encrypted); await checkOrProjectExists(projectName); await sdk.projects.create({ projectName, tags, variables }); diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 3becbc692..90ea6e8c5 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -106,6 +106,18 @@ const prepareKeyValueObjectsFromCLIEnvOption = (environmentVariables) => { return variables; }; +const prepareEncryptedValues = (variables, encrypted) => { + const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); + _.forEach(encrypted, (varName) => { + const variable = variableMap[varName]; + if (!variable) { + throw new CFError(`Variable is not provided: "${varName}"`); + } + variable.encrypted = true; + }); + return variables; +}; + const crudFilenameOption = (yargs, options = {}) => { const filenameOption = { alias: options.alias || 'f', @@ -232,4 +244,5 @@ module.exports = { detectProxy, keyValueArrayToObject, addProxyVariables, + prepareEncryptedValues, }; diff --git a/package.json b/package.json index b53a74aef..c0b4ae3da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.82.7", + "version": "0.82.8", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true, @@ -115,4 +115,4 @@ "./test-setup.js" ] } -} \ No newline at end of file +} From d2e9356568c1033cae8346bea31ab56b4c10a2fb Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 13:36:56 +0300 Subject: [PATCH 02/21] WIP --- .../cli/commands/pipeline/run.base.js | 11 ---- .../cli/commands/project/create.cmd.js | 58 +++++++++---------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index 5ffd3cd6d..a55fa609d 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -64,8 +64,6 @@ class RunBaseCommand { }); } else { const variables = prepareEncryptedValues(this.argv.variable, this.argv.encrypted); - // const variables = prepareKeyValueFromCLIEnvOption(this.argv.variable); - // const variables = this.argv.variable; const request = _.cloneDeep(executionRequestTemplate); request.options.variables = variables; request.options.contexts = contexts; @@ -92,15 +90,6 @@ class RunBaseCommand { const pipelineName = this.argv.name; const userYamlDescriptor = this.argv.yaml; const contexts = this.argv.context; - // prepareEncryptedValues - // const variableMap = _.reduce(this.argv.variable, (acc, v) => _.assign(acc, { [v.key]: v }), {}); - // _.forEach(this.argv.encrypted, (varName) => { - // const variable = variableMap[varName]; - // if (!variable) { - // throw new CFError(`Variable is not provided: "${varName}"`); - // } - // variable.encrypted = true; - // }); if (!pipelineName) { if (!userYamlDescriptor) { throw new CFError({ diff --git a/lib/interface/cli/commands/project/create.cmd.js b/lib/interface/cli/commands/project/create.cmd.js index 46e324468..fc65d80d5 100644 --- a/lib/interface/cli/commands/project/create.cmd.js +++ b/lib/interface/cli/commands/project/create.cmd.js @@ -1,5 +1,3 @@ -const CFError = require('cf-errors'); -const _ = require('lodash'); const Command = require('../../Command'); const { sdk } = require('../../../../logic'); const createRoot = require('../root/create.cmd'); @@ -15,33 +13,35 @@ const command = new Command({ category: 'Projects', title: 'Create Project', }, - builder: (yargs) => yargs - .positional('name', { - describe: 'Name of project', - }) - .option('tag', { - array: true, - alias: 't', - describe: 'Project tags', - default: [], - }) - .option('variable', { - array: true, - alias: 'v', - describe: 'Project variables', - default: [], - coerce: prepareKeyValueObjectsFromCLIEnvOption, - }) - .option('encrypted', { - array: true, - alias: 'e', - describe: 'Variable names to encrypt', - default: [], - }) - .example('codefresh create project NAME', 'Create a project') - .example('codefresh create project NAME -t test -t run', 'Create a project with tags: [ "test", "run"]') - .example('codefresh create project NAME -v test=true -v run=false', 'Create a project with specific variables') - .example('codefresh create project NAME -v secret=secret -e secret', 'Create a project with encrypted variables'), + builder: (yargs) => { + return yargs + .positional('name', { + describe: 'Name of project', + }) + .option('tag', { + array: true, + alias: 't', + describe: 'Project tags', + default: [], + }) + .option('variable', { + array: true, + alias: 'v', + describe: 'Project variables', + default: [], + coerce: prepareKeyValueObjectsFromCLIEnvOption, + }) + .option('encrypted', { + array: true, + alias: 'e', + describe: 'Variable names to encrypt', + default: [], + }) + .example('codefresh create project NAME', 'Create a project') + .example('codefresh create project NAME -t test -t run', 'Create a project with tags: [ "test", "run"]') + .example('codefresh create project NAME -v test=true -v run=false', 'Create a project with specific variables') + .example('codefresh create project NAME -v secret=secret -e secret', 'Create a project with encrypted variables'); + }, handler: async (argv) => { const { name: projectName, From 05113ec367187067d7d0c10fd342dd794a62580c Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 16:38:17 +0300 Subject: [PATCH 03/21] WIP --- .../cli/commands/pipeline/run.base.js | 25 +++++++++---------- lib/interface/cli/helpers/general.js | 13 ++++++---- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index a55fa609d..a57585170 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -1,9 +1,9 @@ const _ = require('lodash'); const Promise = require('bluebird'); -const { prepareKeyValueFromCLIEnvOption, prepareEncryptedValues } = require('../../helpers/general'); +const CFError = require('cf-errors'); +const { prepareKeyValueFromCLIEnvOption, prepareEncryptedValues, prepareKeyValueObjectsFromEnvFileOption } = require('../../helpers/general'); const { validatePipelineYaml } = require('../../helpers/validation'); const { printResult } = require('../root/validate.cmd'); -const CFError = require('cf-errors'); const { sdk } = require('../../../../logic'); class RunBaseCommand { @@ -56,21 +56,20 @@ class RunBaseCommand { }, }; + let requestVariables = []; if (variablesFromFile) { - _.forEach(variablesFromFile, (variables) => { - const request = _.cloneDeep(executionRequestTemplate); - request.options.variables = variables; - this.executionRequests.push(request); - }); + requestVariables = prepareKeyValueObjectsFromEnvFileOption(variablesFromFile); } else { - const variables = prepareEncryptedValues(this.argv.variable, this.argv.encrypted); - const request = _.cloneDeep(executionRequestTemplate); - request.options.variables = variables; - request.options.contexts = contexts; - this.executionRequests.push(request); + requestVariables = this.argv.variable; } - const results = await Promise.all(this.executionRequests.map(request => this.runImpl(request))); + const variables = prepareEncryptedValues(requestVariables, this.argv.encrypted); + const request = _.cloneDeep(executionRequestTemplate); + request.options.variables = variables; + request.options.contexts = contexts; + this.executionRequests.push(request); + + const results = await Promise.all(this.executionRequests.map((request) => this.runImpl(request))); const findMaxReducer = (accumulator, currentValue) => (currentValue > accumulator ? currentValue : accumulator); const exitCode = results.reduce(findMaxReducer); await this.postRunRequest(); diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 90ea6e8c5..6455c369c 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -2,9 +2,9 @@ const Promise = require('bluebird'); const _ = require('lodash'); const fs = require('fs'); const yaml = require('js-yaml'); -const defaults = require('../defaults'); const CFError = require('cf-errors'); const path = require('path'); +const defaults = require('../defaults'); const Output = require('../../../output/Output'); const { sdk } = require('../../../logic'); @@ -106,6 +106,8 @@ const prepareKeyValueObjectsFromCLIEnvOption = (environmentVariables) => { return variables; }; +const prepareKeyValueObjectsFromEnvFileOption = (environmentVariables) => _.map(environmentVariables, (value, key) => ({ key, value })); + const prepareEncryptedValues = (variables, encrypted) => { const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); _.forEach(encrypted, (varName) => { @@ -173,15 +175,15 @@ const selectColumnsOption = (yargs, options = {}) => { }; function pathExists(p) { - return new Promise(resolve => fs.access(p, resolve)) - .then(err => !err); + return new Promise((resolve) => fs.access(p, resolve)) + .then((err) => !err); } const readFile = Promise.promisify(fs.readFile); function watchFile(filename, listener) { fs.watchFile(filename, { interval: 500 }, listener); - const unwatcher = f => () => fs.unwatchFile(f); + const unwatcher = (f) => () => fs.unwatchFile(f); ['exit', 'SIGINT', 'SIGUSR1', 'SIGUSR2', 'uncaughtException', 'SIGTERM'].forEach((eventType) => { process.on(eventType, unwatcher(filename)); }); @@ -189,7 +191,7 @@ function watchFile(filename, listener) { function ignoreHttpError(e) { if (!e.statusCode) { - throw e + throw e; } return undefined; } @@ -245,4 +247,5 @@ module.exports = { keyValueArrayToObject, addProxyVariables, prepareEncryptedValues, + prepareKeyValueObjectsFromEnvFileOption, }; From 4c3b68f8279c1536b5daf9fc437cae5352a18e87 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 17:00:12 +0300 Subject: [PATCH 04/21] WIP --- .../cli/commands/pipeline/run.base.js | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index a57585170..9d11b7b3f 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -56,20 +56,21 @@ class RunBaseCommand { }, }; - let requestVariables = []; if (variablesFromFile) { - requestVariables = prepareKeyValueObjectsFromEnvFileOption(variablesFromFile); + _.forEach(variablesFromFile, (variables) => { + const request = _.cloneDeep(executionRequestTemplate); + request.options.variables = variables; + this.executionRequests.push(request); + }); } else { - requestVariables = this.argv.variable; + const variables = prepareKeyValueFromCLIEnvOption(this.argv.variable); + const request = _.cloneDeep(executionRequestTemplate); + request.options.variables = variables; + request.options.contexts = contexts; + this.executionRequests.push(request); } - const variables = prepareEncryptedValues(requestVariables, this.argv.encrypted); - const request = _.cloneDeep(executionRequestTemplate); - request.options.variables = variables; - request.options.contexts = contexts; - this.executionRequests.push(request); - - const results = await Promise.all(this.executionRequests.map((request) => this.runImpl(request))); + const results = await Promise.all(this.executionRequests.map(request => this.runImpl(request))); const findMaxReducer = (accumulator, currentValue) => (currentValue > accumulator ? currentValue : accumulator); const exitCode = results.reduce(findMaxReducer); await this.postRunRequest(); From 78ff0f28e578361a60c2b95134d96d94854f0c48 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 17:00:57 +0300 Subject: [PATCH 05/21] WIP --- lib/interface/cli/helpers/general.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 6455c369c..a853141c9 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -106,8 +106,6 @@ const prepareKeyValueObjectsFromCLIEnvOption = (environmentVariables) => { return variables; }; -const prepareKeyValueObjectsFromEnvFileOption = (environmentVariables) => _.map(environmentVariables, (value, key) => ({ key, value })); - const prepareEncryptedValues = (variables, encrypted) => { const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); _.forEach(encrypted, (varName) => { From eb04072f3b8feacb57cc0231611109ae9f578b89 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 17:03:15 +0300 Subject: [PATCH 06/21] WIP --- lib/interface/cli/commands/pipeline/run.base.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index 9d11b7b3f..b7d72aafd 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -1,7 +1,7 @@ const _ = require('lodash'); const Promise = require('bluebird'); const CFError = require('cf-errors'); -const { prepareKeyValueFromCLIEnvOption, prepareEncryptedValues, prepareKeyValueObjectsFromEnvFileOption } = require('../../helpers/general'); +const { prepareKeyValueFromCLIEnvOption, prepareEncryptedValues } = require('../../helpers/general'); const { validatePipelineYaml } = require('../../helpers/validation'); const { printResult } = require('../root/validate.cmd'); const { sdk } = require('../../../../logic'); @@ -63,7 +63,7 @@ class RunBaseCommand { this.executionRequests.push(request); }); } else { - const variables = prepareKeyValueFromCLIEnvOption(this.argv.variable); + const variables = prepareEncryptedValues(this.argv.variable); const request = _.cloneDeep(executionRequestTemplate); request.options.variables = variables; request.options.contexts = contexts; From 5286d357876129f0148c2ac326381d97e6fc4af8 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 17:56:06 +0300 Subject: [PATCH 07/21] WIP --- lib/interface/cli/commands/pipeline/run.base.js | 4 ++-- lib/interface/cli/helpers/general.js | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index b7d72aafd..db448d303 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -63,14 +63,14 @@ class RunBaseCommand { this.executionRequests.push(request); }); } else { - const variables = prepareEncryptedValues(this.argv.variable); + const variables = prepareEncryptedValues(this.argv.variable, this.argv.encrypted); const request = _.cloneDeep(executionRequestTemplate); request.options.variables = variables; request.options.contexts = contexts; this.executionRequests.push(request); } - const results = await Promise.all(this.executionRequests.map(request => this.runImpl(request))); + const results = await Promise.all(this.executionRequests.map((request) => this.runImpl(request))); const findMaxReducer = (accumulator, currentValue) => (currentValue > accumulator ? currentValue : accumulator); const exitCode = results.reduce(findMaxReducer); await this.postRunRequest(); diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index a853141c9..a193d60aa 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -245,5 +245,4 @@ module.exports = { keyValueArrayToObject, addProxyVariables, prepareEncryptedValues, - prepareKeyValueObjectsFromEnvFileOption, }; From 0fb6a7daa3a04ad62b3ee639bb613851a9e01fcf Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 21:46:20 +0300 Subject: [PATCH 08/21] WIP --- lib/interface/cli/commands/pipeline/run.base.js | 6 ++++-- lib/interface/cli/helpers/general.js | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index db448d303..b0f87d490 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -1,7 +1,9 @@ const _ = require('lodash'); const Promise = require('bluebird'); const CFError = require('cf-errors'); -const { prepareKeyValueFromCLIEnvOption, prepareEncryptedValues } = require('../../helpers/general'); +const { prepareKeyValueFromCLIEnvOption, + prepareEncryptedValues, + prepareKeyValueObjectsFromEnvFileOption } = require('../../helpers/general'); const { validatePipelineYaml } = require('../../helpers/validation'); const { printResult } = require('../root/validate.cmd'); const { sdk } = require('../../../../logic'); @@ -59,7 +61,7 @@ class RunBaseCommand { if (variablesFromFile) { _.forEach(variablesFromFile, (variables) => { const request = _.cloneDeep(executionRequestTemplate); - request.options.variables = variables; + request.options.variables = prepareKeyValueObjectsFromEnvFileOption(variables); this.executionRequests.push(request); }); } else { diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index a193d60aa..e77428f69 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -106,6 +106,14 @@ const prepareKeyValueObjectsFromCLIEnvOption = (environmentVariables) => { return variables; }; +const prepareKeyValueObjectsFromEnvFileOption = (environmentVariables) => _.map(environmentVariables, (value, key) => { + if (_.isObject(value)) { + const { val, encrypted } = value; + return { key, value: val, encrypted }; + } + return { key, value }; +}); + const prepareEncryptedValues = (variables, encrypted) => { const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); _.forEach(encrypted, (varName) => { @@ -245,4 +253,5 @@ module.exports = { keyValueArrayToObject, addProxyVariables, prepareEncryptedValues, + prepareKeyValueObjectsFromEnvFileOption, }; From 5440cc5bdb9d91533fd18f337052f1fe2e4ac14c Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 22:36:57 +0300 Subject: [PATCH 09/21] WIP --- lib/interface/cli/helpers/general.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index e77428f69..2e18e345c 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -146,7 +146,11 @@ const crudFilenameOption = (yargs, options = {}) => { return options.raw ? rawFile : JSON.parse(rawFile); } if (arg.endsWith('.yml') || arg.endsWith('yaml')) { - return options.raw ? rawFile : yaml.safeLoad(rawFile); + const content = options.raw ? rawFile : yaml.safeLoad(rawFile); + if (_.isObject(content)) { + return content; + } + throw new CFError('Not a valid Yaml file'); } throw new CFError('File extension is not recognized'); } catch (err) { From 896537137ae54d3cb0c25524488d3cd314f10d29 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Mon, 17 Apr 2023 23:54:23 +0300 Subject: [PATCH 10/21] add tests --- .../commands/pipeline/pipeline.sdk.spec.js | 79 +++++++++++++++++-- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js b/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js index 883d72618..6c44cc525 100644 --- a/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js +++ b/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js @@ -1,3 +1,6 @@ +const yaml = require('js-yaml'); +const request = require('requestretry'); +const fs = require('fs'); const DEFAULTS = require('../../defaults'); const getCmd = require('./get.cmd').toCommand(); const deleteCmd = require('./delete.cmd').toCommand(); @@ -10,18 +13,16 @@ jest.mock('../../helpers/validation'); // eslint-disable-line jest.mock('../../../../../check-version'); jest.mock('../../completion/helpers', () => { // eslint-disable-line return { - authContextWrapper: func => func, + authContextWrapper: (func) => func, }; }); jest.mock('../../../../logic/entities/Pipeline', () => { // eslint-disable-line return { - fromResponse: res => res, + fromResponse: (res) => res, }; }); -const request = require('requestretry'); - const DEFAULT_RESPONSE = request.__defaultResponse(); describe('pipeline', () => { @@ -57,11 +58,11 @@ describe('pipeline', () => { }); it('should return default limit', async () => { - expect(_getLimit(undefined,false)).toEqual(DEFAULTS.GET_LIMIT_RESULTS); + expect(_getLimit(undefined, false)).toEqual(DEFAULTS.GET_LIMIT_RESULTS); }); it('should return `unlimited` value', async () => { - expect(_getLimit(undefined,true)).toEqual(DEFAULTS.GET_ALL_PIPELINES_LIMIT); + expect(_getLimit(undefined, true)).toEqual(DEFAULTS.GET_ALL_PIPELINES_LIMIT); }); }); @@ -84,6 +85,72 @@ describe('pipeline', () => { }); }); + describe('run', () => { + it('should handle running pipeline with encrypted variables', async () => { + const argv = { name: 'some name', + detach: true, + variable: + [{ + key: 'secret', + value: 'secret', + }, + { + key: 'VAR1', + value: 'VAL1', + }, + ], + encrypted: ['secret'], + }; + const pip = new CfPipeline(argv); + await pip.run(); + expect(pip.executionRequests[0].options.variables).toEqual([ + { + key: 'secret', + value: 'secret', + encrypted: true, + }, + { + key: 'VAR1', + value: 'VAL1', + }, + ]); + await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line + }); + + it('should handle running pipeline with encrypted variables passing inside json file', async () => { + const rawFile = fs.readFileSync('lib/interface/cli/commands/pipeline/var.json', 'utf8'); + + const argv = { name: 'some name', + detach: true, + 'var-file': JSON.parse(rawFile), + }; + const pip = new CfPipeline(argv); + await pip.run(); + expect(pip.executionRequests[0].options.variables).toEqual( + [{ key: 'help6', value: '85858' }, + { key: 'should_be_encrepted', value: '0000' }, + { encrypted: true, key: 'help7', value: 'test' }], + ); + await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line + }); + + it('should handle running pipeline with encrypted variables passing inside yaml file', async () => { + const rawFile = fs.readFileSync('lib/interface/cli/commands/pipeline/var.yml', 'utf8'); + + const argv = { name: 'some name', + detach: true, + 'var-file': yaml.safeLoad(rawFile), + }; + const pip = new CfPipeline(argv); + await pip.run(); + expect(pip.executionRequests[0].options.variables).toEqual( + [{ key: 'VAR1', value: 'VAL1' }, + { encrypted: true, key: 'VAR2', value: 'VAL2' }], + ); + await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line + }); + }); + describe('runImpl', () => { it('should handle running pipeline', async () => { const argv = { name: 'some name', detach: true }; From 688772f676f5daaec7a9f9bb8c9811e612eeb03a Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 00:07:02 +0300 Subject: [PATCH 11/21] add tests files --- lib/interface/cli/commands/pipeline/var.json | 12 ++++++++++++ lib/interface/cli/commands/pipeline/var.yml | 5 +++++ 2 files changed, 17 insertions(+) create mode 100644 lib/interface/cli/commands/pipeline/var.json create mode 100644 lib/interface/cli/commands/pipeline/var.yml diff --git a/lib/interface/cli/commands/pipeline/var.json b/lib/interface/cli/commands/pipeline/var.json new file mode 100644 index 000000000..802da70b6 --- /dev/null +++ b/lib/interface/cli/commands/pipeline/var.json @@ -0,0 +1,12 @@ +{ + "build1": { + "help6": "85858", + "should_be_encrepted": "0000", + "help7": { + "val": "test", + "encrypted": true + } + } +} + + diff --git a/lib/interface/cli/commands/pipeline/var.yml b/lib/interface/cli/commands/pipeline/var.yml new file mode 100644 index 000000000..71b86b748 --- /dev/null +++ b/lib/interface/cli/commands/pipeline/var.yml @@ -0,0 +1,5 @@ +build1: + VAR1: 'VAL1' + VAR2: + val: VAL2 + encrypted: true From 2df5bace210f3f4e9b46407a9c7f2ae7e57569c9 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 00:12:03 +0300 Subject: [PATCH 12/21] wip --- lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js b/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js index 6c44cc525..5d662281b 100644 --- a/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js +++ b/lib/interface/cli/commands/pipeline/pipeline.sdk.spec.js @@ -89,6 +89,7 @@ describe('pipeline', () => { it('should handle running pipeline with encrypted variables', async () => { const argv = { name: 'some name', detach: true, + annotation: [], variable: [{ key: 'secret', @@ -122,6 +123,7 @@ describe('pipeline', () => { const argv = { name: 'some name', detach: true, + annotation: [], 'var-file': JSON.parse(rawFile), }; const pip = new CfPipeline(argv); @@ -139,6 +141,7 @@ describe('pipeline', () => { const argv = { name: 'some name', detach: true, + annotation: [], 'var-file': yaml.safeLoad(rawFile), }; const pip = new CfPipeline(argv); From 03e140a88a2e7a541e3c4cb1d8fa15504b3885a7 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 13:21:39 +0300 Subject: [PATCH 13/21] add documentation --- docs/content/pipelines/Run Pipeline.md | 22 ++++++++++++++++++++++ lib/interface/cli/helpers/general.js | 15 ++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/content/pipelines/Run Pipeline.md b/docs/content/pipelines/Run Pipeline.md index eec6cdc8a..00f619143 100644 --- a/docs/content/pipelines/Run Pipeline.md +++ b/docs/content/pipelines/Run Pipeline.md @@ -35,4 +35,26 @@ The pipeline will be triggered multiple times according to the array length. ] ``` +#### Variable yaml file with 1 sets of variables and encrypted variables +```yaml +- key: + val: value + encrypted: true + key2: key1 + +``` + +#### Variable json file with 1 sets of variables and encrypted variables +```json +[ + { + "key": { + "val": "value", + "encrypted": true + }, + "key2": "key1" + } +] +``` + {{EXAMPLES}} diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 2e18e345c..2fcfdc935 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -142,17 +142,18 @@ const crudFilenameOption = (yargs, options = {}) => { .coerce(options.name || 'filename', (arg) => { try { const rawFile = fs.readFileSync(path.resolve(process.cwd(), arg), 'utf8'); + let content; if (arg.endsWith('.json')) { - return options.raw ? rawFile : JSON.parse(rawFile); + content = options.raw ? rawFile : JSON.parse(rawFile); } if (arg.endsWith('.yml') || arg.endsWith('yaml')) { - const content = options.raw ? rawFile : yaml.safeLoad(rawFile); - if (_.isObject(content)) { - return content; - } - throw new CFError('Not a valid Yaml file'); + content = options.raw ? rawFile : yaml.safeLoad(rawFile); + } + if (_.isObject(content)) { + return content; } - throw new CFError('File extension is not recognized'); + throw new CFError('Not a valid file.\n' + + ' for more info how to pass valid file doc link: https://codefresh-io.github.io/cli/pipelines/run-pipeline/'); } catch (err) { const error = new CFError({ message: 'Failed to read file', From 155aaf6135edd2886d92ffa3540eefd27f15f7d3 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 13:23:31 +0300 Subject: [PATCH 14/21] add documentation --- lib/interface/cli/helpers/general.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 2fcfdc935..c005a8fdd 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -153,7 +153,7 @@ const crudFilenameOption = (yargs, options = {}) => { return content; } throw new CFError('Not a valid file.\n' - + ' for more info how to pass valid file doc link: https://codefresh-io.github.io/cli/pipelines/run-pipeline/'); + + 'for more information how to pass valid file: https://codefresh-io.github.io/cli/pipelines/run-pipeline/'); } catch (err) { const error = new CFError({ message: 'Failed to read file', From 8436af7fbcb03c1efb6e9cb35ca6311122e54d5a Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 13:50:14 +0300 Subject: [PATCH 15/21] wip documentation --- docs/content/pipelines/Run Pipeline.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/content/pipelines/Run Pipeline.md b/docs/content/pipelines/Run Pipeline.md index 00f619143..9df1e8e2c 100644 --- a/docs/content/pipelines/Run Pipeline.md +++ b/docs/content/pipelines/Run Pipeline.md @@ -15,22 +15,22 @@ The pipeline will be triggered multiple times according to the array length. #### Variable yaml file with 2 sets of variables ```yaml -- key: value - key2: key1 -- key: value - key2: key2 +- VARIABLE_A: value_a_for_the_first_build + VARIABLE_B: value_b_for_the_first_build +- VARIABLE_A: value_a_for_the_first_build + VARIABLE_B: value_b_for_the_first_build ``` #### Variable json file with 2 sets of variables ```json [ { - "key": "value", - "key2": "key1" + "VARIABLE_A": "value_a_for_the_first_build", + "VARIABLE_B": "value_b_for_the_first_build" }, { - "key": "value", - "key2": "key2" + "VARIABLE_A": "value_a_for_the_first_build", + "VARIABLE_B": "value_b_for_the_first_build" } ] ``` From 140d596d6f5e436a07091d80df679a0701cfa466 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 13:52:46 +0300 Subject: [PATCH 16/21] wip documentation --- docs/content/pipelines/Run Pipeline.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/pipelines/Run Pipeline.md b/docs/content/pipelines/Run Pipeline.md index 9df1e8e2c..dfb5e7cfb 100644 --- a/docs/content/pipelines/Run Pipeline.md +++ b/docs/content/pipelines/Run Pipeline.md @@ -34,7 +34,7 @@ The pipeline will be triggered multiple times according to the array length. } ] ``` - +### starting support encrypted variables in codefresh run from cli version: 0.82.8 #### Variable yaml file with 1 sets of variables and encrypted variables ```yaml - key: From 94587aa541db94da1a7b6ecaee5e60a436ce8724 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 16:33:48 +0300 Subject: [PATCH 17/21] wip documentation --- docs/content/pipelines/Run Pipeline.md | 6 +++--- lib/interface/cli/commands/pipeline/run.cmd.js | 1 + lib/interface/cli/helpers/general.js | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/content/pipelines/Run Pipeline.md b/docs/content/pipelines/Run Pipeline.md index dfb5e7cfb..8fef78c36 100644 --- a/docs/content/pipelines/Run Pipeline.md +++ b/docs/content/pipelines/Run Pipeline.md @@ -34,8 +34,8 @@ The pipeline will be triggered multiple times according to the array length. } ] ``` -### starting support encrypted variables in codefresh run from cli version: 0.82.8 -#### Variable yaml file with 1 sets of variables and encrypted variables +### Use encrypted variables in Codefresh build runs; supported from CLI version: 0.82.8 +#### Variable yaml file with single variable set with encrypted variables ```yaml - key: val: value @@ -44,7 +44,7 @@ The pipeline will be triggered multiple times according to the array length. ``` -#### Variable json file with 1 sets of variables and encrypted variables +#### Variable json file single variable set with encrypted variables ```json [ { diff --git a/lib/interface/cli/commands/pipeline/run.cmd.js b/lib/interface/cli/commands/pipeline/run.cmd.js index e69a9e53a..11b468cfc 100644 --- a/lib/interface/cli/commands/pipeline/run.cmd.js +++ b/lib/interface/cli/commands/pipeline/run.cmd.js @@ -132,6 +132,7 @@ const run = new Command({ .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master', 'Defining the source control context using a branch') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -s=52b992e783d2f84dd0123c70ac8623b4f0f938d1', 'Defining the source control context using a commit') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master -v key1=value1 -v key2=value2', 'Setting variables through the command') + .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master -v key1=value1 -v key2=value2 -e key1', 'Setting variables through the command with encrypted variables') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master --var-file ./var_file.yml', 'Settings variables through a yml file') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master --context context', 'Inject contexts to the pipeline execution') .example('codefresh run PIPELINE_ID | PIPELINE_NAME --skip step1 step2 step3', 'Skip specific steps'); diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index c005a8fdd..5f7b51814 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -153,7 +153,7 @@ const crudFilenameOption = (yargs, options = {}) => { return content; } throw new CFError('Not a valid file.\n' - + 'for more information how to pass valid file: https://codefresh-io.github.io/cli/pipelines/run-pipeline/'); + + 'For more information how to pass a valid file, go to https://codefresh-io.github.io/cli/pipelines/run-pipeline/'); } catch (err) { const error = new CFError({ message: 'Failed to read file', From afec1905dfb9a2c95a6ead7e5b193702da1a7aa0 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Tue, 18 Apr 2023 19:15:54 +0300 Subject: [PATCH 18/21] address Kim comments --- lib/interface/cli/commands/pipeline/run.cmd.js | 2 +- lib/interface/cli/helpers/general.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.cmd.js b/lib/interface/cli/commands/pipeline/run.cmd.js index 11b468cfc..f1bb299ff 100644 --- a/lib/interface/cli/commands/pipeline/run.cmd.js +++ b/lib/interface/cli/commands/pipeline/run.cmd.js @@ -132,7 +132,7 @@ const run = new Command({ .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master', 'Defining the source control context using a branch') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -s=52b992e783d2f84dd0123c70ac8623b4f0f938d1', 'Defining the source control context using a commit') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master -v key1=value1 -v key2=value2', 'Setting variables through the command') - .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master -v key1=value1 -v key2=value2 -e key1', 'Setting variables through the command with encrypted variables') + .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master -v key1=value1 -v key2=value2 -e key1', 'Setting variables through the command with encrypted option') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master --var-file ./var_file.yml', 'Settings variables through a yml file') .example('codefresh run PIPELINE_ID | PIPELINE_NAME -b=master --context context', 'Inject contexts to the pipeline execution') .example('codefresh run PIPELINE_ID | PIPELINE_NAME --skip step1 step2 step3', 'Skip specific steps'); diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 5f7b51814..06f299723 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -114,7 +114,8 @@ const prepareKeyValueObjectsFromEnvFileOption = (environmentVariables) => _.map( return { key, value }; }); -const prepareEncryptedValues = (variables, encrypted) => { +const prepareEncryptedValues = (requestedVariables, encrypted) => { + const variables = _.cloneDeep(requestedVariables); const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); _.forEach(encrypted, (varName) => { const variable = variableMap[varName]; From 2768712fb19677482d466b868175a9e7e5389b6f Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Wed, 19 Apr 2023 17:50:41 +0300 Subject: [PATCH 19/21] address Daniel comments --- .../cli/commands/pipeline/run.base.js | 4 ++-- lib/interface/cli/commands/pipeline/var.json | 2 +- lib/interface/cli/commands/pipeline/var.yml | 2 +- .../cli/commands/project/apply.cmd.js | 8 +++---- .../cli/commands/project/create.cmd.js | 8 +++---- lib/interface/cli/helpers/general.js | 24 ++++++++++++++----- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/interface/cli/commands/pipeline/run.base.js b/lib/interface/cli/commands/pipeline/run.base.js index b0f87d490..224f30eec 100644 --- a/lib/interface/cli/commands/pipeline/run.base.js +++ b/lib/interface/cli/commands/pipeline/run.base.js @@ -2,7 +2,7 @@ const _ = require('lodash'); const Promise = require('bluebird'); const CFError = require('cf-errors'); const { prepareKeyValueFromCLIEnvOption, - prepareEncryptedValues, + markEncryptedFlagOnRequestedVariables, prepareKeyValueObjectsFromEnvFileOption } = require('../../helpers/general'); const { validatePipelineYaml } = require('../../helpers/validation'); const { printResult } = require('../root/validate.cmd'); @@ -65,7 +65,7 @@ class RunBaseCommand { this.executionRequests.push(request); }); } else { - const variables = prepareEncryptedValues(this.argv.variable, this.argv.encrypted); + const variables = markEncryptedFlagOnRequestedVariables(this.argv.variable, this.argv.encrypted); const request = _.cloneDeep(executionRequestTemplate); request.options.variables = variables; request.options.contexts = contexts; diff --git a/lib/interface/cli/commands/pipeline/var.json b/lib/interface/cli/commands/pipeline/var.json index 802da70b6..bf6ed0f7d 100644 --- a/lib/interface/cli/commands/pipeline/var.json +++ b/lib/interface/cli/commands/pipeline/var.json @@ -3,7 +3,7 @@ "help6": "85858", "should_be_encrepted": "0000", "help7": { - "val": "test", + "value": "test", "encrypted": true } } diff --git a/lib/interface/cli/commands/pipeline/var.yml b/lib/interface/cli/commands/pipeline/var.yml index 71b86b748..db6b028b0 100644 --- a/lib/interface/cli/commands/pipeline/var.yml +++ b/lib/interface/cli/commands/pipeline/var.yml @@ -1,5 +1,5 @@ build1: VAR1: 'VAL1' VAR2: - val: VAL2 + value: VAL2 encrypted: true diff --git a/lib/interface/cli/commands/project/apply.cmd.js b/lib/interface/cli/commands/project/apply.cmd.js index 3185cf16c..082b9106b 100644 --- a/lib/interface/cli/commands/project/apply.cmd.js +++ b/lib/interface/cli/commands/project/apply.cmd.js @@ -4,7 +4,7 @@ const _ = require('lodash'); const { sdk } = require('../../../../logic'); const applyRoot = require('../root/apply.cmd'); -const { prepareKeyValueObjectsFromCLIEnvOption, ignoreHttpError, prepareEncryptedValues} = require('../../helpers/general'); +const { prepareKeyValueObjectsFromCLIEnvOption, ignoreHttpError, markEncryptedFlagOnRequestedVariables } = require('../../helpers/general'); const command = new Command({ command: 'project [id|name]', @@ -57,11 +57,11 @@ const command = new Command({ const { newName: projectName, tag: tags, - variable, + variable: variables, encrypted, } = argv; - const variables = prepareEncryptedValues(variable, encrypted); + const requestedProjectVariables = markEncryptedFlagOnRequestedVariables(variables, encrypted); let project = await sdk.projects.get({ id }).catch(ignoreHttpError); project = project || await sdk.projects.getByName({ name }).catch(ignoreHttpError); @@ -74,7 +74,7 @@ const command = new Command({ const updatePayload = _.pickBy({ projectName, tags: tags || existingTags, - variables: variables || existingVariables, + variables: requestedProjectVariables || existingVariables, }, _.identity); await sdk.projects.patch({ id: project.id }, updatePayload); diff --git a/lib/interface/cli/commands/project/create.cmd.js b/lib/interface/cli/commands/project/create.cmd.js index fc65d80d5..e24b19a95 100644 --- a/lib/interface/cli/commands/project/create.cmd.js +++ b/lib/interface/cli/commands/project/create.cmd.js @@ -2,7 +2,7 @@ const Command = require('../../Command'); const { sdk } = require('../../../../logic'); const createRoot = require('../root/create.cmd'); const { checkOrProjectExists } = require('../../helpers/validation'); -const { prepareKeyValueObjectsFromCLIEnvOption, prepareEncryptedValues } = require('../../helpers/general'); +const { prepareKeyValueObjectsFromCLIEnvOption, markEncryptedFlagOnRequestedVariables } = require('../../helpers/general'); const command = new Command({ command: 'project ', @@ -46,14 +46,14 @@ const command = new Command({ const { name: projectName, tag: tags, - variable, + variable: variables, encrypted, } = argv; - const variables = prepareEncryptedValues(variable, encrypted); + const requestedProjectVariables = markEncryptedFlagOnRequestedVariables(variables, encrypted); await checkOrProjectExists(projectName); - await sdk.projects.create({ projectName, tags, variables }); + await sdk.projects.create({ projectName, tags, requestedProjectVariables }); console.log(`Project: "${projectName}" created`); }, }); diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index 06f299723..a8fa7fea4 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -106,15 +106,26 @@ const prepareKeyValueObjectsFromCLIEnvOption = (environmentVariables) => { return variables; }; +/** + * will return an array of objects { key, value, encrypted } from parsing an array of objects: key=value ,key: {value: "value", encrypted: true } + * @param environmentVariables + * @returns Array of { key, value, encrypted } + */ const prepareKeyValueObjectsFromEnvFileOption = (environmentVariables) => _.map(environmentVariables, (value, key) => { if (_.isObject(value)) { - const { val, encrypted } = value; - return { key, value: val, encrypted }; + return { key, ...value }; } return { key, value }; }); -const prepareEncryptedValues = (requestedVariables, encrypted) => { +/** + * The function takes in a list of requested variables and an array of encrypted variable names, + * and sets the encrypted flag to true on each corresponding variable object in the list. + * @param environmentVariables + * @param encrypted + * @returns Array of { key, value, encrypted } + */ +const markEncryptedFlagOnRequestedVariables = (requestedVariables, encrypted) => { const variables = _.cloneDeep(requestedVariables); const variableMap = _.reduce(variables, (acc, v) => _.assign(acc, { [v.key]: v }), {}); _.forEach(encrypted, (varName) => { @@ -146,9 +157,10 @@ const crudFilenameOption = (yargs, options = {}) => { let content; if (arg.endsWith('.json')) { content = options.raw ? rawFile : JSON.parse(rawFile); - } - if (arg.endsWith('.yml') || arg.endsWith('yaml')) { + } else if (arg.endsWith('.yml') || arg.endsWith('yaml')) { content = options.raw ? rawFile : yaml.safeLoad(rawFile); + } else { + throw new CFError('File extension is not recognized'); } if (_.isObject(content)) { return content; @@ -258,6 +270,6 @@ module.exports = { detectProxy, keyValueArrayToObject, addProxyVariables, - prepareEncryptedValues, + markEncryptedFlagOnRequestedVariables, prepareKeyValueObjectsFromEnvFileOption, }; From bcffa13030bffd427ad048160badcdaf81610b28 Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Thu, 20 Apr 2023 10:32:33 +0300 Subject: [PATCH 20/21] address Daniel comments --- lib/interface/cli/helpers/general.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/interface/cli/helpers/general.js b/lib/interface/cli/helpers/general.js index a8fa7fea4..60b797c08 100644 --- a/lib/interface/cli/helpers/general.js +++ b/lib/interface/cli/helpers/general.js @@ -113,7 +113,8 @@ const prepareKeyValueObjectsFromCLIEnvOption = (environmentVariables) => { */ const prepareKeyValueObjectsFromEnvFileOption = (environmentVariables) => _.map(environmentVariables, (value, key) => { if (_.isObject(value)) { - return { key, ...value }; + const { value: val, encrypted } = value; + return { key, value: val, encrypted }; } return { key, value }; }); From 51f1165bbd8f664c5abbd7e7f47decbbacd874de Mon Sep 17 00:00:00 2001 From: eti-codefresh Date: Sun, 23 Apr 2023 12:09:03 +0300 Subject: [PATCH 21/21] wip --- lib/interface/cli/commands/project/create.cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/interface/cli/commands/project/create.cmd.js b/lib/interface/cli/commands/project/create.cmd.js index e24b19a95..306dc2c37 100644 --- a/lib/interface/cli/commands/project/create.cmd.js +++ b/lib/interface/cli/commands/project/create.cmd.js @@ -53,7 +53,7 @@ const command = new Command({ const requestedProjectVariables = markEncryptedFlagOnRequestedVariables(variables, encrypted); await checkOrProjectExists(projectName); - await sdk.projects.create({ projectName, tags, requestedProjectVariables }); + await sdk.projects.create({ projectName, tags, variables: requestedProjectVariables }); console.log(`Project: "${projectName}" created`); }, });