@@ -31,7 +31,8 @@ type YargsOptionMapping = Record<string, yargs.Options>;
31
31
const apiEnvCommandLineOptions : YargsOptionMapping = {
32
32
host : {
33
33
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' ,
35
36
} ,
36
37
key : {
37
38
type : 'string' ,
@@ -48,6 +49,13 @@ const apiEnvCommandLineOptions: YargsOptionMapping = {
48
49
* @returns {YargsOptionMapping } mapping of string to yargs option config
49
50
*/
50
51
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
+
51
59
if ( config . keyTypes ) {
52
60
apiEnvCommandLineOptions . key_type = {
53
61
choices : config . keyTypes ,
@@ -56,9 +64,18 @@ export function getApiEnvCommandLineOptions(): YargsOptionMapping {
56
64
} ;
57
65
}
58
66
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
+
59
73
if ( config . hosts ) {
60
74
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
+ ] ,
62
79
description : 'api host to talk to' ,
63
80
} ;
64
81
@@ -92,7 +109,9 @@ export function findApiKey(
92
109
console . error ( `Looking for key in env ${ envVariableName } ` ) ;
93
110
const key = process . env [ envVariableName ] ;
94
111
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
+ ) ;
96
115
}
97
116
return key ;
98
117
}
@@ -119,7 +138,9 @@ export function argvToApiEnv(
119
138
// This gets triggered if a user specifies more than one hostEntry command
120
139
// line option, like --prod and --staging (if both are defined in their config)
121
140
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
+ ) ;
123
144
}
124
145
125
146
// If this entry takes an argument, replace uppercase(hostConfigEntryName)
@@ -134,7 +155,7 @@ export function argvToApiEnv(
134
155
// keyEnv is either the env specified in the hostEntry or just the
135
156
// name of the hostConfig. For example, localhost might specify keyEnv: staging,
136
157
// 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 ;
138
159
139
160
hostEntry . keyType = hostEntry . keyType || _ . first ( config . keyTypes ) ;
140
161
@@ -158,12 +179,21 @@ export function argvToApiEnv(
158
179
apiEnv . protocol = apiEnv ?. protocol || 'http' ;
159
180
160
181
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
+ ) ;
162
189
}
163
190
164
191
if ( config . authStyle ) {
165
192
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
+ ) ;
167
197
}
168
198
169
199
return apiEnv as ApiEnv ;
0 commit comments