Skip to content

Commit

Permalink
Merge pull request #387 from reubenmiller/feat-microservice-name-parsing
Browse files Browse the repository at this point in the history
feat(microservice): strip auto index suffixes from microservice filenames when parsing
  • Loading branch information
reubenmiller authored Jun 9, 2024
2 parents 61231c2 + d8d3d98 commit 0c8dd6e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ func ParseName(file string) string {
baseFileName := filepath.Base(file)
fileExt := filepath.Ext(baseFileName)
baseFileName = baseFileName[0 : len(baseFileName)-len(fileExt)]

// Strip suffixes which are added by OS's when downloading file which already exists in the
// target directory.
// e.g. "./cloud-http-proxy (1).zip"
suffixRegex := regexp.MustCompile(`\s+\(\d+\)$`)
baseFileName = suffixRegex.ReplaceAllString(baseFileName, "")

versionRegex := regexp.MustCompile(`([_-]v?\d+\.\d+\.\d+(-SNAPSHOT)?)?$`)
return versionRegex.ReplaceAllString(baseFileName, "")
}
32 changes: 32 additions & 0 deletions pkg/artifact/artifact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package artifact

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_Filename(t *testing.T) {
cases := []struct {
Filename string
Expected string
}{
{
Filename: "./cloud-http-proxy (1).zip",
Expected: "cloud-http-proxy",
},
{
Filename: "./helloworld3-0.0.1-SNAPSHOT.zip",
Expected: "helloworld3",
},
{
Filename: "./helloworld3-0.0.1-SNAPSHOT (100).zip",
Expected: "helloworld3",
},
}

for _, testcase := range cases {
actual := ParseName(testcase.Filename)
assert.Equal(t, testcase.Expected, actual)
}
}

0 comments on commit 0c8dd6e

Please sign in to comment.