Skip to content

Commit

Permalink
Merge pull request #12 from Cox-Automotive/develop
Browse files Browse the repository at this point in the history
New output format, bug fixes and improved error handling.
  • Loading branch information
brianantonelli authored Sep 9, 2016
2 parents 26203d8 + dbf8f90 commit 49a47e4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Optional arguments:

* `-p [password]` Your password
* `-d [duration]` Duration of the key, in hours. Supports: 2, 6, 12, 18, 24, 36
* `-o [output]` Output format. Supports: `json`, `env`, `docker`, `creds`
* `-o [output]` Output format. Supports: `json`, `env`, `docker`, `creds`, `idea`
* `-n` If output is set to creds, use this named profile (defaults to default)
* `-f` If output is set to creds, force overwriting of AWS credentials if they already exist

Expand All @@ -118,7 +118,7 @@ Output values:
Optional arguments:

* `-p [password]` Your password
* `-o [output]` Output format. Supports: `json`, `env`, `docker`, `creds`
* `-o [output]` Output format. Supports: `json`, `env`, `docker`, `creds`, `idea`
* `-n` If output is set to creds, use this named profile (defaults to default)
* `-f` If output is set to creds, force overwriting of AWS credentials if they already exist

Expand All @@ -140,4 +140,5 @@ ALKS CLI will output in a variety of formats:
* `docker`: Outputs environment arguments to pass to a Docker run call
* `creds`: Updates the AWS credentials file
* By default this will update the default profile, to use another named profile supply: `-n namedProfile`
* If the named profile already exists you'll need to supply an override flag: `-f`
* If the named profile already exists you'll need to supply an override flag: `-f`
* `idea`: Outputs environment variables formatted for Intelli-J
17 changes: 11 additions & 6 deletions bin/alks-developer-configure
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ program
.description('configures developer')
.parse(process.argv);

function getPrompt(field, data, text, callback){
function getPrompt(field, data, text, validator, callback){
var promptConfig = {
name: field,
description: text,
required: true
};

if(!_.isEmpty(data[field])){
promptConfig.default = data[field];
}

if(_.isFunction(validator)){
promptConfig.conform = validator;
}

prompt.start();
prompt.message = '';
prompt.get([ promptConfig ], function(err, result){
Expand All @@ -38,7 +43,7 @@ function getPrompt(field, data, text, callback){
else{
callback(null, result[field]);
}
});
});
}

async.waterfall([
Expand All @@ -48,21 +53,21 @@ async.waterfall([
},
// ask for server
function(previousData, callback){
getPrompt('server', previousData, 'ALKS server', function(err, server){
getPrompt('server', previousData, 'ALKS server', utils.isURL, function(err, server){
callback(err, previousData, server);
});
},
// ask for username
function(previousData, server, callback){
getPrompt('userid', previousData, 'Network Username', function(err, userid){
getPrompt('userid', previousData, 'Network Username', null, function(err, userid){
callback(err, previousData, server, userid);
});
},
// ask for password
function(previousData, server, userid, callback){
Account.getPasswordFromPrompt(function(err, password){
callback(err, previousData, server, userid, password);
}, 'Network Password', false);
}, 'Network Password');
},
// ask if they want to save password
function(previousData, server, userid, password, callback){
Expand Down Expand Up @@ -103,7 +108,7 @@ async.waterfall([
], function(err, server, userid, password, savePassword, alksAccount, alksRole){
// did any of our steps have issues?
if(err){
return utils.errorAndExit('Error configuring account!', err);
return utils.errorAndExit('Error configuring account: ' + err.message);
}

var accountPayload = {
Expand Down
6 changes: 2 additions & 4 deletions bin/alks-developer-switch
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ program
async.waterfall([
// check to be sure were configured
function(callback){
Account.ensureConfigured(function(err, data){
if(err) utils.errorAndExitNoConfig();
else callback(null);
});
Account.ensureConfigured(callback);
},
// get account
function(callback){
Expand Down Expand Up @@ -81,5 +78,6 @@ async.waterfall([
if(err){
utils.errorAndExit(err);
}

utils.checkForUpdate();
});
7 changes: 5 additions & 2 deletions lib/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ var storePassword = exports.storePassword = function(password, callback){
login: ALKS_USERID,
password: password
});

callback(null, true);
}
};

Expand All @@ -84,15 +86,16 @@ exports.removePassword = function(){
}
};

var getPasswordFromPrompt = exports.getPasswordFromPrompt = function (callback, text, optional){
var getPasswordFromPrompt = exports.getPasswordFromPrompt = function (callback, text){
prompt.start();
prompt.message = '';

prompt.get([{
name: 'password',
description: text ? text : 'Password',
hidden: true,
replace: '*',
required: optional ? false : true
required: true
}], function(err, result){
if(err){
callback(err);
Expand Down
14 changes: 12 additions & 2 deletions lib/alks-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ exports.getAccountSelectorDelimiter = function(){
return ACCOUNT_SELECTION_DELIMITER;
};

var getMessageFromBadResponse = function(results){
if(results.body){
if(results.body.statusMessage){
return results.body.statusMessage;
}
}

return 'Bad response received, please check API URL.';
};

exports.createKey = function(account, password, duration, callback){
var payload = _.extend({
password: password,
Expand All @@ -35,7 +45,7 @@ exports.createKey = function(account, password, duration, callback){
return callback(err);
}
else if(results.statusCode !== 200){
return callback(new Error(results.body));
return callback(new Error(getMessageFromBadResponse(results)));
}

callback(null, {
Expand All @@ -57,7 +67,7 @@ exports.getAccounts = function(server, userid, password, callback){
return callback(err);
}
else if(results.statusCode !== 200){
return callback(new Error(results.body));
return callback(new Error(getMessageFromBadResponse(results)));
}

var accounts = [];
Expand Down
7 changes: 7 additions & 0 deletions lib/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ exports.getKeyOutput = function(format, key, profile, force){

return msg.join('');
}
else if(format === 'idea'){
return [
'AWS_ACCESS_KEY_ID='+key.accessKey,
'AWS_SECRET_ACCESS_KEY='+ key.secretKey,
'AWS_SESSION_TOKEN='+ key.sessionToken
].join('\n');
}
else{
return JSON.stringify(key, null, 4);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var getFilePathInHome = exports.getFilePathInHome = function(filename){
};

exports.getOutputValues = function(){
return [ 'json', 'env', 'docker', 'creds' ];
return [ 'json', 'env', 'docker', 'creds', 'idea' ];
};

exports.trim = function(str){
Expand Down Expand Up @@ -150,4 +150,10 @@ exports.passwordSaveErrorHandler = function(err){
if(process.platform === 'win32'){
console.error(clc.red('It looks like you\'re on Windows. This is most likely a script permission error. Please run: "Set-ExecutionPolicy -Scope CurrentUser remotesigned", press "Y" and try again.'));
}
};

exports.isURL = function(url){
var pattern = /(http|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/

return pattern.test(url);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alks",
"version": "1.5.0",
"version": "1.5.1",
"description": "CLI for working with ALKS",
"main": "bin/alks",
"scripts": {
Expand Down

0 comments on commit 49a47e4

Please sign in to comment.