Allow glob of --host and --yaml; cut release 1.2.1

This commit is contained in:
Pim van Pelt
2025-07-06 23:31:41 +02:00
parent fd74c41fb3
commit 9475d7b5c0
5 changed files with 52 additions and 21 deletions

View File

@ -6,11 +6,12 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
const Version = "1.2.0"
const Version = "1.2.1"
// Config and SSH types are now in separate packages
@ -36,8 +37,21 @@ func main() {
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("IPng Networks Router Backup v%s\n", Version)
// Expand glob patterns in YAML files
var expandedYamlFiles []string
for _, pattern := range yamlFiles {
matches, err := filepath.Glob(pattern)
if err != nil {
log.Fatalf("Invalid glob pattern '%s': %v", pattern, err)
}
if len(matches) == 0 {
log.Fatalf("No files matched pattern '%s'", pattern)
}
expandedYamlFiles = append(expandedYamlFiles, matches...)
}
// Load configuration
cfg, err := ConfigRead(yamlFiles)
cfg, err := ConfigRead(expandedYamlFiles)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
@ -63,11 +77,16 @@ func main() {
devicesToProcess := cfg.Devices
if len(hostFilter) > 0 {
devicesToProcess = make(map[string]Device)
for _, hostname := range hostFilter {
if deviceConfig, exists := cfg.Devices[hostname]; exists {
devicesToProcess[hostname] = deviceConfig
} else {
fmt.Printf("Warning: Host '%s' not found in config file\n", hostname)
for _, pattern := range hostFilter {
patternMatched := false
for hostname, deviceConfig := range cfg.Devices {
if matched, _ := filepath.Match(pattern, hostname); matched {
devicesToProcess[hostname] = deviceConfig
patternMatched = true
}
}
if !patternMatched {
fmt.Printf("Warning: Host pattern '%s' did not match any devices\n", pattern)
}
}
}