Skip to content

Commit

Permalink
Fix bug in the detection of module zip files
Browse files Browse the repository at this point in the history
Terraform requires a module URL to include an organisation, a module name and a provider. The provider is not optional.
Fix the the glob pattern so it actually works...
  • Loading branch information
adcharre committed Sep 6, 2022
1 parent 8a150a0 commit cecb133
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions internal/database/filesystem/db_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/terrariumcloud/terrarium-lite/pkg/registry/data/modules"
"github.com/terrariumcloud/terrarium-lite/pkg/registry/stores"
"log"
"os"
"path/filepath"
"strings"
)
Expand All @@ -25,45 +26,29 @@ func (m *adapter) Modules() stores.ModuleStore {
func loadFromPath(modulesPath string) ([]*modules.Module, error) {
allModules := make([]*modules.Module, 0)

matches, _ := filepath.Glob(fmt.Sprintf("%s/**.zip", modulesPath))
matches, _ := filepath.Glob(fmt.Sprintf("%s/*/*/*/*.zip", modulesPath))

for _, name := range matches {
sourcePath, err := filepath.Rel(modulesPath, name)
if err != nil {
return nil, err
}

elements := filepath.SplitList(sourcePath)
switch len(elements) {
case 4: // organization / module name / provider / version.zip
elements := strings.Split(sourcePath, string(os.PathSeparator))
if len(elements) == 4 {
module := modules.Module{
ID: strings.Join(elements, "/"),
OrganizationID: elements[0],
Name: elements[1],
Organization: elements[0],
Provider: elements[2],
Version: elements[3][:len(elements[3])-4],
Version: elements[3][:len(elements[3])-4], // remove .zip from the version name.
Description: "",
Source: sourcePath,
}
allModules = append(allModules, &module)
log.Printf("INFO: Added module %s", name)
break
case 3: // organization / module name / version.zip
module := modules.Module{
ID: strings.Join(elements, "/"),
OrganizationID: elements[0],
Name: elements[1],
Organization: elements[0],
Provider: "",
Version: elements[2][:len(elements[2])-4],
Description: "",
Source: name,
}
allModules = append(allModules, &module)
log.Printf("INFO: Added module %s", name)
break
default:
} else {
log.Printf("WARN: Ignoring invalid module path: %s", name)
}
}
Expand Down

0 comments on commit cecb133

Please sign in to comment.