Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
v0.1.10 Added ability to override base path
Browse files Browse the repository at this point in the history
  • Loading branch information
unsetbit committed Aug 22, 2014
1 parent d0eaf6d commit c05c6bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-cli-client",
"version": "0.1.9",
"version": "0.1.10",
"description": "Generates a command-line interface for any Swagger Specification",
"keywords": [],
"main": "./src/client.js",
Expand Down
38 changes: 34 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ var clientGenerator = require('swagger-node-client'),
printResources = require('./printResources');

module.exports = function(schema){
var api = clientGenerator(schema);
var authMethodName = api.authorization ? 'authorization' : 'auth';
var authMethod = api[authMethodName];
var args = minimist(process.argv.slice(2));
var appName = path.basename(process.argv[1]);

var resourceName = args._.shift();
var operationName = args._.shift();

var basePathOverride = args.basePathOverride || tryToGetBasePathOverride();
if(basePathOverride) {
print.ln();
print.ln('Overridding base API path to %s'.red, basePathOverride);
print.ln();
// Override the base path
schema.apis.forEach(function(api){
api.apiDeclaration.basePath = basePathOverride;
});
}

var api = clientGenerator(schema);
var authMethodName = api.authorization ? 'authorization' : 'auth';
var authMethod = api[authMethodName];

if(args.v) return printVersion(schema);

var auth = args.auth || tryToGetAuth();
Expand Down Expand Up @@ -60,6 +71,25 @@ module.exports = function(schema){
}
};

function tryToGetBasePathOverride(){
var appName = path.basename(process.argv[1]);

// Attempt to get it from the env
var envVar = appName.replace(/\W/g, '').toUpperCase() + '_BASE_PATH';
var auth = process.env[envVar];
if(auth) return auth;

// Attempt to get it form the ~/.<appname> json file
var homeDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
var configFile = path.resolve(homeDir, '.' + appName);
try {
var config = yaml.safeLoad(fs.readFileSync(configFile));
return config.basePath;
} catch(e){
// it's ok to fail here
}
}

function tryToGetAuth(){
var appName = path.basename(process.argv[1]);

Expand Down

0 comments on commit c05c6bd

Please sign in to comment.