Skip to content

Commit

Permalink
Tolerate module file paths with @ in them
Browse files Browse the repository at this point in the history
But version must also be specified to disambiguate.

Fix #109
  • Loading branch information
mholt committed Jul 29, 2022
1 parent 47f9ded commit 7577d60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
28 changes: 13 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,25 +343,23 @@ func trapSignals(ctx context.Context, cancel context.CancelFunc) {
func splitWith(arg string) (module, version, replace string, err error) {
const versionSplit, replaceSplit = "@", "="

modules := strings.SplitN(arg, replaceSplit, 2)
if len(modules) > 1 {
replace = modules[1]
parts := strings.SplitN(arg, replaceSplit, 2)
if len(parts) > 1 {
replace = parts[1]
}

parts := strings.SplitN(modules[0], versionSplit, 2)
module = parts[0]
if len(parts) == 1 {
parts := strings.SplitN(module, replaceSplit, 2)
if len(parts) > 1 {
module = parts[0]
replace = parts[1]

// accommodate module paths that have @ in them, but we can only tolerate that if there's also
// a version, otherwise we don't know if it's a version separator or part of the file path (see #109)
lastVersionSplit := strings.LastIndex(module, versionSplit)
if lastVersionSplit < 0 {
if replaceIdx := strings.Index(module, replaceSplit); replaceIdx >= 0 {
module, replace = module[:replaceIdx], module[replaceIdx+1:]
}
} else {
version = parts[1]
parts := strings.SplitN(version, replaceSplit, 2)
if len(parts) > 1 {
version = parts[0]
replace = parts[1]
module, version = module[:lastVersionSplit], module[lastVersionSplit+1:]
if replaceIdx := strings.Index(version, replaceSplit); replaceIdx >= 0 {
version, replace = module[:replaceIdx], module[replaceIdx+1:]
}
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func TestSplitWith(t *testing.T) {
input: "",
expectErr: true,
},
{
// issue #109
input: "/home/devin/projects/@relay/caddy-bin@version",
expectModule: "/home/devin/projects/@relay/caddy-bin",
expectVersion: "version",
},
} {
actualModule, actualVersion, actualReplace, actualErr := splitWith(tc.input)
if actualModule != tc.expectModule {
Expand Down

0 comments on commit 7577d60

Please sign in to comment.