55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
package node
|
|
|
|
import core "dappco.re/go/core"
|
|
|
|
// localFileSystem is the package-scoped filesystem rooted at `/` so node code
|
|
// can use Core file operations without os helpers.
|
|
var localFileSystem = (&core.Fs{}).New("/")
|
|
|
|
func filesystemEnsureDir(path string) error {
|
|
return filesystemResultError(localFileSystem.EnsureDir(path))
|
|
}
|
|
|
|
func filesystemWrite(path, content string) error {
|
|
return filesystemResultError(localFileSystem.Write(path, content))
|
|
}
|
|
|
|
func filesystemRead(path string) (string, error) {
|
|
result := localFileSystem.Read(path)
|
|
if !result.OK {
|
|
return "", filesystemResultError(result)
|
|
}
|
|
|
|
content, ok := result.Value.(string)
|
|
if !ok {
|
|
return "", core.E("node.filesystemRead", "filesystem read returned non-string content", nil)
|
|
}
|
|
|
|
return content, nil
|
|
}
|
|
|
|
func filesystemDelete(path string) error {
|
|
return filesystemResultError(localFileSystem.Delete(path))
|
|
}
|
|
|
|
func filesystemRename(oldPath, newPath string) error {
|
|
return filesystemResultError(localFileSystem.Rename(oldPath, newPath))
|
|
}
|
|
|
|
func filesystemExists(path string) bool {
|
|
return localFileSystem.Exists(path)
|
|
}
|
|
|
|
func filesystemResultError(result core.Result) error {
|
|
if result.OK {
|
|
return nil
|
|
}
|
|
|
|
if err, ok := result.Value.(error); ok && err != nil {
|
|
return err
|
|
}
|
|
|
|
return core.E("node.fs", "filesystem operation failed", nil)
|
|
}
|