diff options
| author | ewy <ewy0@protonmail.com> | 2026-06-02 15:07:59 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-06-02 15:07:59 +0200 |
| commit | f77fc4ef5f2df96c6bba3cd6c7d88a77d61a5a80 (patch) | |
| tree | bdead2d2b245407aaa20e71c433d7c8f757d7049 /model/new.go | |
| parent | 6d607f114f63184a43237a3ff80aee622401ee23 (diff) | |
.wants adds potential out-of-tree sources to your state (for monorepos, for example)
.includes adds targets from another source to this one, adding inheritance
Diffstat (limited to 'model/new.go')
| -rw-r--r-- | model/new.go | 98 |
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 } |
