|
| 1 | +import * as _ from "lodash"; |
| 2 | +import logger from "./logger"; |
| 3 | +import { ConfigHostEntry } from "./config"; |
| 4 | +import config from "./config"; |
| 5 | +import { failedExit } from "./cli-utils"; |
| 6 | + |
| 7 | +const apiEnvCommandLineOptions: Record<string, any> = { |
| 8 | + host: { |
| 9 | + type: "string", |
| 10 | + description: "Host/port - will override --env", |
| 11 | + }, |
| 12 | + protocol: { |
| 13 | + choices: ["http", "https"], |
| 14 | + description: |
| 15 | + "What protocol to use (if not specified in url), defaults to http for local, https otherwise", |
| 16 | + }, |
| 17 | + key: { |
| 18 | + type: "string", |
| 19 | + description: `Authorization key, if not specified will try to find one in the env, in .env or, in local mode, directly in mongo`, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +export function getApiEnvCommandLineOptions(): Record<string, any> { |
| 24 | + if (config.keyTypes) { |
| 25 | + apiEnvCommandLineOptions.key_type = { |
| 26 | + choices: config.keyTypes, |
| 27 | + default: config.keyTypes[0], |
| 28 | + description: "authorization key type to use", |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + if (config.hosts) { |
| 33 | + apiEnvCommandLineOptions.env = { |
| 34 | + choices: [..._.keys(config.hosts), ..._.flatMap(config.hosts, (v) => v.aliases || [])], |
| 35 | + description: `api host to talk to`, |
| 36 | + }; |
| 37 | + |
| 38 | + _.forEach(config.hosts, (hostEntry: ConfigHostEntry, key: string) => { |
| 39 | + apiEnvCommandLineOptions[key] = { |
| 40 | + alias: hostEntry.aliases, |
| 41 | + description: `set host to ${hostEntry.host}`, |
| 42 | + }; |
| 43 | + }); |
| 44 | + } |
| 45 | + return apiEnvCommandLineOptions; |
| 46 | +} |
| 47 | + |
| 48 | +export interface ApiEnv { |
| 49 | + key: string; |
| 50 | + protocol: string; |
| 51 | + host: string; |
| 52 | + keyEnv: string; |
| 53 | + keyType?: string; |
| 54 | +} |
| 55 | + |
| 56 | +type KeyParams = Pick<ApiEnv, "keyEnv" | "keyType">; |
| 57 | + |
| 58 | +function findKey({ keyEnv, keyType }: KeyParams): string { |
| 59 | + require("dotenv").config(); |
| 60 | + |
| 61 | + const env_variable_name = [keyEnv, keyType, "API_KEY"] |
| 62 | + .filter((s) => !_.isEmpty(s)) |
| 63 | + .join("_") |
| 64 | + .toUpperCase(); |
| 65 | + logger.info(`Looking for key in env ${env_variable_name}`); |
| 66 | + const key = process.env[env_variable_name]; |
| 67 | + if (!key) { |
| 68 | + failedExit(`No key found for ${env_variable_name} in .env and --key not specified`); |
| 69 | + } |
| 70 | + return key; |
| 71 | +} |
| 72 | + |
| 73 | +export function fixApiEnvKey(apiEnv: Partial<ApiEnv>) { |
| 74 | + apiEnv.key = |
| 75 | + apiEnv.key || |
| 76 | + findKey({ keyEnv: apiEnv.keyEnv || "", keyType: apiEnv.keyType || config.keyTypes?.[0] }); |
| 77 | +} |
| 78 | + |
| 79 | +export function argvToApiEnv(argv: any): ApiEnv { |
| 80 | + let apiEnv: Partial<ApiEnv> = _.clone(argv); |
| 81 | + |
| 82 | + let aliasedHostEntry: ConfigHostEntry; |
| 83 | + _.forEach(config.hosts, (hostEntry: ConfigHostEntry, key: string) => { |
| 84 | + if (argv[key]) { |
| 85 | + if (aliasedHostEntry) { |
| 86 | + throw new Error(`Can only specify one of ${_.keys(config.hosts).join(",")}`); |
| 87 | + } |
| 88 | + |
| 89 | + if (hostEntry.takesArg) { |
| 90 | + const toReplace = key.toUpperCase(); |
| 91 | + hostEntry.host = hostEntry.host.replace(toReplace, argv[key]); |
| 92 | + } |
| 93 | + |
| 94 | + hostEntry.keyEnv = hostEntry.keyEnv || key; |
| 95 | + |
| 96 | + aliasedHostEntry = hostEntry; |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + if (aliasedHostEntry) { |
| 101 | + apiEnv = { |
| 102 | + ...aliasedHostEntry, |
| 103 | + ...apiEnv, |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + if (apiEnv.host.startsWith("http")) { |
| 108 | + const url = new URL(apiEnv.host); |
| 109 | + apiEnv.host = url.host; |
| 110 | + apiEnv.protocol = url.protocol.replace(":", ""); |
| 111 | + } |
| 112 | + |
| 113 | + fixApiEnvKey(apiEnv); |
| 114 | + |
| 115 | + return apiEnv as ApiEnv; |
| 116 | +} |
0 commit comments