GitRoot

Craft your forge, Build your project, Grow your community freely
 1// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: EUPL-1.2
 4
 5package fs
 6
 7import (
 8	"context"
 9	"io/fs"
10	"strconv"
11	"testing/fstest"
12
13	"gitroot.dev/server/logger"
14)
15
16type PluginFs struct {
17	num_fs int
18	logger *logger.Logger
19	subFs  *multipleFs
20}
21
22const (
23	web   = "webcontent"
24	git   = "worktree"
25	cache = "cache"
26)
27
28var num_fs = 0
29
30func NewPluginEmpty(ctx context.Context) *PluginFs {
31	empty := map[string]fs.FS{
32		web:   fstest.MapFS{},
33		git:   fstest.MapFS{},
34		cache: fstest.MapFS{},
35	}
36	num_fs++
37	f := &PluginFs{
38		num_fs: num_fs,
39		logger: logger.NewLoggerCtx(logger.FS_PLUGIN, ctx).NewSubLogger("pluginFs").NewSubLogger(strconv.Itoa(num_fs)),
40		subFs:  newMultiple(ctx, num_fs, empty),
41	}
42	f.logger.Debug("create fs")
43	return f
44}
45
46func (f *PluginFs) Clear() *PluginFs {
47	return f.
48		Web(fstest.MapFS{}).
49		Git(fstest.MapFS{}).
50		Cache(fstest.MapFS{})
51}
52
53func (f *PluginFs) Get() fs.FS {
54	return f.subFs
55}
56
57func (f *PluginFs) Git(g fs.FS) *PluginFs {
58	f.logger.Debug("update git sub fs", logger.NewLoggerPair("git", g != nil))
59	f.subFs.UpdateSubFs(git, g)
60	return f
61}
62
63func (f *PluginFs) Web(g fs.FS) *PluginFs {
64	f.logger.Debug("update web sub fs", logger.NewLoggerPair("web", g != nil))
65	f.subFs.UpdateSubFs(web, g)
66	return f
67}
68
69func (f *PluginFs) Cache(g fs.FS) *PluginFs {
70	f.logger.Debug("update cache sub fs", logger.NewLoggerPair("cache", g != nil))
71	f.subFs.UpdateSubFs(cache, g)
72	return f
73}