summaryrefslogtreecommitdiff
path: root/model/new.go
diff options
context:
space:
mode:
Diffstat (limited to 'model/new.go')
-rw-r--r--model/new.go98
1 files changed, 59 insertions, 39 deletions
diff --git a/model/new.go b/model/new.go
index 94e19a8..fc57ad0 100644
--- a/model/new.go
+++ b/model/new.go
@@ -4,13 +4,58 @@ import (
"errors"
"github.com/ewy1/pik/flags"
"github.com/ewy1/pik/identity"
+ "github.com/ewy1/pik/spool"
"io/fs"
"path/filepath"
"strings"
"sync"
)
-func NewState(rootFs fs.FS, locations []string, indexers []Indexer, runners []Runner) (*State, []error) {
+func NewSource(rootFs fs.FS, loc string, indexers []Indexer, runners []Runner) (*Source, []error) {
+ var errs []error
+ _, dirName := filepath.Split(strings.TrimSuffix(loc, "/"))
+ src := &Source{
+ Path: loc,
+ Identity: identity.New(dirName),
+ Whitelists: make(map[string][]string),
+ }
+ loc = strings.TrimSuffix(loc, "/")
+ loc = strings.TrimPrefix(loc, "/")
+
+ if loc == "" {
+ return nil, nil
+ }
+
+ locationWg := sync.WaitGroup{}
+ var targets = make([][]Target, len(indexers), len(indexers))
+ for ti, indexer := range indexers {
+ locationWg.Go(func() {
+ subFs, err := fs.Sub(rootFs, loc)
+ if err != nil && !errors.Is(err, fs.ErrNotExist) {
+ errs = append(errs, err)
+ return
+ }
+ result, err := indexer.Index("/"+loc, subFs, runners)
+ if err != nil && !errors.Is(err, fs.ErrNotExist) {
+ errs = append(errs, err)
+ return
+ }
+ targets[ti] = result
+ })
+ }
+ locationWg.Wait()
+
+ for _, t := range targets {
+ if t == nil {
+ continue
+ }
+ src.Targets = append(src.Targets, t...)
+ }
+
+ return src, errs
+}
+
+func NewState(rootFs fs.FS, locations []string, data map[string]*SourceData, indexers []Indexer, runners []Runner) (*State, []error) {
var errs []error
st := &State{
All: *flags.All,
@@ -19,46 +64,12 @@ func NewState(rootFs fs.FS, locations []string, indexers []Indexer, runners []Ru
var sources = make([]*Source, len(locations), len(locations))
for i, loc := range locations {
wg.Go(func() {
- _, dirName := filepath.Split(strings.TrimSuffix(loc, "/"))
- src := &Source{
- Path: loc,
- Identity: identity.New(dirName),
- Whitelists: make(map[string][]string),
+ src, err := NewSource(rootFs, loc, indexers, runners)
+ errs = append(errs, err...)
+ for _, e := range err {
+ _, _ = spool.Warn("%v\n", e)
}
sources[i] = src
- loc = strings.TrimSuffix(loc, "/")
- loc = strings.TrimPrefix(loc, "/")
-
- if loc == "" {
- return
- }
-
- locationWg := sync.WaitGroup{}
- var targets = make([][]Target, len(indexers), len(indexers))
- for ti, indexer := range indexers {
- locationWg.Go(func() {
- subFs, err := fs.Sub(rootFs, loc)
- if err != nil && !errors.Is(err, fs.ErrNotExist) {
- errs = append(errs, err)
- return
- }
- result, err := indexer.Index("/"+loc, subFs, runners)
- if err != nil && !errors.Is(err, fs.ErrNotExist) {
- errs = append(errs, err)
- return
- }
- targets[ti] = result
- })
- }
- locationWg.Wait()
-
- for _, t := range targets {
- if t == nil {
- continue
- }
- sources[i].Targets = append(sources[i].Targets, t...)
- }
-
})
}
@@ -71,5 +82,14 @@ func NewState(rootFs fs.FS, locations []string, indexers []Indexer, runners []Ru
st.Sources = append(st.Sources, s)
}
+ err := Wants(rootFs, st, data, indexers, runners)
+ if err != nil {
+ errs = append(errs, err)
+ }
+ err = Include(rootFs, st, data, indexers, runners)
+ if err != nil {
+ errs = append(errs, err)
+ }
+
return st, errs
}