Skip to content

Commit afc5828

Browse files
Itai GendlerItai Gendler
authored andcommitted
add helm-repo operations
1 parent f443322 commit afc5828

File tree

10 files changed

+283
-10
lines changed

10 files changed

+283
-10
lines changed

docs/content/operate on resources/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
+++
22
title = "Operate On Resources"
3-
weight = 60
3+
weight = 31
44
+++
55

66
The CLI supports the ability to work with `spec` files when working with resources.<br>

docs/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ const baseDir = path.resolve(TEMP_DIR, './content');
1515
const ALLOW_BETA_COMMANDS = process.env.ALLOW_BETA_COMMANDS;
1616
const categoriesOrder = {
1717
authentication: 30,
18+
'operate on resources' : 31,
1819
pipelines : 40,
1920
'pipelines v2 (beta)' : 42,
2021
builds: 50,
21-
'operate on resources' : 60,
2222
contexts : 70 ,
2323
images : 80 ,
2424
triggers : 90,
2525
environments : 100 ,
2626
compositions : 110 ,
27+
'helm repos' : 111 ,
2728
'predefined pipelines': 120,
2829
more : 130 ,
2930
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { helm } = require('../../../../../logic').api;
6+
const { printError } = require('../../../helpers/general');
7+
const { specifyOutputForArray } = require('../../../helpers/get');
8+
9+
const applyRoot = require('../../root/apply.cmd');
10+
11+
12+
const command = new Command({
13+
command: 'helm-repo [name]',
14+
aliases: [],
15+
parent: applyRoot,
16+
description: 'Update a helm repo',
17+
webDocs: {
18+
category: 'Helm Repos',
19+
title: 'Update Helm Repo',
20+
},
21+
builder: (yargs) => {
22+
yargs
23+
.positional('name', {
24+
describe: 'Name of context',
25+
})
26+
.option('public', {
27+
describe: 'Mark the helm repo as public',
28+
alias: 'p',
29+
type: 'boolean',
30+
default: false,
31+
})
32+
.example('codefresh patch helm-repo my-repo -p', 'Update helm repo to be public')
33+
.example('codefresh patch helm-repo my-repo -p=false', 'Update helm repo to be private');
34+
35+
return yargs;
36+
},
37+
handler: async (argv) => {
38+
const data = {
39+
name: argv.name,
40+
public: argv.public,
41+
};
42+
43+
await helm.updateRepo(data.name, data);
44+
console.log(`Helm repo: ${data.name} patched.`);
45+
},
46+
});
47+
48+
module.exports = command;
49+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const debug = require('debug')('codefresh:cli:create:context');
2+
const Command = require('../../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { helm } = require('../../../../../logic').api;
6+
const createRoot = require('../../root/create.cmd');
7+
8+
const command = new Command({
9+
command: 'helm-repo [name]',
10+
parent: createRoot,
11+
description: 'Create a Codefresh managed helm repo',
12+
usage: 'ATM it is only possible to create a helm repository against Codefresh managed helm registry',
13+
webDocs: {
14+
category: 'Helm Repos',
15+
title: 'Create Helm Repo',
16+
},
17+
builder: (yargs) => {
18+
yargs
19+
.positional('name', {
20+
describe: 'Name of context',
21+
})
22+
.option('public', {
23+
describe: 'Mark the helm repo as public',
24+
alias: 'p',
25+
type: 'boolean',
26+
default: false,
27+
})
28+
.example('codefresh create helm-repo', 'Create a private helm repo managed by Codefresh.')
29+
.example('codefresh create helm-repo --public', 'Create a public helm repo managed by Codefresh.');
30+
31+
return yargs;
32+
},
33+
handler: async (argv) => {
34+
const data = {
35+
name: argv.name,
36+
public: argv.public,
37+
};
38+
39+
await helm.createRepo(data);
40+
console.log(`Helm repo: ${data.name} created`);
41+
},
42+
});
43+
44+
module.exports = command;
45+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { helm } = require('../../../../../logic').api;
6+
const deleteRoot = require('../../root/delete.cmd');
7+
8+
9+
const command = new Command({
10+
command: 'helm-repo [name]',
11+
aliases: [],
12+
parent: deleteRoot,
13+
description: 'Delete a helm repo',
14+
webDocs: {
15+
category: 'Helm Repos',
16+
title: 'Delete Helm Repo',
17+
},
18+
builder: (yargs) => {
19+
yargs
20+
.positional('name', {
21+
describe: 'Helm repo name',
22+
})
23+
.example('codefresh delete helm-repo my-repo', 'Delete a helm repo.');
24+
return yargs;
25+
},
26+
handler: async (argv) => {
27+
const { name } = argv;
28+
29+
await helm.deleteRepo(name);
30+
console.log(`Helm repo '${name}' deleted.`);
31+
},
32+
});
33+
34+
35+
module.exports = command;
36+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { helm } = require('../../../../../logic').api;
6+
const { printError } = require('../../../helpers/general');
7+
const { specifyOutputForArray } = require('../../../helpers/get');
8+
9+
const getRoot = require('../../root/get.cmd');
10+
11+
12+
const command = new Command({
13+
command: 'helm-repo [name..]',
14+
aliases: [],
15+
parent: getRoot,
16+
description: 'Get a specific helm-repo or an array of helm-repos',
17+
webDocs: {
18+
category: 'Helm Repos',
19+
title: 'Get Helm Repo',
20+
},
21+
builder: (yargs) => {
22+
yargs
23+
.positional('name', {
24+
describe: 'Helm repo name',
25+
})
26+
.example('codefresh get helm-repo', 'Get all helm repos')
27+
.example('codefresh get helm-repo my-repo', 'Get a specific helm repo');
28+
return yargs;
29+
},
30+
handler: async (argv) => {
31+
const { name: names, output } = argv;
32+
33+
if (!_.isEmpty(names)) {
34+
const repos = [];
35+
for (const name of names) {
36+
try {
37+
const currRepo = await helm.getRepoByName(name);
38+
repos.push(currRepo);
39+
} catch (err) {
40+
if (repos.length) {
41+
specifyOutputForArray(output, repos);
42+
}
43+
44+
debug(err.toString());
45+
const message = err.toString().includes('not find') ? `Helm repo '${name}' was not found.` : 'Error occurred';
46+
throw new CFError({
47+
cause: err,
48+
message,
49+
});
50+
}
51+
}
52+
specifyOutputForArray(output, repos);
53+
} else {
54+
specifyOutputForArray(output, await helm.getAllRepos());
55+
}
56+
},
57+
});
58+
59+
module.exports = command;
60+

lib/logic/api/helm.js

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
const Promise = require('bluebird');
22
const _ = require('lodash');
33
const CFError = require('cf-errors'); // eslint-disable-line
4-
const {
5-
sendHttpRequest,
6-
} = require('./helper');
7-
const {
8-
getContextByName,
9-
} = require('./context');
4+
const { sendHttpRequest, } = require('./helper');
5+
const { getContextByName, } = require('./context');
6+
const HelmRepo = require('../entities/HelmRepo');
107

118
const SUPPORTED_TYPES = [
129
'yaml',
1310
'secret-yaml',
1411
];
1512

16-
1713
const _normalizeValues = async (values = []) => {
1814
const normalizedValues = await Promise.reduce(values, async (normalized, name) => {
1915
let context;
@@ -139,8 +135,74 @@ const deleteRelease = async ({
139135
return res.id;
140136
};
141137

138+
139+
const _extractFieldsForHelmRepoEntity = helmRepo => _.pick(helmRepo, 'Name', 'CreatedAt', 'Public', 'UpdatedAt');
140+
141+
const getAllRepos = async (options = {}) => {
142+
const qs = options.query || {};
143+
144+
const RequestOptions = {
145+
url: '/api/helm/repos',
146+
method: 'GET',
147+
qs,
148+
};
149+
150+
const result = await sendHttpRequest(RequestOptions);
151+
const helmRepos = [];
152+
_.forEach(result, (helmRepo) => {
153+
const data = _extractFieldsForHelmRepoEntity(helmRepo);
154+
helmRepos.push(new HelmRepo(data));
155+
});
156+
157+
return helmRepos;
158+
};
159+
160+
const getRepoByName = async (name) => {
161+
const RequestOptions = {
162+
url: `/api/helm/repos/${name}`,
163+
method: 'GET',
164+
};
165+
166+
const result = await sendHttpRequest(RequestOptions);
167+
return new HelmRepo(result);
168+
};
169+
170+
const createRepo = async (data) => {
171+
const RequestOptions = {
172+
url: '/api/helm/repos',
173+
method: 'POST',
174+
body: data,
175+
};
176+
177+
return sendHttpRequest(RequestOptions);
178+
};
179+
180+
const updateRepo = async (name, data) => {
181+
const RequestOptions = {
182+
url: `/api/helm/repos/${name}`,
183+
method: 'PUT',
184+
body: data,
185+
};
186+
187+
return sendHttpRequest(RequestOptions);
188+
};
189+
190+
const deleteRepo = async (name) => {
191+
const RequestOptions = {
192+
url: `/api/helm/repos/${name}`,
193+
method: 'DELETE',
194+
};
195+
196+
return sendHttpRequest(RequestOptions);
197+
};
198+
142199
module.exports = {
143200
installChart,
144201
testRelease,
145202
deleteRelease,
203+
getAllRepos,
204+
createRepo,
205+
deleteRepo,
206+
getRepoByName,
207+
updateRepo,
146208
};

lib/logic/api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const environment = require('./environment');
99
const workflow = require('./workflow');
1010
const log = require('./log');
1111
const version = require('./version');
12+
const helm = require('./helm');
1213

1314
module.exports = {
1415
user,
@@ -22,4 +23,5 @@ module.exports = {
2223
workflow,
2324
log,
2425
version,
26+
helm,
2527
};

lib/logic/entities/HelmRepo.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const moment = require('moment');
2+
const Entity = require('./Entity');
3+
4+
class Pipeline extends Entity {
5+
constructor(data) {
6+
super();
7+
this.entityType = 'helm-repo';
8+
this.info = data;
9+
this.name = this.info.Name;
10+
this.created = moment(this.info.CreatedAt).fromNow();
11+
this.updated = moment(this.info.UpdatedAt).fromNow();
12+
this.public = this.info.Public.toString();
13+
this.defaultColumns = ['name', 'public', 'created', 'updated'];
14+
this.wideColumns = this.defaultColumns.concat([]);
15+
}
16+
}
17+
18+
module.exports = Pipeline;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.8.48",
3+
"version": "0.8.49",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)