Improve globbing to better handle both patterns and non patterns
This commit is contained in:
@@ -284,24 +284,20 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
|
|
||||||
expanded := []LinkInstruction{}
|
expanded := []LinkInstruction{}
|
||||||
for _, link := range config.Links {
|
for _, link := range config.Links {
|
||||||
if strings.Contains(link.Source, "*") {
|
LogSource("Expanding pattern source %s in YAML file %s", link.Source, filename)
|
||||||
LogSource("Expanding wildcard source %s in YAML file %s", link.Source, filename)
|
newlinks, err := ExpandPattern(link.Source, workdir, link.Target)
|
||||||
newlinks, err := ExpandPattern(link.Source, workdir, link.Target)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("error expanding pattern: %w", err)
|
||||||
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
|
||||||
}
|
|
||||||
// "Clone" the original link instruction for each expanded link
|
|
||||||
for i := range newlinks {
|
|
||||||
newlinks[i].Delete = link.Delete
|
|
||||||
newlinks[i].Hard = link.Hard
|
|
||||||
newlinks[i].Force = link.Force
|
|
||||||
}
|
|
||||||
LogInfo("Expanded wildcard source %s in YAML file %s to %d links",
|
|
||||||
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
|
|
||||||
expanded = append(expanded, newlinks...)
|
|
||||||
} else {
|
|
||||||
expanded = append(expanded, link)
|
|
||||||
}
|
}
|
||||||
|
// "Clone" the original link instruction for each expanded link
|
||||||
|
for i := range newlinks {
|
||||||
|
newlinks[i].Delete = link.Delete
|
||||||
|
newlinks[i].Hard = link.Hard
|
||||||
|
newlinks[i].Force = link.Force
|
||||||
|
}
|
||||||
|
LogInfo("Expanded pattern %s in YAML file %s to %d links",
|
||||||
|
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
|
||||||
|
expanded = append(expanded, newlinks...)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range expanded {
|
for i := range expanded {
|
||||||
@@ -325,34 +321,58 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
||||||
static, pattern := doublestar.SplitPattern(source)
|
static, pattern := doublestar.SplitPattern(source)
|
||||||
if static == "" {
|
if static == "" {
|
||||||
cwd, err := os.Getwd()
|
static = workdir
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error getting current working directory: %w", err)
|
|
||||||
}
|
|
||||||
static = cwd
|
|
||||||
}
|
}
|
||||||
LogInfo("Static part: %s", static)
|
LogInfo("Static part: %s", static)
|
||||||
LogInfo("Pattern part: %s", pattern)
|
LogInfo("Pattern part: %s", pattern)
|
||||||
|
|
||||||
files, err := doublestar.Glob(os.DirFS(static), pattern)
|
files, err := doublestar.Glob(os.DirFS(static), pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
return nil, fmt.Errorf("error expanding pattern: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
targetIsFile := false
|
||||||
|
if info, err := os.Stat(target); err == nil && !info.IsDir() {
|
||||||
|
targetIsFile = true
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
ext := filepath.Ext(file)
|
if info, err := os.Stat(file); err == nil && info.IsDir() {
|
||||||
if ext == "" {
|
// We don't care about matched directories
|
||||||
LogInfo("Skipping file %s because it has no extension (Directory?)", file)
|
// We want files within them
|
||||||
|
if len(files) == 1 {
|
||||||
|
// Special case: if there is only one file, and it's a directory
|
||||||
|
// This should only ever happen if our source is a path (and not a glob!)
|
||||||
|
// And our target is a path, a directory
|
||||||
|
// ...but it will also happen if the source IS a glob and it happens to match ONE directory
|
||||||
|
// I think that should happen rarely enough to not be an issue...
|
||||||
|
links = append(links, LinkInstruction{
|
||||||
|
Source: filepath.Join(static, file),
|
||||||
|
Target: target,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
LogInfo("Skipping directory %s", file)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
link := LinkInstruction{
|
|
||||||
Source: file,
|
var targetPath string
|
||||||
Target: filepath.Join(target, file),
|
if targetIsFile && len(files) == 1 {
|
||||||
|
// Special case: target is a file, and glob matches exactly one file.
|
||||||
|
// Use target directly (don't append filename).
|
||||||
|
targetPath = target
|
||||||
|
} else {
|
||||||
|
// Default: append filename to target dir.
|
||||||
|
targetPath = filepath.Join(target, file)
|
||||||
}
|
}
|
||||||
links = append(links, link)
|
|
||||||
|
links = append(links, LinkInstruction{
|
||||||
|
Source: filepath.Join(static, file),
|
||||||
|
Target: targetPath,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
LogInfo("Expanded wildcard source %s to %d links", FormatSourcePath(source), len(links))
|
LogInfo("Expanded pattern %s to %d links", FormatSourcePath(source), len(links))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user