Skip to content

Commit a9a2ae7

Browse files
yaroslav-codefreshitai-codefresh
authored andcommitted
Refactor cli to use new codefresh-sdk (codefresh-io#279)
1 parent 50143a0 commit a9a2ae7

File tree

188 files changed

+3877
-4211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+3877
-4211
lines changed

Diff for: __mocks__/requestretry.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const _ = require('lodash');
2+
3+
const DEFAULT_RESPONSE = { statusCode: 200, body: { test: 'test' } };
4+
5+
let RESPONSE = _.clone(DEFAULT_RESPONSE);
6+
7+
let QUEUE = [];
8+
9+
const request = jest.fn(async () => {
10+
if (!_.isEmpty(QUEUE)) {
11+
return QUEUE.shift();
12+
}
13+
return RESPONSE;
14+
});
15+
16+
request.defaults = () => request;
17+
18+
request.__setResponse = (response) => {
19+
RESPONSE = response;
20+
};
21+
22+
request.__reset = () => {
23+
RESPONSE = _.clone(DEFAULT_RESPONSE);
24+
QUEUE = [];
25+
};
26+
27+
request.__defaultResponse = () => {
28+
return _.clone(DEFAULT_RESPONSE);
29+
};
30+
31+
request.__queueResponses = (queue) => {
32+
QUEUE = QUEUE.concat(queue);
33+
};
34+
35+
module.exports = request;

Diff for: lib/interface/cli/Command.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const yargs = require('yargs');
22
const CFError = require('cf-errors');
33
const assert = require('assert');
44
const _ = require('lodash');
5-
const { printError, wrapHandler } = require('./helpers/general');
5+
const { wrapHandler } = require('./helpers/general');
66
const authManager = require('../../logic').auth.manager;
7+
const Output = require('../../output/Output');
78

89

910
class Command {
@@ -51,7 +52,7 @@ class Command {
5152
this.addBuilder(this._createUsageBuilder.bind(this));
5253

5354
(_.isUndefined(command.handler)) ? command.handler = (args) => {
54-
printError(`Error: unknown command "${args._[args._.length - 1]}"\n` +
55+
Output.printError(`Error: unknown command "${args._[args._.length - 1]}"\n` +
5556
'Run \'codefresh --help\' for usage.');
5657

5758
} : _.noop();

Diff for: lib/interface/cli/commands/board/apply.cmd.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Command = require('../../Command');
2-
const { board: boardLogic } = require('../../../../logic').api;
2+
const { sdk } = require('../../../../logic');
33

44
const applyRoot = require('../root/apply.cmd');
55

@@ -38,23 +38,23 @@ const command = new Command({
3838
return yargs;
3939
},
4040
handler: async (argv) => {
41-
let { id } = argv;
41+
let { id, name } = argv;
4242

4343
const data = {
4444
name: argv.newName,
4545
filter: argv.filter,
4646
};
4747

4848
if (!id) {
49-
if (!argv.name) throw Error('Nor board-id nor board-name was specified');
49+
if (!name) throw Error('Neither board-id nor board-name were specified');
5050

51-
const boardObj = await boardLogic.getBoardByName(argv.name);
52-
id = boardObj.id;
51+
const boardObj = await sdk.helm.boards.getByName({ name });
52+
id = boardObj._id;
5353
}
5454

5555

56-
await boardLogic.updateBoard(id, data);
57-
console.log(`Board: "${data.name}" patched.`);
56+
await sdk.helm.boards.patch({ id }, data);
57+
console.log(`Board: "${name || id}" patched.`);
5858
},
5959
});
6060

Diff for: lib/interface/cli/commands/board/board.sdk.spec.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const getCmd = require('./get.cmd').toCommand();
2+
const deleteCmd = require('./delete.cmd').toCommand();
3+
const createCmd = require('./create.cmd').toCommand();
4+
const applyCmd = require('./apply.cmd').toCommand();
5+
6+
7+
jest.mock('../../../../logic/entities/Board');
8+
9+
const request = require('requestretry');
10+
11+
const DEFAULT_RESPONSE = request.__defaultResponse();
12+
13+
describe('board commands', () => {
14+
beforeEach(async () => {
15+
request.__reset();
16+
request.mockClear();
17+
await configureSdk(); // eslint-disable-line
18+
});
19+
20+
describe('get', () => {
21+
it('should handle getting given id', async () => {
22+
const argv = { id: 'some id' };
23+
await getCmd.handler(argv);
24+
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
25+
});
26+
27+
it('should handle getting given name', async () => {
28+
const argv = { name: 'some name' };
29+
await getCmd.handler(argv);
30+
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
31+
});
32+
33+
it('should handle getting all', async () => {
34+
const argv = {};
35+
const response = { statusCode: 200, body: [DEFAULT_RESPONSE.body] };
36+
request.__setResponse(response);
37+
await getCmd.handler(argv);
38+
await verifyResponsesReturned([response]); // eslint-disable-line
39+
});
40+
});
41+
42+
describe('create', () => {
43+
it('should handle creation', async () => {
44+
const argv = { name: 'some name' };
45+
await createCmd.handler(argv);
46+
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
47+
});
48+
});
49+
50+
describe('apply', () => {
51+
it('should handle patching by id', async () => {
52+
const argv = { id: 'some id' };
53+
await applyCmd.handler(argv);
54+
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
55+
});
56+
57+
it('should handle patching by name', async () => {
58+
const argv = { name: 'some name' };
59+
const responses = [
60+
{ statusCode: 200, body: { _id: 'some id' } },
61+
DEFAULT_RESPONSE,
62+
];
63+
request.__queueResponses(responses);
64+
await applyCmd.handler(argv);
65+
await verifyResponsesReturned(responses); // eslint-disable-line
66+
});
67+
});
68+
69+
describe('delete', () => {
70+
it('should handle deletion given id', async () => {
71+
const argv = { id: 'some id' };
72+
await deleteCmd.handler(argv);
73+
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
74+
});
75+
76+
it('should handle deletion given name', async () => {
77+
const argv = { name: 'some name' };
78+
const responses = [
79+
{ statusCode: 200, body: { _id: 'some id' } },
80+
DEFAULT_RESPONSE,
81+
];
82+
request.__queueResponses(responses);
83+
await deleteCmd.handler(argv);
84+
await verifyResponsesReturned(responses); // eslint-disable-line
85+
});
86+
});
87+
});

Diff for: lib/interface/cli/commands/board/create.cmd.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const Command = require('../../Command');
2-
const { board: boardLogic } = require('../../../../logic').api;
2+
const { sdk } = require('../../../../logic');
33
const createRoot = require('../root/create.cmd');
44

55
const command = new Command({
66
command: 'board <name>',
77
parent: createRoot,
8-
description: 'Create a team',
8+
description: 'Create a board',
99
usage: 'You can create a board specifying name unique for account.',
1010
webDocs: {
1111
category: 'Boards',
@@ -25,7 +25,7 @@ const command = new Command({
2525
handler: async (argv) => {
2626
const reqBody = Object.assign({ name: argv.name }, argv.filter);
2727

28-
await boardLogic.createBoard(reqBody);
28+
await sdk.helm.boards.create(reqBody);
2929
console.log(`Board: "${reqBody.name}" created`);
3030
},
3131
});

Diff for: lib/interface/cli/commands/board/delete.cmd.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Command = require('../../Command');
2-
const { board } = require('../../../../logic').api;
2+
const { sdk } = require('../../../../logic');
33
const deleteRoot = require('../root/delete.cmd');
44

55
const command = new Command({
@@ -28,13 +28,13 @@ const command = new Command({
2828
let { id } = argv;
2929

3030
if (!id) {
31-
if (!name) throw Error('Nor board-id nor board-name was specified');
31+
if (!name) throw Error('Neither board-id nor board-name was specified');
3232

33-
const boardObj = await board.getBoardByName(name);
34-
id = boardObj.id;
33+
const boardObj = await sdk.helm.boards.getByName({ name });
34+
id = boardObj._id;
3535
}
3636

37-
await board.deleteBoard(id);
37+
await sdk.helm.boards.delete({ id });
3838
console.log(`Board '${name || id}' deleted.`);
3939
},
4040
});

Diff for: lib/interface/cli/commands/board/get.cmd.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const debug = require('debug')('codefresh:cli:create:pipelines2');
22
const Command = require('../../Command');
33
const CFError = require('cf-errors');
4-
const { board: boardLogic } = require('../../../../logic').api;
4+
const { sdk } = require('../../../../logic');
5+
const Board = require('../../../../logic/entities/Board');
56
const Output = require('../../../../output/Output');
67

78
const getRoot = require('../root/get.cmd');
@@ -28,8 +29,8 @@ const command = new Command({
2829
const { id, name } = argv;
2930
if (id) {
3031
try {
31-
const board = await boardLogic.getBoardById(id);
32-
Output.print(board);
32+
const board = await sdk.helm.boards.get({ id });
33+
Output.print(Board.fromResponse(board));
3334
} catch (err) {
3435
debug(err.toString());
3536
const message = `Board '${id}' was not found`;
@@ -40,8 +41,8 @@ const command = new Command({
4041
}
4142
} else if (name) {
4243
try {
43-
const board = await boardLogic.getBoardByName(name);
44-
Output.print(board);
44+
const board = await sdk.helm.boards.getByName({ name });
45+
Output.print(Board.fromResponse(board));
4546
} catch (err) {
4647
debug(err.toString());
4748
const message = `Board '${name}' was not found`;
@@ -51,8 +52,8 @@ const command = new Command({
5152
});
5253
}
5354
} else {
54-
const boards = await boardLogic.getAll({ name });
55-
Output.print(boards);
55+
const boards = await sdk.helm.boards.list();
56+
Output.print(boards.map(Board.fromResponse));
5657
}
5758
},
5859
});
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const getCmd = require('./get.cmd').toCommand();
2+
const deleteCmd = require('./delete.cmd').toCommand();
3+
const createCmd = require('./create.cmd').toCommand();
4+
5+
const request = require('requestretry');
6+
7+
jest.mock('../../../../logic/entities/Cluster');
8+
9+
10+
const DEFAULT_RESPONSE = request.__defaultResponse();
11+
12+
describe('cluster commands', () => {
13+
beforeEach(async () => {
14+
request.__reset();
15+
request.mockClear();
16+
await configureSdk(); // eslint-disable-line
17+
});
18+
19+
describe('get', () => {
20+
it('should handle getting all', async () => {
21+
const argv = {};
22+
const response = { statusCode: 200, body: [DEFAULT_RESPONSE.body] };
23+
request.__setResponse(response);
24+
await getCmd.handler(argv);
25+
await verifyResponsesReturned([response]); // eslint-disable-line
26+
});
27+
});
28+
29+
describe('create', () => {
30+
it.skip('should skip', () => {
31+
// todo : test custom sdk logic
32+
});
33+
});
34+
35+
describe('delete', () => {
36+
it('should handle deletion given name', async () => {
37+
const clusterName = 'some name';
38+
const provider = 'some provider';
39+
const argv = { name: clusterName };
40+
const responses = [
41+
{ statusCode: 200, body: [{ _id: 'some id', selector: clusterName, provider }] },
42+
DEFAULT_RESPONSE,
43+
];
44+
request.__queueResponses(responses);
45+
await deleteCmd.handler(argv);
46+
await verifyResponsesReturned(responses); // eslint-disable-line
47+
});
48+
49+
it('should throw on cluster not found', async () => {
50+
const clusterName = 'some name';
51+
const argv = { name: clusterName };
52+
const response = { statusCode: 200, body: [] };
53+
request.__setResponse(response); // no clusters on getAll
54+
await expectThrows(async () => { // eslint-disable-line
55+
await deleteCmd.handler(argv);
56+
});
57+
await verifyResponsesReturned([response]); // eslint-disable-line
58+
});
59+
});
60+
});

Diff for: lib/interface/cli/commands/cluster/create.cmd.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const debug = require('debug')('codefresh:cli:create:context');
21
const Command = require('../../Command');
3-
const { cluster } = require('../../../../logic').api;
42
const createRoot = require('../root/create.cmd');
53
const authManager = require('../../../../logic/auth').manager; // eslint-disable-line
4+
const { sdk } = require('../../../../logic');
65

76
const command = new Command({
87
command: 'clusters [name]',
@@ -49,7 +48,7 @@ const command = new Command({
4948
'behind-firewall': behindFirewall,
5049
name,
5150
} = argv;
52-
await cluster.createCluster({
51+
await sdk.clusters.create({
5352
contextName,
5453
context,
5554
namespace,

Diff for: lib/interface/cli/commands/cluster/delete.cmd.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const debug = require('debug')('codefresh:cli:create:context');
21
const Command = require('../../Command');
3-
const { cluster } = require('../../../../logic').api;
42
const deleteRoot = require('../root/delete.cmd');
3+
const { sdk } = require('../../../../logic');
4+
const CFError = require('cf-errors');
5+
const _ = require('lodash');
56

67
const command = new Command({
78
command: 'cluster <name>',
@@ -21,8 +22,17 @@ const command = new Command({
2122
.example('codefresh delete cluster NAME', 'Delete cluster NAME');
2223
},
2324
handler: async (argv) => {
24-
await cluster.deleteCluster(argv.name);
25-
console.log(`Cluster: ${argv.name} deleted`);
25+
const clusters = await sdk.clusters.list();
26+
const cluster = _.find(clusters, (curr) => {
27+
return _.isEqual(curr.selector, argv.name);
28+
});
29+
30+
if (!cluster) {
31+
throw new CFError(`No such cluster: ${argv.name}`);
32+
}
33+
34+
await sdk.clusters.delete({ id: cluster._id, provider: cluster.provider });
35+
console.log(`Cluster: '${argv.name}' deleted`);
2636
},
2737
});
2838

0 commit comments

Comments
 (0)