MCPcopy
hub / github.com/keploy/keploy / modifyDockerComposeCommand

Function modifyDockerComposeCommand

pkg/client/app/util.go:96–145  ·  view source on GitHub ↗
(appCmd, newComposeFile, appComposePath, appServiceName string)

Source from the content-addressed store, hash-verified

94}
95
96func modifyDockerComposeCommand(appCmd, newComposeFile, appComposePath, appServiceName string) string {
97 // Ensure newComposeFile starts with ./
98 if !strings.HasPrefix(newComposeFile, "./") {
99 newComposeFile = "./" + newComposeFile
100 }
101
102 var modifiedCmd string
103 // Define a regular expression pattern to match "-f <file>"
104 pattern := `(-f\s+("[^"]+"|'[^']+'|\S+))`
105 re := regexp.MustCompile(pattern)
106
107 // Find all matches and replace only the one that matches appComposePath
108 matches := re.FindAllStringSubmatch(appCmd, -1)
109 if len(matches) > 0 {
110 for _, match := range matches {
111 fullMatch := match[0]
112 filePart := match[1]
113
114 // Extract the actual file path from the match (remove -f and whitespace)
115 filePattern := `-f\s+("[^"]+"|'[^']+'|\S+)`
116 fileRe := regexp.MustCompile(filePattern)
117 fileMatch := fileRe.FindStringSubmatch(filePart)
118
119 if len(fileMatch) > 1 {
120 quotedFile := fileMatch[1]
121 // Remove quotes if present
122 actualFile := strings.Trim(quotedFile, `"'`)
123
124 // Check if this file matches the appComposePath
125 if actualFile == appComposePath {
126 modifiedCmd = strings.Replace(appCmd, fullMatch, fmt.Sprintf("-f %s", newComposeFile), 1)
127 return ensureComposeExitOnAppFailure(modifiedCmd, appServiceName)
128 }
129 }
130 }
131 // If no matching compose path found, return original command
132 modifiedCmd = appCmd
133 return ensureComposeExitOnAppFailure(modifiedCmd, appServiceName)
134 }
135
136 // If the pattern doesn't exist, inject the new Compose file right after "docker-compose" or "docker compose"
137 upIdx := strings.Index(appCmd, " up")
138 if upIdx != -1 {
139 modifiedCmd = fmt.Sprintf("%s -f %s%s", appCmd[:upIdx], newComposeFile, appCmd[upIdx:])
140 return ensureComposeExitOnAppFailure(modifiedCmd, appServiceName)
141 }
142
143 modifiedCmd = fmt.Sprintf("%s -f %s", appCmd, newComposeFile)
144 return ensureComposeExitOnAppFailure(modifiedCmd, appServiceName)
145}
146
147func isDetachMode(logger *zap.Logger, command string, kind utils.CmdType) bool {
148 args := strings.Fields(command)

Callers 1

SetupComposeMethod · 0.85

Calls 1

Tested by

no test coverage detected