Skip to content

Commit f452c8a

Browse files
author
David Blackman
committed
add --key_env
1 parent f68dc13 commit f452c8a

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@radarlabs/api-diff",
3-
"version": "1.0.34",
3+
"version": "1.0.35",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

src/api-diff/api-diff.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async function compareQuery({
4848
newApiEnv?: ApiEnv;
4949
query: Query;
5050
argv: CompareArgs;
51-
}): Promise<Change> {
51+
}): Promise<Change | undefined> {
5252
// if the query has a baseline response (running from a golden json file), use that
5353
// otherwise run it against the old server
5454
const oldResponse = query.baselineResponse
@@ -74,8 +74,12 @@ async function compareQuery({
7474
if (argv.response_filter) {
7575
const hadData = !_.isEmpty(oldResponse.data) || !_.isEmpty(newResponse.data);
7676

77-
oldResponse.data = jp.query(oldResponse.data, argv.response_filter);
78-
newResponse.data = jp.query(newResponse.data, argv.response_filter);
77+
try {
78+
oldResponse.data = jp.query(oldResponse.data, argv.response_filter);
79+
newResponse.data = jp.query(newResponse.data, argv.response_filter);
80+
} catch (e) {
81+
console.error(e);
82+
}
7983

8084
if (hadData && _.isEmpty(oldResponse.data) && _.isEmpty(newResponse.data)) {
8185
console.error(
@@ -152,6 +156,10 @@ async function compareQueries({
152156
throw e;
153157
});
154158

159+
if (!change) {
160+
return;
161+
}
162+
155163
formatter.queryCompleted(change);
156164

157165
oldResponseTimes.push(

src/apiEnv.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ type YargsOptionMapping = Record<string, yargs.Options>;
3131
const apiEnvCommandLineOptions: YargsOptionMapping = {
3232
host: {
3333
type: 'string',
34-
description: '(http|https://)hostname(:port) - protocol and port are optional, default to http/80',
34+
description:
35+
'(http|https://)hostname(:port) - protocol and port are optional, default to http/80',
3536
},
3637
key: {
3738
type: 'string',
@@ -48,6 +49,13 @@ const apiEnvCommandLineOptions: YargsOptionMapping = {
4849
* @returns {YargsOptionMapping} mapping of string to yargs option config
4950
*/
5051
export function getApiEnvCommandLineOptions(): YargsOptionMapping {
52+
const keyEnvs = _.flatMap(config.hosts, (hostEntry, hostEnv) => {
53+
if (hostEntry.keyEnv === hostEnv) {
54+
return [hostEnv];
55+
}
56+
return [];
57+
});
58+
5159
if (config.keyTypes) {
5260
apiEnvCommandLineOptions.key_type = {
5361
choices: config.keyTypes,
@@ -56,9 +64,18 @@ export function getApiEnvCommandLineOptions(): YargsOptionMapping {
5664
};
5765
}
5866

67+
apiEnvCommandLineOptions.key_env = {
68+
choices: keyEnvs,
69+
description:
70+
'authorization key env to use, defaults to the env specified by the host entry',
71+
};
72+
5973
if (config.hosts) {
6074
apiEnvCommandLineOptions.env = {
61-
choices: [..._.keys(config.hosts), ..._.flatMap(config.hosts, (v) => v.aliases || [])],
75+
choices: [
76+
..._.keys(config.hosts),
77+
..._.flatMap(config.hosts, (v) => v.aliases || []),
78+
],
6279
description: 'api host to talk to',
6380
};
6481

@@ -92,7 +109,9 @@ export function findApiKey(
92109
console.error(`Looking for key in env ${envVariableName}`);
93110
const key = process.env[envVariableName];
94111
if (!key && exitOnFailure) {
95-
failedExit(`No key found for ${envVariableName} in .env and --key not specified`);
112+
failedExit(
113+
`No key found for ${envVariableName} in .env and --key not specified`,
114+
);
96115
}
97116
return key;
98117
}
@@ -119,7 +138,9 @@ export function argvToApiEnv(
119138
// This gets triggered if a user specifies more than one hostEntry command
120139
// line option, like --prod and --staging (if both are defined in their config)
121140
if (aliasedHostEntry) {
122-
throw new Error(`Can only specify one of ${_.keys(config.hosts).join(',')}`);
141+
throw new Error(
142+
`Can only specify one of ${_.keys(config.hosts).join(',')}`,
143+
);
123144
}
124145

125146
// If this entry takes an argument, replace uppercase(hostConfigEntryName)
@@ -134,7 +155,7 @@ export function argvToApiEnv(
134155
// keyEnv is either the env specified in the hostEntry or just the
135156
// name of the hostConfig. For example, localhost might specify keyEnv: staging,
136157
// while the hostConfig for "staging" wouldn't need to do so
137-
hostEntry.keyEnv = hostEntry.keyEnv || hostKey;
158+
hostEntry.keyEnv = argv.keyEnv || hostEntry.keyEnv || hostKey;
138159

139160
hostEntry.keyType = hostEntry.keyType || _.first(config.keyTypes);
140161

@@ -158,12 +179,21 @@ export function argvToApiEnv(
158179
apiEnv.protocol = apiEnv?.protocol || 'http';
159180

160181
if (!apiEnv.host && exitOnFailure) {
161-
failedExit(`Could not find host via arguments specified ${JSON.stringify(argv || {}, null, 2)}`);
182+
failedExit(
183+
`Could not find host via arguments specified ${JSON.stringify(
184+
argv || {},
185+
null,
186+
2,
187+
)}`,
188+
);
162189
}
163190

164191
if (config.authStyle) {
165192
apiEnv.key = apiEnv.key
166-
|| findApiKey({ keyEnv: apiEnv.keyEnv, keyType: apiEnv.keyType }, exitOnFailure);
193+
|| findApiKey(
194+
{ keyEnv: apiEnv.keyEnv, keyType: apiEnv.keyType },
195+
exitOnFailure,
196+
);
167197
}
168198

169199
return apiEnv as ApiEnv;

0 commit comments

Comments
 (0)