diff --git a/internal/plugin/index_builder.go b/internal/plugin/index_builder.go index 556db9139..ce7eb2fb2 100644 --- a/internal/plugin/index_builder.go +++ b/internal/plugin/index_builder.go @@ -210,9 +210,7 @@ func (i *IndexBuilder) appendIndexEntry(entries map[string][]pluginBinariesIndex } pType, pName, os, arch := parts[0], parts[1], parts[2], parts[3] - for ext := range getter.Decompressors { - arch = strings.TrimSuffix(arch, "."+ext) // normalize in case archive was used - } + arch = trimArchiveExtension(arch) // normalize in case archive was used if !pNameRegex.MatchString(pName) { i.log.WithField("file", entryName).Debug("Ignoring file as it doesn't match filter") @@ -288,10 +286,34 @@ func (i *IndexBuilder) getJSONSchemaLoaderForEntry(entry IndexEntry) gojsonschem } func hasArchiveExtension(path string) bool { - for ext := range getter.Decompressors { + for _, ext := range getAvailableDecompressors() { if strings.HasSuffix(path, "."+ext) { return true } } return false } + +func trimArchiveExtension(in string) string { + for _, ext := range getAvailableDecompressors() { + in = strings.TrimSuffix(in, "."+ext) + } + return in +} + +func getAvailableDecompressors() []string { + var ( + multiExt []string + singleExt []string + ) + + for ext := range getter.Decompressors { + if strings.Contains(ext, ".") { + multiExt = append(multiExt, ext) + continue + } + singleExt = append(singleExt, ext) + } + + return append(multiExt, singleExt...) +} diff --git a/internal/plugin/index_builder_test.go b/internal/plugin/index_builder_test.go index 8bf2fd6e7..61a9f0048 100644 --- a/internal/plugin/index_builder_test.go +++ b/internal/plugin/index_builder_test.go @@ -106,3 +106,36 @@ func TestIndexBuilder_ValidateJSONSchemas(t *testing.T) { // then assert.EqualError(t, err, expectedErrMsg) } + +func TestRemoveArchiveExtension(t *testing.T) { + tests := []struct { + name string + givenName string + expName string + }{ + { + name: "Trim single extension archive", + givenName: "test.gz", + expName: "test", + }, + { + name: "Trim multi extension archive", + givenName: "test.tar.gz", + expName: "test", + }, + { + name: "Does nothing if not archive", + givenName: "test.exe", + expName: "test.exe", + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // when + gotName := trimArchiveExtension(tc.givenName) + + // then + assert.Equal(t, tc.expName, gotName) + }) + } +}