// SPDX-FileCopyrightText: 2026 Romain Maneschi // // SPDX-License-Identifier: MIT package model import ( "fmt" "io/fs" "os" ) type FsBase string const ( FS_BASE_WORKTREE = FsBase("worktree") FS_BASE_WEBCONTENT = FsBase("webcontent") FS_BASE_CACHE = FsBase("cache") ) type GrFs struct { base FsBase server ServerNeeded fs fs.FS } type ServerNeeded interface { WriteContent(fromFs FsBase, filepath, content string) error ReplaceContent(fromFs FsBase, filepath string, oldContent string, content string) error CopyFile(fromFs FsBase, fromPath string, toFs FsBase, toPath string) error DeleteFile(fromFs FsBase, filename string) error MoveFile(fromFs FsBase, fromPath string, toFs FsBase, toPath string) error } func NewGrFs(base FsBase, server ServerNeeded) *GrFs { return &GrFs{ base: base, server: server, fs: os.DirFS(fmt.Sprintf("/%s", base)), } } func NewGrFsWithFs(base FsBase, server ServerNeeded, origFs fs.FS) *GrFs { return &GrFs{ base: base, server: server, fs: origFs, } } // Implements all os.DirFS methods func (g *GrFs) Open(name string) (fs.File, error) { return g.fs.Open(name) } func (g *GrFs) ReadFile(name string) ([]byte, error) { return fs.ReadFile(g.fs, name) } func (g *GrFs) ReadDir(name string) ([]fs.DirEntry, error) { return fs.ReadDir(g.fs, name) } func (g *GrFs) Stat(name string) (fs.FileInfo, error) { return fs.Stat(g.fs, name) } func (g *GrFs) Lstat(name string) (fs.FileInfo, error) { return fs.Lstat(g.fs, name) } func (g *GrFs) ReadLink(name string) (string, error) { return fs.ReadLink(g.fs, name) } // Personnal methods func (g *GrFs) WriteContent(filepath, content string) error { return g.server.WriteContent(g.base, filepath, content) } func (g *GrFs) ReplaceContent(filepath string, oldContent string, content string) error { return g.server.ReplaceContent(g.base, filepath, oldContent, content) } func (g *GrFs) CopyFile(fromPath string, toPath string) error { return g.server.CopyFile(g.base, fromPath, g.base, toPath) } func (g *GrFs) CopyFileToFs(fromPath string, toFs *GrFs, toPath string) error { return g.server.CopyFile(g.base, fromPath, toFs.base, toPath) } func (g *GrFs) DeleteFile(filename string) error { return g.server.DeleteFile(g.base, filename) } func (g *GrFs) MoveFile(fromPath string, toPath string) error { return g.server.MoveFile(g.base, fromPath, g.base, toPath) } func (g *GrFs) MoveFileToFs(fromPath string, toFs *GrFs, toPath string) error { return g.server.MoveFile(g.base, fromPath, toFs.base, toPath) }