MCPcopy Index your code
hub / github.com/nikivdev/go / readInstructions

Function readInstructions

try/dockerlayers/main.go:350–399  ·  view source on GitHub ↗
(path string)

Source from the content-addressed store, hash-verified

348}
349
350func readInstructions(path string) ([]rawInstruction, error) {
351 file, err := os.Open(path)
352 if err != nil {
353 return nil, err
354 }
355 defer file.Close()
356
357 scanner := bufio.NewScanner(file)
358 var instructions []rawInstruction
359 var current strings.Builder
360 var currentLine int
361
362 for line := 1; scanner.Scan(); line++ {
363 text := scanner.Text()
364 trimmed := strings.TrimSpace(text)
365 if trimmed == "" || strings.HasPrefix(trimmed, "#") {
366 continue
367 }
368
369 lineWithoutInlineComment := removeInlineComment(trimmed)
370 if lineWithoutInlineComment == "" {
371 continue
372 }
373
374 if current.Len() == 0 {
375 currentLine = line
376 } else {
377 current.WriteString(" ")
378 }
379 linePart, carries := stripContinuation(lineWithoutInlineComment)
380 current.WriteString(linePart)
381
382 if !carries {
383 instructions = append(instructions, rawInstruction{
384 line: currentLine,
385 text: current.String(),
386 })
387 current.Reset()
388 }
389 }
390
391 if err := scanner.Err(); err != nil {
392 return nil, err
393 }
394 if current.Len() != 0 {
395 return nil, errors.New("unterminated line continuation at end of file")
396 }
397
398 return instructions, nil
399}
400
401func stripContinuation(line string) (string, bool) {
402 if strings.HasSuffix(line, "\\") {

Callers 1

analyzeDockerfileFunction · 0.85

Calls 3

removeInlineCommentFunction · 0.85
stripContinuationFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected