Skip to content

Commit 341c44a

Browse files
ziv-codefreshitai-codefresh
authored andcommitted
Fix initial authentication issue (#91)
1 parent cca1878 commit 341c44a

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

lib/interface/cli/Command.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ class Command {
99
constructor(command) {
1010
this.root = command.root;
1111
delete command.root;
12-
12+
if (command.hasOwnProperty('requiresAuthentication')) {
13+
this.requiresAuthentication = command.requiresAuthentication;
14+
}
1315
this.betaCommand = command.betaCommand || false;
1416
this.command = command;
1517
this.subCommands = [];
@@ -43,17 +45,25 @@ class Command {
4345

4446
toCommand() {
4547
let command = this.command;
46-
//assert(_.get(this.command, "builder", true));
4748

4849
let funcs = _.map(this.builders, (b) => {
4950
return (yargs) => {
5051
b(yargs);
5152
_.forEach(this.subCommands, (subCommand) => {
52-
// TODO use .toCommand here too
53-
if (subCommand instanceof Command && (!subCommand.isBetaCommand() || authManager.getCurrentContext().isBetaFeatEnabled())) {
54-
yargs.command(subCommand.toCommand());
53+
if (!subCommand.hasOwnProperty('requiresAuthentication') && command.hasOwnProperty('requiresAuthentication')) {
54+
subCommand.requiresAuthentication = command.requiresAuthentication;
55+
} else {
56+
subCommand.requiresAuthentication = true;
57+
}
58+
59+
if (subCommand.isBetaCommand()) {
60+
// load beta commands only if authentication exists and it is beta enabled
61+
const currentContext = authManager.getCurrentContext();
62+
if (currentContext && currentContext.isBetaFeatEnabled()) {
63+
yargs.command(subCommand.toCommand());
64+
}
5565
} else {
56-
yargs.command(subCommand);
66+
yargs.command(subCommand.toCommand());
5767
}
5868
});
5969
return yargs;
@@ -65,7 +75,7 @@ class Command {
6575
};
6676

6777
command.builder = builder;
68-
command.handler = wrapHandler(command.handler);
78+
command.handler = wrapHandler(command.handler, this.requiresAuthentication);
6979
return command;
7080

7181
}

lib/interface/cli/codefresh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ recursive(path.resolve(__dirname, 'commands'), (err, files) => {
3030
.config('cfconfig', 'Custom path for authentication contexts config file', (configFilePath) => {
3131
try {
3232
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-
}
3733
_.forEach(files, (file) => {
3834
if (file.endsWith('.cmd.js')) {
3935
const command = require(file);
40-
if (command.isRoot() && (!command.isBetaCommand() || authManager.getCurrentContext().beta)) {
41-
rootCommands.push(command);
36+
if (command.isRoot()) {
37+
if (command.isBetaCommand()) {
38+
const currentContext = authManager.getCurrentContext();
39+
if (currentContext && currentContext.isBetaFeatEnabled()) {
40+
rootCommands.push(command);
41+
}
42+
} else {
43+
rootCommands.push(command);
44+
}
4245
}
4346
}
4447
});
@@ -65,7 +68,7 @@ recursive(path.resolve(__dirname, 'commands'), (err, files) => {
6568
\\____|\\___/ \\__,_|\\___|_| _| \\___|____/_| |_| \\____|_____|___|
6669
6770
68-
For more information, find our official documentation at http://cli.codefresh.io
71+
For more information, find our official documentation at https://github.com/codefresh-io/cli
6972
`)
7073
// .option('help', {
7174
// global: false,

lib/interface/cli/commands/root/auth.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Command = require('../../Command');
22

33
const auth = new Command({
44
root: true,
5+
requiresAuthentication: false,
56
command: 'auth',
67
description: 'Manage authentication contexts',
78
builder: (yargs) => {

lib/interface/cli/commands/root/version.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const cliPackage = require('./../../../../../package.json');
33

44
const version = new Command({
55
root: true,
6+
requiresAuthentication: false,
67
command: 'version',
78
description: 'Print version',
89
builder: (yargs) => {

lib/interface/cli/helpers/general.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ const printError = (error) => {
1515
}
1616
};
1717

18-
const wrapHandler = (handler) => {
18+
const wrapHandler = (handler, requiresAuthentication) => {
1919
return async (argv) => {
2020
try {
21+
const authManager = require('../../../logic').auth.manager;
22+
if (requiresAuthentication && !authManager.getCurrentContext()) {
23+
printError('Authentication error: Please create an authentication context (see codefresh auth create-context --help)');
24+
process.exit(1);
25+
}
26+
2127
if (!argv.watch) {
2228
await handler(argv);
2329
return;

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.6.6",
3+
"version": "0.6.7",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)