|
| 1 | +import { extra, autoService } from 'knifecycle'; |
| 2 | +import { readArgs } from '@whook/whook'; |
| 3 | +import { getOpenAPIOperations } from '@whook/http-router'; |
| 4 | +import { YError } from 'yerror'; |
| 5 | +import { exec } from 'child_process'; |
| 6 | +import crypto from 'crypto'; |
| 7 | +import yaml from 'js-yaml'; |
| 8 | +import type { ExecException } from 'child_process'; |
| 9 | +import type { LogService } from 'common-services'; |
| 10 | +import type { |
| 11 | + WhookCommandArgs, |
| 12 | + WhookCommandDefinition, |
| 13 | + WhookAPIHandlerDefinition, |
| 14 | +} from '@whook/whook'; |
| 15 | +import type { OpenAPIV3_1 } from 'openapi-types'; |
| 16 | + |
| 17 | +export const definition: WhookCommandDefinition = { |
| 18 | + description: 'A command printing functions informations for Terraform', |
| 19 | + example: `whook terraformValues --type paths`, |
| 20 | + arguments: { |
| 21 | + type: 'object', |
| 22 | + additionalProperties: false, |
| 23 | + required: ['type'], |
| 24 | + properties: { |
| 25 | + type: { |
| 26 | + description: 'Type of values to return', |
| 27 | + type: 'string', |
| 28 | + enum: ['globals', 'paths', 'functions', 'function'], |
| 29 | + }, |
| 30 | + pretty: { |
| 31 | + description: 'Pretty print JSON values', |
| 32 | + type: 'boolean', |
| 33 | + }, |
| 34 | + functionName: { |
| 35 | + description: 'Name of the function', |
| 36 | + type: 'string', |
| 37 | + }, |
| 38 | + pathsIndex: { |
| 39 | + description: 'Index of the paths to retrieve', |
| 40 | + type: 'number', |
| 41 | + }, |
| 42 | + functionType: { |
| 43 | + description: 'Types of the functions to return', |
| 44 | + type: 'string', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +export default extra(definition, autoService(initTerraformValuesCommand)); |
| 51 | + |
| 52 | +async function initTerraformValuesCommand({ |
| 53 | + API, |
| 54 | + BASE_PATH, |
| 55 | + log, |
| 56 | + args, |
| 57 | + execAsync = _execAsync, |
| 58 | +}: { |
| 59 | + API: OpenAPIV3_1.Document; |
| 60 | + BASE_PATH: string; |
| 61 | + log: LogService; |
| 62 | + args: WhookCommandArgs; |
| 63 | + execAsync: typeof _execAsync; |
| 64 | +}) { |
| 65 | + return async () => { |
| 66 | + const { |
| 67 | + namedArguments: { type, pretty, functionName, functionType }, |
| 68 | + } = readArgs<{ |
| 69 | + type: string; |
| 70 | + pretty: boolean; |
| 71 | + functionName: string; |
| 72 | + functionType: string; |
| 73 | + }>(definition.arguments, args); |
| 74 | + const operations = |
| 75 | + getOpenAPIOperations<WhookAPIHandlerDefinition['operation']['x-whook']>( |
| 76 | + API, |
| 77 | + ); |
| 78 | + const configurations = operations.map((operation) => { |
| 79 | + const whookConfiguration = (operation['x-whook'] || { |
| 80 | + type: 'http', |
| 81 | + }) as WhookAPIHandlerDefinition['operation']['x-whook']; |
| 82 | + const configuration = { |
| 83 | + type: 'http', |
| 84 | + timeout: '10', |
| 85 | + memory: '128', |
| 86 | + description: operation.summary || '', |
| 87 | + enabled: 'true', |
| 88 | + sourceOperationId: operation.operationId, |
| 89 | + suffix: '', |
| 90 | + ...Object.keys(whookConfiguration || {}).reduce( |
| 91 | + (accConfigurations, key) => ({ |
| 92 | + ...accConfigurations, |
| 93 | + [key]: ( |
| 94 | + ( |
| 95 | + whookConfiguration as NonNullable< |
| 96 | + WhookAPIHandlerDefinition['operation']['x-whook'] |
| 97 | + > |
| 98 | + )[key] as string |
| 99 | + ).toString(), |
| 100 | + }), |
| 101 | + {}, |
| 102 | + ), |
| 103 | + }; |
| 104 | + const qualifiedOperationId = |
| 105 | + (configuration.sourceOperationId || operation.operationId) + |
| 106 | + (configuration.suffix || ''); |
| 107 | + |
| 108 | + return { |
| 109 | + qualifiedOperationId, |
| 110 | + method: operation.method.toUpperCase(), |
| 111 | + path: operation.path, |
| 112 | + ...configuration, |
| 113 | + }; |
| 114 | + }); |
| 115 | + |
| 116 | + if (type === 'globals') { |
| 117 | + const commitHash = await execAsync(`git rev-parse HEAD`); |
| 118 | + const commitMessage = ( |
| 119 | + await execAsync(`git rev-list --format=%B --max-count=1 HEAD`) |
| 120 | + ).split('\n')[1]; |
| 121 | + const openapi2 = yaml.safeDump({ |
| 122 | + swagger: '2.0', |
| 123 | + info: { |
| 124 | + title: API.info.title, |
| 125 | + description: API.info.description, |
| 126 | + version: API.info.version, |
| 127 | + }, |
| 128 | + host: '${infos_host}', |
| 129 | + basePath: BASE_PATH, |
| 130 | + schemes: ['https'], |
| 131 | + produces: ['application/json'], |
| 132 | + paths: configurations.reduce((accPaths, configuration) => { |
| 133 | + const operation = operations.find( |
| 134 | + ({ operationId }) => |
| 135 | + operationId === configuration.sourceOperationId, |
| 136 | + ); |
| 137 | + |
| 138 | + return { |
| 139 | + ...accPaths, |
| 140 | + [configuration.path]: { |
| 141 | + ...(accPaths[configuration.path] || {}), |
| 142 | + [configuration.method.toLowerCase()]: { |
| 143 | + summary: configuration.description || '', |
| 144 | + operationId: configuration.qualifiedOperationId, |
| 145 | + ...((operation?.parameters || []).length |
| 146 | + ? { |
| 147 | + parameters: ( |
| 148 | + operation?.parameters as OpenAPIV3_1.ParameterObject[] |
| 149 | + ).map(({ in: theIn, name, required }) => ({ |
| 150 | + in: theIn, |
| 151 | + name, |
| 152 | + type: 'string', |
| 153 | + required: required || false, |
| 154 | + })), |
| 155 | + } |
| 156 | + : undefined), |
| 157 | + 'x-google-backend': { |
| 158 | + address: `\${function_${configuration.qualifiedOperationId}}`, |
| 159 | + }, |
| 160 | + responses: { |
| 161 | + '200': { description: 'x', schema: { type: 'string' } }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }; |
| 166 | + }, {}), |
| 167 | + }); |
| 168 | + const openapiHash = crypto |
| 169 | + .createHash('md5') |
| 170 | + .update(JSON.stringify(API)) |
| 171 | + .digest('hex'); |
| 172 | + const infos = { |
| 173 | + commitHash, |
| 174 | + commitMessage, |
| 175 | + openapi2, |
| 176 | + openapiHash, |
| 177 | + }; |
| 178 | + log('info', JSON.stringify(infos)); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + if (type === 'functions') { |
| 183 | + const functions = configurations |
| 184 | + .filter((configuration) => |
| 185 | + functionType ? configuration.type === functionType : true, |
| 186 | + ) |
| 187 | + .reduce( |
| 188 | + (accLambdas, configuration) => ({ |
| 189 | + ...accLambdas, |
| 190 | + [configuration.qualifiedOperationId]: |
| 191 | + configuration.qualifiedOperationId, |
| 192 | + }), |
| 193 | + {}, |
| 194 | + ); |
| 195 | + |
| 196 | + log('info', `${JSON.stringify(functions, null, pretty ? 2 : 0)}`); |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + if (!functionName) { |
| 201 | + throw new YError('E_FUNCTION_NAME_REQUIRED'); |
| 202 | + } |
| 203 | + |
| 204 | + const functionConfiguration = configurations.find( |
| 205 | + ({ qualifiedOperationId }) => qualifiedOperationId === functionName, |
| 206 | + ); |
| 207 | + |
| 208 | + log( |
| 209 | + 'info', |
| 210 | + `${JSON.stringify(functionConfiguration, null, pretty ? 2 : 0)}`, |
| 211 | + ); |
| 212 | + }; |
| 213 | +} |
| 214 | + |
| 215 | +async function _execAsync(command: string): Promise<string> { |
| 216 | + return await new Promise((resolve, reject) => { |
| 217 | + exec( |
| 218 | + command, |
| 219 | + (err: ExecException | null, stdout: string, stderr: string) => { |
| 220 | + if (err) { |
| 221 | + reject(YError.wrap(err, 'E_EXEC_FAILURE', stderr)); |
| 222 | + return; |
| 223 | + } |
| 224 | + resolve(stdout.trim()); |
| 225 | + }, |
| 226 | + ); |
| 227 | + }); |
| 228 | +} |
0 commit comments