Skip to content

Commit 284e484

Browse files
author
Oleg Sucharevich
authored
Add cmd to generate codefresh runtime environment token (#257)
* Add cmd to create codefresh runtime environment token
1 parent b87504f commit 284e484

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const debug = require('debug')('codefresh:cli:create:token');
2+
const Command = require('../../Command');
3+
const createCmd = require('../root/create.cmd');
4+
const {
5+
generateToken,
6+
} = require('../../../../logic/api/token');
7+
8+
const command = new Command({
9+
command: 'tokens',
10+
aliases: ['token'],
11+
parent: createCmd,
12+
description: 'Create Codefresh token',
13+
usage: 'Create Codefresh token',
14+
webDocs: {
15+
category: 'Tokens',
16+
title: 'Tokens tokens',
17+
},
18+
builder: (yargs) => {
19+
return yargs
20+
.option('subject', {
21+
describe: 'name of the token subject',
22+
required: true,
23+
})
24+
.options('type', {
25+
describe: 'Type of the subject',
26+
default: 'runtime-environment',
27+
options: ['runtime-environment'],
28+
})
29+
.option('name', {
30+
describe: 'token name',
31+
required: true,
32+
})
33+
.example('codefresh create token --subject my-k8s-cluster/namespace --name new-token', 'Create token form runtime environment');
34+
},
35+
handler: async (argv) => {
36+
const res = await generateToken({
37+
subjectType: argv.type,
38+
subjectReference: argv.subject,
39+
name: argv.name,
40+
});
41+
console.log('Token created');
42+
console.log(res.token);
43+
},
44+
});
45+
46+
module.exports = command;
47+

lib/logic/api/token.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const _ = require('lodash');
2+
const { sendHttpRequest } = require('./helper');
3+
4+
const generateToken = async (info) => {
5+
const options = {
6+
url: '/api/auth/key',
7+
method: 'POST',
8+
qs: {
9+
subjectType: info.subjectType,
10+
subjectReference: info.subjectReference,
11+
},
12+
body: {
13+
name: info.name,
14+
},
15+
};
16+
const token = await sendHttpRequest(options);
17+
return {
18+
token,
19+
};
20+
};
21+
22+
23+
module.exports = {
24+
generateToken,
25+
};

0 commit comments

Comments
 (0)