Skip to content

Commit

Permalink
support for walkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmonsays committed Apr 1, 2024
1 parent 7b9e7e3 commit 871dd52
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
55 changes: 48 additions & 7 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 871dd52

Please sign in to comment.