Skip to content

Commit

Permalink
perform all logic in the register function
Browse files Browse the repository at this point in the history
  • Loading branch information
seanenck committed Nov 12, 2024
1 parent 22b15f4 commit d9ec5fa
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions internal/dotfiles/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,44 +103,21 @@ func (v variables) list() ([]dotfile, error) {
}
script := lua.NewState()
defer script.Close()
entries := make(map[bool][]string)
var lErr []error
fxn := func(l *lua.LState) int {
s := strings.TrimSpace(l.ToString(1))
if s != "" {
negate := strings.HasPrefix(s, "!")
if negate {
s = s[1:]
}
has, ok := entries[negate]
if !ok {
has = []string{}
}
entries[negate] = append(has, s)
}
return 1
}
exists := func(l *lua.LState) int {
s := l.ToString(1)
s = os.Expand(s, os.Getenv)
if paths.Exists(s) {
return 1
}
return 0
}
script.SetGlobal("register", script.NewFunction(fxn))
script.SetGlobal("exists", script.NewFunction(exists))
if err := script.DoFile(path); err != nil {
return nil, err
}
for negate, values := range entries {
for _, line := range values {
t := line
full := filepath.Join(v.root, t)
full := filepath.Join(v.root, s)
items := []string{full}
if strings.Contains(full, "*") {
globs, err := filepath.Glob(full)
if err != nil {
return nil, err
lErr = append(lErr, err)
return 0
}
items = globs
}
Expand All @@ -163,10 +140,28 @@ func (v variables) list() ([]dotfile, error) {
return nil
})
if err != nil {
return nil, err
lErr = append(lErr, err)
return 0
}
}
}
return 1
}
exists := func(l *lua.LState) int {
s := l.ToString(1)
s = os.Expand(s, os.Getenv)
if paths.Exists(s) {
return 1
}
return 0
}
script.SetGlobal("register", script.NewFunction(fxn))
script.SetGlobal("exists", script.NewFunction(exists))
if err := script.DoFile(path); err != nil {
return nil, err
}
if len(lErr) > 0 {
return nil, errors.Join(lErr...)
}
if len(found) == 0 {
return nil, errors.New("no items matched")
Expand Down

0 comments on commit d9ec5fa

Please sign in to comment.