Skip to content

Commit 394a076

Browse files
authored
hack/: remove existing replace lines in go.mod for the SDK repo (#1664)
1 parent f7f02de commit 394a076

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

hack/lib/test_lib.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ function add_go_mod_replace() {
5858
exit 1
5959
fi
6060

61+
# Check if a replace line already exists. If it does, remove. If not, append.
62+
if grep -q "${from_path} =>" go.mod; then
63+
sed -E -i 's|^.+'"${from_path} =>"'.+$||g' go.mod
64+
fi
6165
# Do not use "go mod edit" so formatting stays the same.
6266
local replace="replace ${from_path} => ${to_path}"
6367
if [[ -n "$version" ]]; then

hack/tests/scaffolding/scaffold-memcached.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/exec"
2424
"path/filepath"
25+
"regexp"
2526
"strings"
2627

2728
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
@@ -256,6 +257,10 @@ func insertGoModReplace(repo, path, sha string) ([]byte, error) {
256257
if err != nil {
257258
return nil, errors.Wrap(err, "failed to read go.mod")
258259
}
260+
// Remove all replace lines in go.mod.
261+
replaceRe := regexp.MustCompile(fmt.Sprintf("(replace )?%s =>.+", repo))
262+
modBytes = replaceRe.ReplaceAll(modBytes, nil)
263+
// Append the desired replace to the end of go.mod's bytes.
259264
sdkReplace := fmt.Sprintf("replace %s => %s", repo, path)
260265
if sha != "" {
261266
sdkReplace = fmt.Sprintf("%s %s", sdkReplace, sha)

0 commit comments

Comments
 (0)