Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/interface/cli/commands/root/create.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const get = new Command({
root: true,
command: 'create',
description: 'Create a resource from a file or stdin',
usage: 'Supported resources: \n\t\'context\'\n\t\'pipeline\'',
usage: 'Supported resources: \n\t\'context\'\n\t\'pipeline\'\'\n\t\'step\'',
webDocs: {
description: 'Create a resource from a file, directory or url',
category: 'Operate On Resources',
Expand Down Expand Up @@ -48,6 +48,10 @@ const get = new Command({
await sdk.pipelines.create(data);
console.log(`Pipeline '${name}' created`);
break;
case 'step':
await sdk.steps.create(data);
console.log(`Step '${name}' created`);
break;
default:
throw new CFError(`Entity: ${entity} not supported`);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/interface/cli/commands/root/replace.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const annotate = new Command({
root: true,
command: 'replace',
description: 'Replace a resource by filename',
usage: 'Supported resources: \n\t\'Context\'\n\t\'Pipeline\'',
usage: 'Supported resources: \n\t\'Context\'\n\t\'Pipeline\'\'\n\t\'Step\'',
webDocs: {
description: 'Replace a resource from a file, directory or url',
category: 'Operate On Resources',
Expand Down Expand Up @@ -56,6 +56,12 @@ const annotate = new Command({
}, data);
console.log(`Pipeline '${name}' updated`);
break;
case 'step':
await sdk.steps.replace({
name,
}, data);
console.log(`Step '${name}' updated`);
break;
default:
throw new CFError(`Entity: ${entity} not supported`);
}
Expand Down
30 changes: 30 additions & 0 deletions lib/interface/cli/commands/step/delete.cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Command = require('../../Command');
const deleteRoot = require('../root/delete.cmd');
const { sdk } = require('../../../../logic');


const command = new Command({
command: 'step [name]',
parent: deleteRoot,
description: 'Delete a step',
webDocs: {
category: 'Steps',
title: 'Delete Step',
},
builder: (yargs) => {
return yargs
.positional('name', {
describe: 'Step name',
});
},
handler: async (argv) => {
const { name } = argv;

await sdk.steps.delete({ name });
console.log(`Step '${name}' deleted.`);
},
});


module.exports = command;

84 changes: 84 additions & 0 deletions lib/interface/cli/commands/step/get.cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const debug = require('debug')('codefresh:cli:get:step');
const Command = require('../../Command');
const CFError = require('cf-errors');
const _ = require('lodash');
const DEFAULTS = require('../../defaults');
const { prepareKeyValueFromCLIEnvOption } = require('../../helpers/general');
const Output = require('../../../../output/Output');
const { sdk } = require('../../../../logic');
const Step = require('../../../../logic/entities/Step');

const getRoot = require('../root/get.cmd');


const command = new Command({
command: 'steps [id..]',
aliases: ['step'],
parent: getRoot,
description: 'Get a specific step or an array of steps',
webDocs: {
category: 'Steps',
title: 'Get Step',
},
builder: (yargs) => {
return yargs
.positional('id', {
describe: 'Step name/id',
})
.option('name', {
describe: 'Filter steps by name',
})
.option('label', {
describe: 'Filter by a label',
alias: 'l',
default: [],
})
.option('limit', {
describe: 'Limit amount of returned results',
default: DEFAULTS.GET_LIMIT_RESULTS,
})
.option('page', {
describe: 'Paginated page',
default: DEFAULTS.GET_PAGINATED_PAGE,
});
},
handler: async (argv) => {
const { id: ids, name } = argv;
const limit = argv.limit;
const offset = (argv.page - 1) * limit;
const labels = prepareKeyValueFromCLIEnvOption(argv.label);

if (!_.isEmpty(ids)) {
const steps = [];
for (const id of ids) {
try {
const currStep = await sdk.steps.get({ name: id });
steps.push(Step.fromResponse(currStep));
} catch (err) {
if (steps.length) {
Output.print(steps);
}

debug(err.toString());
const message = err.toString().includes('not find') ? `Step '${id}' was not found.` : 'Error occurred';
throw new CFError({
cause: err,
message,
});
}
}
Output.print(steps);
} else {
const steps = await sdk.steps.list({
limit,
offset,
id: name,
labels,
});
Output.print(_.map(_.get(steps, 'docs'), Step.fromResponse));
}
},
});

module.exports = command;

14 changes: 14 additions & 0 deletions lib/interface/cli/commands/step/get.completion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const _ = require('lodash');
const steps = () => require('../../../../logic').sdk.steps;
const Step = require('../../../../logic/entities/Step');
const { authContextWrapper } = require('../../completion/helpers');

const positionalHandler = async ({word, argv}) => {
const stps = await steps().list({ limit: 25, offset: 0 });
return _.map(_.get(stps, 'docs'), Step.fromResponse).map(p => p.name);
};

module.exports = {
positionalHandler: authContextWrapper(positionalHandler),
};

67 changes: 67 additions & 0 deletions lib/interface/cli/commands/step/step.sdk.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const getCmd = require('./get.cmd').toCommand();
const deleteCmd = require('./delete.cmd').toCommand();
const { positionalHandler } = require('./get.completion');

jest.mock('../../helpers/validation'); // eslint-disable-line

jest.mock('../../completion/helpers', () => { // eslint-disable-line
return {
authContextWrapper: func => func,
};
});

jest.mock('../../../../logic/entities/Step', () => { // eslint-disable-line
return {
fromResponse: res => res,
};
});

const request = require('requestretry');

const DEFAULT_RESPONSE = request.__defaultResponse();

describe('step', () => {
beforeEach(async () => {
request.__reset();
request.mockClear();
await configureSdk(); // eslint-disable-line
});

describe('commands', () => {
describe('get', () => {
it('should handle getting given id', async () => {
const argv = { id: ['some id'] };
await getCmd.handler(argv);
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
});

it('should handle getting all', async () => {
const argv = {};
const response = { statusCode: 200, body: { docs: [DEFAULT_RESPONSE.body] } };
request.__setResponse(response);
await getCmd.handler(argv);
await verifyResponsesReturned([response]); // eslint-disable-line
});
});

// decided to follow unit tests modularity concept
describe('delete', () => {
it('should handle deletion given name', async () => {
const argv = { name: 'some name' };
await deleteCmd.handler(argv);
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
});
});
});

describe('completions', () => {
describe('get', () => {
it('should handle getting completion', async () => {
const response = { statusCode: 200, body: { docs: [{ name: 'some pip' }] } };
request.__setResponse(response);
await positionalHandler({});
await verifyResponsesReturned([response]); // eslint-disable-line
});
});
});
});
22 changes: 22 additions & 0 deletions lib/logic/entities/Step.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const Entity = require('./Entity');
const _ = require('lodash');

class Step extends Entity {
constructor(data) {
super();
this.entityType = 'step';
this.info = data;
this.id = this.info.metadata.id;
this.name = this.info.metadata.name;
this.created = this.info.metadata.created_at ? new Date(this.info.metadata.created_at) : undefined;
this.updated = this.info.metadata.updated_at ? new Date(this.info.metadata.updated_at) : undefined;
this.defaultColumns = ['name', 'updated', 'created'];
this.wideColumns = ['id'].concat(this.defaultColumns);
}

static fromResponse(response) {
return new Step(_.pick(response, 'id', 'version', 'kind', 'metadata', 'spec'));
}
}

module.exports = Step;
Loading