Skip to content

Commit

Permalink
add includes to CompileRun
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmonsays committed Apr 2, 2024
1 parent fb06a23 commit 87a9cf1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 82 deletions.
11 changes: 6 additions & 5 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ func (me *Clean) RunFile(path string) error {
if log.IsTrace() {
cfg.PrintConfig()
}
return me.RunConfig(cfg)
return me.RunConfig(cfg, path)
}

func (me *Clean) RunConfig(cfg *AppConfig) error {
func (me *Clean) RunConfig(cfg *AppConfig, path string) error {

err := me.CleanUnreferenced(cfg)
err := me.CleanUnreferenced(cfg, path)
if err != nil {
log.Warnf("Clean unreferenced %s", err)
}

return nil
}
func (me *Clean) CleanUnreferenced(cfg *AppConfig) error {
run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script)
func (me *Clean) CleanUnreferenced(cfg *AppConfig, path string) error {

run, err := CompileRun(path, cfg.Symlinks, cfg.WalkDir, cfg.Script, cfg.Include)
if err != nil {
return err
}
Expand Down
82 changes: 18 additions & 64 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (me *Link) Flags() []cli.Flag {
}

type LinkOptions struct {
Dir string
// Dir string
Pretend bool
AutoMode bool
Copy bool
Expand Down Expand Up @@ -77,8 +77,8 @@ func (me *Link) Run(c *cli.Context) error {
}

func (me *Link) RunFile(opts *LinkOptions, path string) error {

log.Tracef("runfile %s", path)

cfg := GetDefaultConfig()
err := cfg.LoadYaml(path)
if err != nil {
Expand All @@ -95,74 +95,28 @@ func (me *Link) RunFile(opts *LinkOptions, path string) error {
}

// run includes - expand the globs
includes, err := me.GetIncludes(path, cfg.Include)
if err != nil {
log.Errorf("GetIncludes %s: %s", path, err)
return err
}

for _, include := range includes {
err := me.RunFile(opts, include)
if err != nil {
log.Errorf("RunFile(include) %s: %s", include, err)
return err
}
}

return nil
}

func (me *Link) GetIncludes(path string, includes []string) ([]string, error) {
ret := make([]string, 0)

if len(includes) == 0 {
log.Tracef("no includes to process in %s", path)
return nil, nil
}
for _, include := range includes {
matches, err := filepath.Glob(include)
if err != nil {
log.Warnf("Glob %s: %", include, err)
continue
}
ret = append(ret, matches...)
}
return ret, nil
}

// change directory to a filename
func ChdirToFile(path string) error {
abs, err := filepath.Abs(path)
if err != nil {
log.Warnf("abs %s: %s", path, err)
return err
}
dir := filepath.Dir(abs)
log.Tracef("chdir %s", dir)
err = os.Chdir(dir)
if err != nil {
log.Errorf("Chdir %s: %s", dir, err)
return err
}
// todo: Trash me.. I think
// includes, err := GetIncludes(path, cfg.Include)
// if err != nil {
// log.Errorf("GetIncludes %s: %s", path, err)
// return err
// }

// for _, include := range includes {
// err := me.RunFile(opts, include)
// if err != nil {
// log.Errorf("RunFile(include) %s: %s", include, err)
// return err
// }
// }

return nil
}

func (me *Link) RunConfig(path string, opts *LinkOptions, cfg *AppConfig) error {

// change dir and then change back before returning
dir, _ := os.Getwd()
ChdirToFile(path)
defer func() {
log.Tracef("chdir to old dir %s", dir)
os.Chdir(dir)
}()

log.Tracef("RunConfig %s (in %s)", path, dir)
opts.Dir = dir

// compile run
run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script)
run, err := CompileRun(path, cfg.Symlinks, cfg.WalkDir, cfg.Script, cfg.Include)
if err != nil {
return err
}
Expand Down Expand Up @@ -317,7 +271,7 @@ func (me *Link) RunAutoMode(opts *LinkOptions) error {
cfg.Symlinks["~/"+filename] = filename
}

run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script)
run, err := CompileRun("", cfg.Symlinks, cfg.WalkDir, cfg.Script, cfg.Include)
if err != nil {
return err
}
Expand Down
94 changes: 86 additions & 8 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
)

func NewRun() *Run {
ret := &Run{}
run := &Run{}
usr, _ := user.Current()
homedir := usr.HomeDir
log.Tracef("homedir is %s", homedir)
ret.HomeDir = homedir
return ret
run.HomeDir = homedir
run.Script = make([]*Script, 0)
return run
}

type Run struct {
Expand All @@ -33,9 +34,30 @@ type LinkInfo struct {
NeedsCreate bool
}

func CompileRun(symlinks, walkdir map[string]string, script []*Script) (*Run, error) {
func CompileRun(path string, symlinks, walkdir map[string]string, script []*Script, include []string) (*Run, error) {
run := NewRun()
run.Script = make([]*Script, 0)
err := CompileRunWithRun(run, path, symlinks, walkdir, script, include)
if err != nil {
return nil, err
}
return run, nil
}

func CompileRunWithRun(
run *Run,
path string,
symlinks,
walkdir map[string]string,
script []*Script,
include []string) error {

// change dir and then change back before returning
dir, _ := os.Getwd()
ChdirToFile(path)
defer func() {
log.Tracef("chdir to old dir %s", dir)
os.Chdir(dir)
}()

// scripts
for _, s := range script {
Expand All @@ -48,17 +70,37 @@ func CompileRun(symlinks, walkdir map[string]string, script []*Script) (*Run, er
err := CompileRunSymlinks(run, symlinks)
if err != nil {
log.Warnf("CompileRunSymlinks %s", err)
return run, err
return err
}

err = CompileRunWalkDir(run, walkdir)
if err != nil {
log.Warnf("CompileRunWalkDir %s", err)
return run, err
return err
}

return run, nil
includes, err := GetIncludes(path, include)
if err != nil {
log.Warnf("GetIncludes %s", err)
return err
}
for _, include := range includes {
cfg2 := GetDefaultConfig()
err := cfg2.LoadYaml(include)
if err != nil {
log.Errorf("Include %s: %s", include, err)
continue
}
err = CompileRunWithRun(run, include, cfg2.Symlinks, cfg2.WalkDir, cfg2.Script, cfg2.Include)
if err != nil {
log.Errorf("Include CompileRunWithRun %s: %s", include, err)
continue
}
}

return nil
}

func CompileRunSymlinks(run *Run, symlinks map[string]string) error {

// symlinks
Expand Down Expand Up @@ -159,3 +201,39 @@ func CompileRunWalkDir(run *Run, walkdir map[string]string) error {
}
return CompileRunSymlinks(run, symlinks)
}

func GetIncludes(path string, includes []string) ([]string, error) {
ret := make([]string, 0)

if len(includes) == 0 {
log.Tracef("no includes to process in %s", path)
return nil, nil
}
for _, include := range includes {
matches, err := filepath.Glob(include)
if err != nil {
log.Warnf("Glob %s: %", include, err)
continue
}
ret = append(ret, matches...)
}
return ret, nil
}

// change directory to a filename
func ChdirToFile(path string) error {
abs, err := filepath.Abs(path)
if err != nil {
log.Warnf("abs %s: %s", path, err)
return err
}
dir := filepath.Dir(abs)
log.Tracef("chdir %s", dir)
err = os.Chdir(dir)
if err != nil {
log.Errorf("Chdir %s: %s", dir, err)
return err
}

return nil
}
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.WalkDir, cfg.Script)
run, err := CompileRun(path, cfg.Symlinks, cfg.WalkDir, cfg.Script, cfg.Include)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func (me *Unlink) RunFile(opts *UnlinkOptions, path string) error {
cfg.PrintConfig()
}

return me.RunConfig(opts, cfg)
return me.RunConfig(opts, cfg, path)
}

func (me *Unlink) RunConfig(opts *UnlinkOptions, cfg *AppConfig) error {
run, err := CompileRun(cfg.Symlinks, cfg.WalkDir, cfg.Script)
func (me *Unlink) RunConfig(opts *UnlinkOptions, cfg *AppConfig, path string) error {
run, err := CompileRun(path, cfg.Symlinks, cfg.WalkDir, cfg.Script, cfg.Include)
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.WalkDir, cfg.Script)
run, err := CompileRun("", cfg.Symlinks, cfg.WalkDir, cfg.Script, cfg.Include)
if err != nil {
return err
}
Expand Down

0 comments on commit 87a9cf1

Please sign in to comment.