fix: isHashID matches for hyphenated application names
Fixes `isHashID` checks for hyphenated application names
When a user calls bd init inside an application that has a "-" for
example my-first-application without any options than ids will follow a
pattern of my-first-application-{ID}. When using SplitN on the "-"
separator it would result in the split parts returning as [my,
first-application-{ID}] which is incorrectly pulling out the {ID}
resulting in the `isHashID` returning false when it could be a hash id.
Instead of using SplitN, this changes to find the last index of the
separator resulting in the suffix becoming the actual {ID}.
This commit is contained in:
committed by
Steve Yegge
parent
7b865eb541
commit
408244ed73
@@ -328,12 +328,12 @@ func replaceIDReferences(text string, mapping map[string]string) string {
|
|||||||
func isHashID(id string) bool {
|
func isHashID(id string) bool {
|
||||||
// Hash IDs contain hex letters (a-f), sequential IDs are only digits
|
// Hash IDs contain hex letters (a-f), sequential IDs are only digits
|
||||||
// May have hierarchical suffix like .1 or .1.2
|
// May have hierarchical suffix like .1 or .1.2
|
||||||
parts := strings.SplitN(id, "-", 2)
|
lastSeperatorIndex := strings.LastIndex(id, "-")
|
||||||
if len(parts) != 2 {
|
if lastSeperatorIndex == -1 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
suffix := parts[1]
|
suffix := id[lastSeperatorIndex+1:]
|
||||||
// Strip hierarchical suffix like .1 or .1.2
|
// Strip hierarchical suffix like .1 or .1.2
|
||||||
baseSuffix := strings.Split(suffix, ".")[0]
|
baseSuffix := strings.Split(suffix, ".")[0]
|
||||||
|
|
||||||
|
|||||||
@@ -262,7 +262,6 @@ func TestIsHashID(t *testing.T) {
|
|||||||
{"bd-123abc", true},
|
{"bd-123abc", true},
|
||||||
{"bd-a3f8e9a2.1", true},
|
{"bd-a3f8e9a2.1", true},
|
||||||
{"bd-a3f8e9a2.1.2", true},
|
{"bd-a3f8e9a2.1.2", true},
|
||||||
|
|
||||||
// Hash IDs that are numeric but 5+ characters (likely hash)
|
// Hash IDs that are numeric but 5+ characters (likely hash)
|
||||||
{"bd-12345", true},
|
{"bd-12345", true},
|
||||||
{"bd-0088", false}, // 4 chars, all numeric - ambiguous, defaults to false
|
{"bd-0088", false}, // 4 chars, all numeric - ambiguous, defaults to false
|
||||||
@@ -278,6 +277,15 @@ func TestIsHashID(t *testing.T) {
|
|||||||
{"bd-", false}, // Empty suffix
|
{"bd-", false}, // Empty suffix
|
||||||
{"invalid", false}, // No dash
|
{"invalid", false}, // No dash
|
||||||
{"bd-0", false}, // Single digit
|
{"bd-0", false}, // Single digit
|
||||||
|
|
||||||
|
// Hyphenated prefixes
|
||||||
|
{"bd-beads-1", false},
|
||||||
|
{"bd-beads-123", false},
|
||||||
|
{"bd-beads-a3f8e9a2", true},
|
||||||
|
{"bd-beads-abc123", true},
|
||||||
|
{"bd-beads-123abc", true},
|
||||||
|
{"bd-beads-a3f8e9a2.1", true},
|
||||||
|
{"bd-beads-a3f8e9a2.1.2", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user