diff --git a/builder.go b/builder.go index 5e6f23c..2032c5b 100644 --- a/builder.go +++ b/builder.go @@ -179,6 +179,13 @@ type Dependency struct { Version string `json:"version,omitempty"` } +func (d Dependency) String() string { + if d.Version != "" { + return d.PackagePath + "@" + d.Version + } + return d.PackagePath +} + // ReplacementPath represents an old or new path component // within a Go module replacement directive. type ReplacementPath string diff --git a/cmd/main.go b/cmd/main.go index 16e3d81..e6024c3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -97,9 +97,30 @@ func runBuild(ctx context.Context, args []string) error { } log.Printf("[INFO] Resolved relative replacement %s to %s", args[i], repl) } - replacements = append(replacements, xcaddy.NewReplace(mod, repl)) + replacements = append(replacements, xcaddy.NewReplace(xcaddy.Dependency{PackagePath: mod, Version: ver}.String(), repl)) } - + case "--replace": + if i == len(args)-1 { + return fmt.Errorf("expected value after --replace flag") + } + i++ + mod, ver, repl, err := splitWith(args[i]) + if err != nil { + return err + } + mod = strings.TrimSuffix(mod, "/") // easy to accidentally leave a trailing slash if pasting from a URL, but is invalid for Go modules + if repl == "" { + return fmt.Errorf("expected value after --replace flag") + } + // 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", args[i], repl) + } + replacements = append(replacements, xcaddy.NewReplace(xcaddy.Dependency{PackagePath: mod, Version: ver}.String(), repl)) case "--output": if i == len(args)-1 { return fmt.Errorf("expected value after --output flag")