Support globs for multiple mapping files
Change-Id: I048c9bff0e384763dfc7825c35dd6055c89b7c82
diff --git a/cmd/termmapper/main.go b/cmd/termmapper/main.go
index aa940ac..e945572 100644
--- a/cmd/termmapper/main.go
+++ b/cmd/termmapper/main.go
@@ -4,6 +4,7 @@
"fmt"
"os"
"os/signal"
+ "path/filepath"
"strings"
"syscall"
@@ -23,7 +24,7 @@
type appConfig struct {
Port *int `kong:"short='p',help='Port to listen on'"`
Config string `kong:"short='c',help='YAML configuration file containing mapping directives and global settings'"`
- Mappings []string `kong:"short='m',help='Individual YAML mapping files to load'"`
+ Mappings []string `kong:"short='m',help='Individual YAML mapping files to load (supports glob patterns like dir/*.yaml)'"`
LogLevel *string `kong:"short='l',help='Log level (debug, info, warn, error)'"`
}
@@ -79,8 +80,14 @@
log.Fatal().Msg("At least one configuration source must be provided: use -c for main config file or -m for mapping files")
}
+ // Expand glob patterns in mapping files
+ expandedMappings, err := expandGlobs(cfg.Mappings)
+ if err != nil {
+ log.Fatal().Err(err).Msg("Failed to expand glob patterns in mapping files")
+ }
+
// Load configuration from multiple sources
- yamlConfig, err := config.LoadFromSources(cfg.Config, cfg.Mappings)
+ yamlConfig, err := config.LoadFromSources(cfg.Config, expandedMappings)
if err != nil {
log.Fatal().Err(err).Msg("Failed to load configuration")
}
@@ -357,3 +364,27 @@
return html
}
+
+// expandGlobs expands glob patterns in the slice of file paths
+// Returns the expanded list of files or an error if glob expansion fails
+func expandGlobs(patterns []string) ([]string, error) {
+ var expanded []string
+
+ for _, pattern := range patterns {
+ // Use filepath.Glob which works cross-platform
+ matches, err := filepath.Glob(pattern)
+ if err != nil {
+ return nil, fmt.Errorf("failed to expand glob pattern '%s': %w", pattern, err)
+ }
+
+ // If no matches found, treat as literal filename (consistent with shell behavior)
+ if len(matches) == 0 {
+ log.Warn().Str("pattern", pattern).Msg("Glob pattern matched no files, treating as literal filename")
+ expanded = append(expanded, pattern)
+ } else {
+ expanded = append(expanded, matches...)
+ }
+ }
+
+ return expanded, nil
+}