From b72e330d5d7439ce848e29ade848760012e9e386 Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Wed, 11 Sep 2024 10:28:31 +0000 Subject: [PATCH] cmd: handle replacements in `--with` Fixes #201 --- cmd/commands.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/commands.go b/cmd/commands.go index be75ccd..daec448 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -74,7 +74,7 @@ Flags: return fmt.Errorf("unable to parse --replace arguments: %s", err.Error()) } for _, withArg := range withArgs { - mod, ver, _, err := splitWith(withArg) + mod, ver, repl, err := splitWith(withArg) if err != nil { return err } @@ -83,6 +83,7 @@ Flags: PackagePath: mod, Version: ver, }) + handleReplace(withArg, mod, ver, repl, &replacements) } for _, withArg := range replaceArgs { @@ -90,15 +91,7 @@ Flags: if err != nil { return err } - // adjust relative replacements in current working directory since our temporary module is in a different directory - if strings.HasPrefix(repl, ".") { - repl, err = filepath.Abs(repl) - if err != nil { - log.Fatalf("[FATAL] %v", err) - } - log.Printf("[INFO] Resolved relative replacement %s to %s", withArg, repl) - } - replacements = append(replacements, xcaddy.NewReplace(xcaddy.Dependency{PackagePath: mod, Version: ver}.String(), repl)) + handleReplace(withArg, mod, ver, repl, &replacements) } output, err = cmd.Flags().GetString("output") @@ -187,3 +180,18 @@ Flags: return nil }, } + +func handleReplace(orig, mod, ver, repl string, replacements *[]xcaddy.Replace) { + if repl != "" { + // adjust relative replacements in current working directory since our temporary module is in a different directory + if strings.HasPrefix(repl, ".") { + var err error + repl, err = filepath.Abs(repl) + if err != nil { + log.Fatalf("[FATAL] %v", err) + } + log.Printf("[INFO] Resolved relative replacement %s to %s", orig, repl) + } + *replacements = append(*replacements, xcaddy.NewReplace(xcaddy.Dependency{PackagePath: mod, Version: ver}.String(), repl)) + } +}