-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·176 lines (152 loc) · 5.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
/*
* Copyright (c) 2015-2016 Khang Nguyen
* MIT Licensed
*/
var _ = require('lodash');
var Swagger = require('swagger-client');
var winston = require('winston');
var fs = require('fs');
var chalk = require('chalk');
var validUrl = require('url-validator');
var path = require('path');
// Custom libs
var CONFIG_FILE_PATH = __dirname + '/config/config.json';
var LOCAL_CONFIG_NAME = '.swagger-commander.json';
var config = require(CONFIG_FILE_PATH);
var splash = require('./lib/splash');
var apiCommander = require('./lib/apiCommander');
var apiOperationCommander = require('./lib/apiOperationCommander');
var authConfigParser = require('./lib/parseAuthConfig');
var configHelp = require('./lib/configHelp');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
colorize: true,
level: 'info'
})
]
});
// Check current working directory for local config file
var localConfig = getLocalConfig('./' + LOCAL_CONFIG_NAME);
if (localConfig) {
config = localConfig;
}
apiCommander.init(logger);
apiOperationCommander.init(logger);
if (process.argv[2] === 'set-swagger-url') {
if (process.argv[3]) {
setSwaggerUrlConfig(process.argv[3]);
if (localConfig) {
logger.warn('Local swagger file detected in current directory, ' +
'the swagger-url in the local file has not been changed, you must do that manually');
}
} else {
logger.error('No swagger file URL given');
}
return;
}
var swagSpecURL = config.swagger_spec_url;
if (!swagSpecURL) {
logger.error('Swagger spec URL not set');
return;
}
// parse auth config
var authObj;
if (_.has(config, 'auth')) {
authObj = authConfigParser(_.get(config, 'auth'), logger);
}
//swagSpecURL = 'http://petstore.swagger.io/v2/swagger.json'; // DEMO url
new Swagger({
url: swagSpecURL,
authorizations: authObj,
usePromise: true
}
).then(commanderSetup)
.catch(function (err) {
logger.error('!Error connecting to Swagger file URL: ' + swagSpecURL);
logger.error(err);
configHelp();
});
/*************
* Methods *
*************/
function commanderSetup(client) {
var parentCommand = process.argv[2];
// If first argument begins with "-", then we are specifying
// an option in apiCommander
if (parentCommand && parentCommand.charAt(0) === '-') {
// TODO this assumes that resources cannot begin with -
parentCommand = null;
}
// Split by "tags"
var apis = client.apis;
// Commander can't contain white spaces in commands,
// so we need to replace API keys that have whitespaces
var keyNoWhiteSpace;
_.each(apis, function (obj, key) {
if (hasWhitespace(key) === true) {
keyNoWhiteSpace = key.replace(/ /g, '');
apis[keyNoWhiteSpace] = obj;
delete apis[key];
}
});
var unrecognizedParentCommand = parentCommand && !_.has(apis, parentCommand);
// If no parentCommand, create the commander that just shows the available resources
// Otherwise, if parentCommand given, create subCommand program that shows the operations for that resource
if (!parentCommand || unrecognizedParentCommand) {
if (unrecognizedParentCommand && (parentCommand !== '-h') && (parentCommand !== '--help')) {
logger.error('Unrecognized parent command: ' + parentCommand);
}
logAdditionalInfo(true, client);
apiCommander.setupParentcommand(process.argv, client, apis);
} else if (parentCommand) {
logAdditionalInfo(false);
apiOperationCommander.setupSubcommand(process.argv,
apis[parentCommand],
client.clientAuthorizations,
parentCommand);
}
// Commander in apiCommander and apiOperationCommander never return the thread
// so any code here is unreachable
}
function setSwaggerUrlConfig(swaggerFileUrl) {
if (!validUrl(swaggerFileUrl)) {
logger.warn('this URL does not appear to be valid');
}
var swagConfig = {
'swagger_spec_url': swaggerFileUrl
};
fs.writeFile(CONFIG_FILE_PATH, JSON.stringify(swagConfig), function (err) {
if (err) {
logger.info('There has been an error setting Swagger file URL.');
logger.error(err.message);
return;
}
logger.info('Swagger file URL changed to: ', swaggerFileUrl);
});
}
function getLocalConfig(filePath) {
try {
var resolvePath = path.resolve(filePath);
var localConfig = require(resolvePath);
return localConfig;
} catch (e) {
return null;
}
}
function hasWhitespace(s) {
return s.indexOf(' ') >= 0;
}
function logAdditionalInfo(showStartupInfo, client) {
if (showStartupInfo) {
// Show splash and version info
console.log(chalk.green(splash.ascii) + '\n'); // show splash
if (client.info && client.info.title) {
console.log(client.info.title + ' ' + client.info.version);
}
}
if (localConfig) {
logger.info('Using local swagger-config file "' + LOCAL_CONFIG_NAME + '" in current directory');
}
}