MCPcopy
hub / github.com/apache/answer / resolveAnswerModuleReplacement

Function resolveAnswerModuleReplacement

internal/cli/build.go:257–301  ·  view source on GitHub ↗

resolveAnswerModuleReplacement resolves the ANSWER_MODULE value to a usable local path or remote replacement string. For v2+ versioned module paths (e.g. github.com/apache/answer@v2.0.0), Go module system rejects the version because the module path has no /v2 suffix. In that case the repository is c

(replacement, tmpDir string)

Source from the content-addressed store, hash-verified

255// Go module system rejects the version because the module path has no /v2 suffix. In that case
256// the repository is cloned locally and the local path is returned instead.
257func resolveAnswerModuleReplacement(replacement, tmpDir string) (string, error) {
258 // Local paths can be used as-is.
259 if strings.HasPrefix(replacement, "/") || strings.HasPrefix(replacement, "./") || strings.HasPrefix(replacement, "../") {
260 return replacement, nil
261 }
262
263 // Parse module@version format.
264 moduleName, version, hasVersion := strings.Cut(replacement, "@")
265 if !hasVersion {
266 return replacement, nil
267 }
268
269 // Only handle v2+ versions on module paths without the /vN suffix.
270 ver, err := semver.StrictNewVersion(strings.TrimPrefix(version, "v"))
271 if err != nil || ver.Major() < 2 {
272 return replacement, nil
273 }
274 if strings.HasSuffix(moduleName, fmt.Sprintf("/v%d", ver.Major())) {
275 return replacement, nil
276 }
277
278 // Clone the repo to a local directory and return its path.
279 gitURL := "https://" + moduleName
280 tag := "v" + strings.TrimPrefix(version, "v")
281 localPath := filepath.Join(filepath.Dir(tmpDir), fmt.Sprintf("answer_src_%s", strings.ReplaceAll(version, ".", "_")))
282
283 if _, statErr := os.Stat(localPath); statErr == nil {
284 fmt.Printf("[build] using cached local clone at %s\n", localPath)
285 return localPath, nil
286 }
287
288 fmt.Printf("[build] v2+ module detected, cloning %s@%s to local path %s...\n", moduleName, version, localPath)
289 cloneCmd := exec.Command("git", "clone", "--depth=1", "--branch="+tag, gitURL, localPath)
290 cloneCmd.Stdout = os.Stdout
291 cloneCmd.Stderr = os.Stderr
292 if err = cloneCmd.Run(); err != nil {
293 return "", fmt.Errorf(
294 "failed to clone %s@%s: %w\nTip: set ANSWER_MODULE to a local checkout path instead, e.g. ANSWER_MODULE=/path/to/answer",
295 moduleName, version, err,
296 )
297 }
298
299 fmt.Printf("[build] successfully cloned to %s\n", localPath)
300 return localPath, nil
301}
302
303// movePluginToVendor move plugin to vendor dir
304// Traverse the plugins, and if the plugin path is not github.com/apache/answer-plugins, move the contents of the current plugin to the vendor/github.com/apache/answer-plugins/ directory.

Callers 1

downloadGoModFileFunction · 0.85

Calls 1

RunMethod · 0.80

Tested by

no test coverage detected