diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..c025f0b --- /dev/null +++ b/config/config.go @@ -0,0 +1,41 @@ +package config + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +// collectConfigFiles scans searchPaths for files ending in suffix and returns +// a name -> filepath map. Earlier paths take priority (first occurrence wins). +// The name is derived by trimming the suffix from the filename. +func collectConfigFiles(searchPaths []string, suffix string) (map[string]string, error) { + files := make(map[string]string) + + for _, dir := range searchPaths { + entries, err := os.ReadDir(dir) + if err != nil { + if os.IsNotExist(err) { + continue + } + return nil, fmt.Errorf("failed to read directory %s: %w", dir, err) + } + + for _, entry := range entries { + if entry.IsDir() { + continue + } + if !strings.HasSuffix(entry.Name(), suffix) { + continue + } + + name := strings.TrimSuffix(entry.Name(), suffix) + if _, exists := files[name]; !exists { + files[name] = filepath.Join(dir, entry.Name()) + } + } + } + + return files, nil +} diff --git a/config/feature.go b/config/feature.go index defc640..736588e 100644 --- a/config/feature.go +++ b/config/feature.go @@ -12,6 +12,8 @@ import ( "gopkg.in/ini.v1" ) +const featureSuffix = ".feature" + // Feature represents a parsed .feature configuration file type Feature struct { Name string // Derived from filename (e.g., "devel" from "devel.feature") @@ -35,31 +37,9 @@ func LoadFeatures(customPath string) ([]*Feature, error) { } // Collect all .feature files, with earlier paths taking priority - featureFiles := make(map[string]string) // feature name -> file path - - for _, dir := range searchPaths { - entries, err := os.ReadDir(dir) - if err != nil { - if os.IsNotExist(err) { - continue - } - return nil, fmt.Errorf("failed to read directory %s: %w", dir, err) - } - - for _, entry := range entries { - if entry.IsDir() { - continue - } - if !strings.HasSuffix(entry.Name(), ".feature") { - continue - } - - featureName := strings.TrimSuffix(entry.Name(), ".feature") - if _, exists := featureFiles[featureName]; !exists { - // Earlier paths take priority - featureFiles[featureName] = filepath.Join(dir, entry.Name()) - } - } + featureFiles, err := collectConfigFiles(searchPaths, featureSuffix) + if err != nil { + return nil, err } if len(featureFiles) == 0 { diff --git a/config/transfer.go b/config/transfer.go index a7af533..7947b22 100644 --- a/config/transfer.go +++ b/config/transfer.go @@ -4,7 +4,6 @@ import ( "cmp" "fmt" "os" - "path/filepath" "runtime" "slices" "strings" @@ -12,6 +11,8 @@ import ( "gopkg.in/ini.v1" ) +const transferSuffix = ".transfer" + // Transfer represents a parsed .transfer configuration file type Transfer struct { Component string // Derived from filename @@ -93,31 +94,9 @@ func LoadTransfers(customPath string) ([]*Transfer, error) { } // Collect all .transfer files, with earlier paths taking priority - transferFiles := make(map[string]string) // component name -> file path - - for _, dir := range searchPaths { - entries, err := os.ReadDir(dir) - if err != nil { - if os.IsNotExist(err) { - continue - } - return nil, fmt.Errorf("failed to read directory %s: %w", dir, err) - } - - for _, entry := range entries { - if entry.IsDir() { - continue - } - if !strings.HasSuffix(entry.Name(), ".transfer") { - continue - } - - component := strings.TrimSuffix(entry.Name(), ".transfer") - if _, exists := transferFiles[component]; !exists { - // Earlier paths take priority - transferFiles[component] = filepath.Join(dir, entry.Name()) - } - } + transferFiles, err := collectConfigFiles(searchPaths, transferSuffix) + if err != nil { + return nil, err } if len(transferFiles) == 0 {