diff --git a/clean.go b/clean.go index 8f3ac97..235d7d3 100644 --- a/clean.go +++ b/clean.go @@ -50,7 +50,7 @@ func (me *Clean) RunConfig(cfg *AppConfig) error { return nil } func (me *Clean) CleanUnreferenced(cfg *AppConfig) error { - run, err := CompileRun(cfg.Symlinks, cfg.Script) + run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script) if err != nil { return err } diff --git a/config.go b/config.go index 2071345..3c5f8d5 100644 --- a/config.go +++ b/config.go @@ -15,6 +15,7 @@ type AppConfig struct { Symlinks map[string]string `yaml:"symlinks"` Script []*Script `yaml:"script"` Include []string `yaml:"include"` + WalkDir map[string]string `yaml:"walkdir"` } func (c *AppConfig) LoadYaml(path string) error { diff --git a/link.go b/link.go index 0b931e9..5e67431 100644 --- a/link.go +++ b/link.go @@ -99,7 +99,7 @@ func (me *Link) RunFile(opts *LinkOptions, path string) error { } func (me *Link) RunConfig(path string, opts *LinkOptions, cfg *AppConfig) error { - run, err := CompileRun(cfg.Symlinks, cfg.Script) + run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script) if err != nil { return err } @@ -113,6 +113,7 @@ func (me *Link) RunConfig(path string, opts *LinkOptions, cfg *AppConfig) error if err != nil { return err } + err = CleanLinks(opts, cfg.Clean, run.HomeDir) if err != nil { return err @@ -284,7 +285,7 @@ func (me *Link) RunAutoMode(opts *LinkOptions) error { cfg.Symlinks["~/"+filename] = filename } - run, err := CompileRun(cfg.Symlinks, cfg.Script) + run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script) if err != nil { return err } diff --git a/run.go b/run.go index 378ced6..876b949 100644 --- a/run.go +++ b/run.go @@ -33,25 +33,42 @@ type LinkInfo struct { NeedsCreate bool } -func CompileRun(symlinks map[string]string, script []*Script) (*Run, error) { - ret := NewRun() - ret.Script = make([]*Script, 0) +func CompileRun(symlinks, walkdir map[string]string, script []*Script) (*Run, error) { + run := NewRun() + run.Script = make([]*Script, 0) + + // scripts for _, s := range script { if s.Disabled { continue } - ret.Script = append(ret.Script, s) + run.Script = append(run.Script, s) + } + + err := CompileRunSymlinks(run, symlinks) + if err != nil { + return run, err + } + + err = CompileRunWalkDir(run, walkdir) + if err != nil { + return run, err } + return run, nil +} +func CompileRunSymlinks(run *Run, symlinks map[string]string) error { + + // symlinks for target, link := range symlinks { li := &LinkInfo{} - ret.Links = append(ret.Links, li) + run.Links = append(run.Links, li) li.OrigTarget = target // resolve target tilde prefix if strings.HasPrefix(target, "~/") { - target = filepath.Join(ret.HomeDir, target[2:]) + target = filepath.Join(run.HomeDir, target[2:]) } abslink, err := filepath.Abs(link) @@ -116,5 +133,29 @@ func CompileRun(symlinks map[string]string, script []*Script) (*Run, error) { } - return ret, nil + return nil +} + +func CompileRunWalkDir(run *Run, walkdir map[string]string) error { + + // compile a walkdir into a CompileRunSymlinks + symlinks := make(map[string]string, 0) + + for targetdir, srcdir := range walkdir { + filenames, err := ListDir(srcdir) + if err != nil { + log.Warnf("ListDir %s: %s", srcdir, err) + continue + } + + for _, filename := range filenames { + + log.Tracef("ProcessWalkDir %s", filename) + src := filename + target := filepath.Join(targetdir, filename) + symlinks[src] = target + } + + } + return CompileRunSymlinks(run, symlinks) } diff --git a/status.go b/status.go index c78ab22..f2ef37b 100644 --- a/status.go +++ b/status.go @@ -59,7 +59,7 @@ func (me *Status) RunFile(path string, asJson, showAll bool) error { cfg.PrintConfig() } - run, err := CompileRun(cfg.Symlinks, cfg.Script) + run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script) if err != nil { return err } diff --git a/unlink.go b/unlink.go index 9045d08..7cd55b3 100644 --- a/unlink.go +++ b/unlink.go @@ -80,7 +80,7 @@ func (me *Unlink) RunFile(opts *UnlinkOptions, path string) error { } func (me *Unlink) RunConfig(opts *UnlinkOptions, cfg *AppConfig) error { - run, err := CompileRun(cfg.Symlinks, cfg.Script) + run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script) if err != nil { return err } @@ -147,7 +147,7 @@ func (me *Unlink) RunAutoMode(opts *UnlinkOptions) error { cfg.Symlinks["~/"+filename] = filename } - run, err := CompileRun(cfg.Symlinks, cfg.Script) + run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script) if err != nil { return err }