Skip to content

Commit 5910d8c

Browse files
Add beta features (#82)
1 parent eb45544 commit 5910d8c

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

lib/interface/cli/Command.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ const yargs = require('yargs');
22
const assert = require('assert');
33
const _ = require('lodash');
44
const { printError, wrapHandler } = require('./helpers/general');
5+
const authManager = require('../../logic').auth.manager;
56

67

78
class Command {
89
constructor(command) {
910
this.root = command.root;
1011
delete command.root;
1112

13+
this.betaCommand = command.betaCommand || false;
1214
this.command = command;
1315
this.subCommands = [];
1416
this.builders = [];
@@ -26,6 +28,10 @@ class Command {
2628
return this.root;
2729
}
2830

31+
isBetaCommand() {
32+
return this.betaCommand;
33+
}
34+
2935
addBuilder(b) {
3036
this.builders.push(b);
3137
}
@@ -44,7 +50,7 @@ class Command {
4450
b(yargs);
4551
_.forEach(this.subCommands, (subCommand) => {
4652
// TODO use .toCommand here too
47-
if (subCommand instanceof Command) {
53+
if (subCommand instanceof Command && (!subCommand.isBetaCommand() || authManager.getCurrentContext().beta)) {
4854
yargs.command(subCommand.toCommand());
4955
} else {
5056
yargs.command(subCommand);

lib/interface/cli/codefresh

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ process.on('unhandledRejection', error => {
2121

2222
recursive(path.resolve(__dirname, 'commands'), (err, files) => {
2323
const rootCommands = [];
24-
25-
_.forEach(files, (file) => {
26-
if (file.endsWith('.cmd.js')) {
27-
const command = require(file);
28-
if (command.isRoot()) {
29-
rootCommands.push(command);
30-
}
31-
}
32-
});
33-
3424
yargs
3525
.env('')
3626
.options('cfconfig', {
@@ -40,15 +30,28 @@ recursive(path.resolve(__dirname, 'commands'), (err, files) => {
4030
.config('cfconfig', 'Custom path for authentication contexts config file', (configFilePath) => {
4131
try {
4232
authManager.loadContexts(configFilePath, process.env.CF_API_KEY, process.env.CF_URL || DEFAULTS.URL);
33+
if (_.isEmpty(authManager.getAllContexts())) {
34+
printError('Unauthorized error: Please create an authentication context (see codefresh auth create-context --help)');
35+
process.exit(1);
36+
}
37+
_.forEach(files, (file) => {
38+
if (file.endsWith('.cmd.js')) {
39+
const command = require(file);
40+
if (command.isRoot() && (!command.isBetaCommand() || authManager.getCurrentContext().beta)) {
41+
rootCommands.push(command);
42+
}
43+
}
44+
});
45+
_.forEach(rootCommands, (command) => {
46+
yargs.command(command.toCommand());
47+
});
4348
} catch (err) {
4449
printError(err);
4550
process.exit(1);
4651
}
4752
});
4853

49-
_.forEach(rootCommands, (command) => {
50-
yargs.command(command.toCommand());
51-
});
54+
5255

5356

5457
yargs // eslint-disable-line

lib/logic/auth/contexts/APIKeyContext.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class APIKeyContext extends Context {
1212
this.token = options.token;
1313
this.name = options.name || this.name;
1414
this.defaultColumns = ['current', 'name', 'url'];
15+
this.beta = options.beta || this.beta;
1516
}
1617

1718
prepareHttpOptions() {
@@ -42,6 +43,7 @@ class APIKeyContext extends Context {
4243
serialize() {
4344
const data = {
4445
token: this.token,
46+
beta: this.beta,
4547
};
4648

4749
return _.assignIn(super.serialize(), data);
@@ -52,6 +54,7 @@ class APIKeyContext extends Context {
5254
name: rawContext.name,
5355
url: rawContext.url,
5456
token: rawContext.token,
57+
beta: rawContext.beta,
5558
});
5659
}
5760

lib/logic/auth/contexts/Context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Context {
55
constructor(options) {
66
this.name = options.name || uuidv4();
77
this.url = options.url;
8+
this.beta = false;
89
}
910

1011
setName(name) {

lib/logic/auth/contexts/JWTContext.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class JWTContext extends Context {
1919
this.expires = new Date(options.expires * 1000);
2020
this.name = options.name || this.name;
2121
this.defaultColumns = ['current', 'name', 'url'];
22+
this.beta = options.beta || this.beta;
2223
}
2324

2425
_reCalculateName() {
@@ -65,6 +66,7 @@ class JWTContext extends Context {
6566
'user-id': this.userId,
6667
'account-id': this.accountId,
6768
expires: this.expires.getTime() / 1000,
69+
beta: this.beta,
6870
};
6971

7072
if (this.userName) {
@@ -89,6 +91,7 @@ class JWTContext extends Context {
8991
accountId: rawContext['account-id'],
9092
accountName: rawContext['account-name'],
9193
expires: rawContext.expires,
94+
beta: rawContext.beta,
9295
});
9396
}
9497

0 commit comments

Comments
 (0)