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: MIT
4
5package model
6
7import (
8 "fmt"
9 "io/fs"
10 "os"
11)
12
13type FsBase string
14
15const (
16 FS_BASE_WORKTREE = FsBase("worktree")
17 FS_BASE_WEBCONTENT = FsBase("webcontent")
18 FS_BASE_CACHE = FsBase("cache")
19)
20
21type GrFs struct {
22 base FsBase
23 server ServerNeeded
24 fs fs.FS
25}
26
27type ServerNeeded interface {
28 WriteContent(fromFs FsBase, filepath, content string) error
29 ReplaceContent(fromFs FsBase, filepath string, oldContent string, content string) error
30 CopyFile(fromFs FsBase, fromPath string, toFs FsBase, toPath string) error
31 DeleteFile(fromFs FsBase, filename string) error
32 MoveFile(fromFs FsBase, fromPath string, toFs FsBase, toPath string) error
33}
34
35func NewGrFs(base FsBase, server ServerNeeded) *GrFs {
36 return &GrFs{
37 base: base,
38 server: server,
39 fs: os.DirFS(fmt.Sprintf("/%s", base)),
40 }
41}
42
43func NewGrFsWithFs(base FsBase, server ServerNeeded, origFs fs.FS) *GrFs {
44 return &GrFs{
45 base: base,
46 server: server,
47 fs: origFs,
48 }
49}
50
51// Implements all os.DirFS methods
52
53func (g *GrFs) Open(name string) (fs.File, error) {
54 return g.fs.Open(name)
55}
56
57func (g *GrFs) ReadFile(name string) ([]byte, error) {
58 return fs.ReadFile(g.fs, name)
59}
60
61func (g *GrFs) ReadDir(name string) ([]fs.DirEntry, error) {
62 return fs.ReadDir(g.fs, name)
63}
64
65func (g *GrFs) Stat(name string) (fs.FileInfo, error) {
66 return fs.Stat(g.fs, name)
67}
68
69func (g *GrFs) Lstat(name string) (fs.FileInfo, error) {
70 return fs.Lstat(g.fs, name)
71}
72
73func (g *GrFs) ReadLink(name string) (string, error) {
74 return fs.ReadLink(g.fs, name)
75}
76
77// Personnal methods
78func (g *GrFs) WriteContent(filepath, content string) error {
79 return g.server.WriteContent(g.base, filepath, content)
80}
81
82func (g *GrFs) ReplaceContent(filepath string, oldContent string, content string) error {
83 return g.server.ReplaceContent(g.base, filepath, oldContent, content)
84}
85
86func (g *GrFs) CopyFile(fromPath string, toPath string) error {
87 return g.server.CopyFile(g.base, fromPath, g.base, toPath)
88}
89
90func (g *GrFs) CopyFileToFs(fromPath string, toFs *GrFs, toPath string) error {
91 return g.server.CopyFile(g.base, fromPath, toFs.base, toPath)
92}
93
94func (g *GrFs) DeleteFile(filename string) error {
95 return g.server.DeleteFile(g.base, filename)
96}
97
98func (g *GrFs) MoveFile(fromPath string, toPath string) error {
99 return g.server.MoveFile(g.base, fromPath, g.base, toPath)
100}
101
102func (g *GrFs) MoveFileToFs(fromPath string, toFs *GrFs, toPath string) error {
103 return g.server.MoveFile(g.base, fromPath, toFs.base, toPath)
104}