ensureInMemoryComposeFlags rewrites the docker compose command to use stdin ("-f -") for in-memory content and injects the exit-code-from flags. It tokenizes the arguments, strips all -f/--file flags (including dangling ones without a value), and injects a single "-f -" before the first compose subc
(appCmd, serviceName string)
| 27 | // ones without a value), and injects a single "-f -" before the first compose |
| 28 | // subcommand to avoid producing multiple stdin readers. |
| 29 | func ensureInMemoryComposeFlags(appCmd, serviceName string) string { |
| 30 | parts := strings.Fields(appCmd) |
| 31 | |
| 32 | // Strip every existing -f/--file flag and its value, including dangling |
| 33 | // -f/--file tokens that appear without a following value. |
| 34 | cleaned := make([]string, 0, len(parts)) |
| 35 | for i := 0; i < len(parts); i++ { |
| 36 | if parts[i] == "-f" || parts[i] == "--file" { |
| 37 | // Skip the value if one follows. |
| 38 | if i+1 < len(parts) { |
| 39 | i++ |
| 40 | } |
| 41 | continue |
| 42 | } |
| 43 | if strings.HasPrefix(parts[i], "-f=") || strings.HasPrefix(parts[i], "--file=") { |
| 44 | continue |
| 45 | } |
| 46 | cleaned = append(cleaned, parts[i]) |
| 47 | } |
| 48 | |
| 49 | // Inject a single "-f -" before the first compose subcommand so the flag |
| 50 | // stays in the global-options position. If no subcommand is found, append |
| 51 | // at the end as a fallback. |
| 52 | injected := false |
| 53 | result := make([]string, 0, len(cleaned)+2) |
| 54 | for _, p := range cleaned { |
| 55 | if !injected && composeSubcommands[p] { |
| 56 | result = append(result, "-f", "-") |
| 57 | injected = true |
| 58 | } |
| 59 | result = append(result, p) |
| 60 | } |
| 61 | if !injected { |
| 62 | result = append(result, "-f", "-") |
| 63 | } |
| 64 | |
| 65 | return ensureComposeExitOnAppFailure(strings.Join(result, " "), serviceName) |
| 66 | } |
| 67 | |
| 68 | func findComposeFile(cmd string) []string { |
| 69 |
no test coverage detected