MCPcopy Create free account
hub / github.com/devcontainers/cli / findValue

Function findValue

src/spec-node/dockerfileUtils.ts:181–214  ·  view source on GitHub ↗
(dockerfile: Dockerfile, buildArgs: Record<string, string>, baseImageEnv: Record<string, string>, globalBuildxPlatformArgs: Record<string, string> = {}, variable: string, stage: { from?: From; instructions: Instruction[] }, beforeInstructionIndex: number)

Source from the content-addressed store, hash-verified

179}
180
181function findValue(dockerfile: Dockerfile, buildArgs: Record<string, string>, baseImageEnv: Record<string, string>, globalBuildxPlatformArgs: Record<string, string> = {}, variable: string, stage: { from?: From; instructions: Instruction[] }, beforeInstructionIndex: number): string | undefined {
182 let considerArg = true;
183 const seen = new Set<typeof stage>();
184 while (true) {
185 if (seen.has(stage)) {
186 return undefined;
187 }
188 seen.add(stage);
189
190 const i = findLastIndex(stage.instructions, i => i.name === variable && (i.instruction === 'ENV' || (considerArg && typeof (buildArgs[i.name] ?? i.value) === 'string')), beforeInstructionIndex - 1);
191 if (i !== -1) {
192 const instruction = stage.instructions[i];
193 if (instruction.instruction === 'ENV') {
194 return replaceVariables(dockerfile, buildArgs, baseImageEnv, globalBuildxPlatformArgs, instruction.value!, stage, i);
195 }
196 if (instruction.instruction === 'ARG') {
197 return replaceVariables(dockerfile, buildArgs, baseImageEnv, globalBuildxPlatformArgs, buildArgs[instruction.name] ?? instruction.value, stage, i);
198 }
199 }
200
201 if (!stage.from) {
202 const value = baseImageEnv[variable] ?? globalBuildxPlatformArgs[variable];
203 if (typeof value === 'string') {
204 return value;
205 }
206 return undefined;
207 }
208
209 const image = replaceVariables(dockerfile, buildArgs, baseImageEnv, globalBuildxPlatformArgs, stage.from.image, dockerfile.preamble, dockerfile.preamble.instructions.length);
210 stage = dockerfile.stagesByLabel[image] || dockerfile.preamble;
211 beforeInstructionIndex = stage.instructions.length;
212 considerArg = stage === dockerfile.preamble;
213 }
214}
215
216function findLastIndex<T>(array: T[], predicate: (value: T, index: number, obj: T[]) => boolean, position = array.length - 1): number {
217 for (let i = position; i >= 0; i--) {

Callers 1

replaceVariablesFunction · 0.85

Calls 2

findLastIndexFunction · 0.85
replaceVariablesFunction · 0.85

Tested by

no test coverage detected