Implement doublestar wildcards

This commit is contained in:
2025-04-13 19:28:57 +02:00
parent 3a5a333c62
commit 78536c3e19
6 changed files with 30 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar/v4"
"gopkg.in/yaml.v3"
)
@@ -285,7 +286,7 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
for _, link := range config.Links {
if strings.Contains(link.Source, "*") {
LogSource("Expanding wildcard source %s in YAML file %s", link.Source, filename)
newlinks, err := ExpandWildcard(link.Source, workdir, link.Target)
newlinks, err := ExpandPattern(link.Source, workdir, link.Target)
if err != nil {
return nil, fmt.Errorf("error expanding wildcard: %w", err)
}
@@ -321,19 +322,32 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
return expanded, nil
}
func ExpandWildcard(source, workdir, target string) (links []LinkInstruction, err error) {
dir := filepath.Dir(source)
pattern := filepath.Base(source)
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
static, pattern := doublestar.SplitPattern(source)
if static == "" {
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("error getting current working directory: %w", err)
}
static = cwd
}
LogInfo("Static part: %s", static)
LogInfo("Pattern part: %s", pattern)
files, err := filepath.Glob(filepath.Join(workdir, dir, pattern))
files, err := doublestar.Glob(os.DirFS(static), pattern)
if err != nil {
return nil, fmt.Errorf("error expanding wildcard: %w", err)
}
for _, file := range files {
ext := filepath.Ext(file)
if ext == "" {
LogInfo("Skipping file %s because it has no extension (Directory?)", file)
continue
}
link := LinkInstruction{
Source: file,
Target: filepath.Join(target, filepath.Base(file)),
Target: filepath.Join(target, file),
}
links = append(links, link)
}