summaryrefslogtreecommitdiff
path: root/funcs/file.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-01-25 20:11:31 -0500
committerGitHub <noreply@github.com>2024-01-25 20:11:31 -0500
commitebb97fb7367fb983cffc1935a8fb57e4b80f5249 (patch)
tree43ef6cd01f629f60f59efe1e5b003f7c8e3a1257 /funcs/file.go
parentf1d9158ea99abbe556251c1ff2fe970f3b460ee9 (diff)
Move funcs package to internal (#1977)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'funcs/file.go')
-rw-r--r--funcs/file.go128
1 files changed, 0 insertions, 128 deletions
diff --git a/funcs/file.go b/funcs/file.go
deleted file mode 100644
index 32af47cf..00000000
--- a/funcs/file.go
+++ /dev/null
@@ -1,128 +0,0 @@
-package funcs
-
-import (
- "context"
- "io/fs"
- "path/filepath"
-
- osfs "github.com/hack-pad/hackpadfs/os"
- "github.com/hairyhenderson/gomplate/v4/conv"
- "github.com/hairyhenderson/gomplate/v4/internal/datafs"
- "github.com/hairyhenderson/gomplate/v4/internal/iohelpers"
-)
-
-// FileNS - the File namespace
-//
-// Deprecated: don't use
-func FileNS() *FileFuncs {
- return &FileFuncs{}
-}
-
-// AddFileFuncs -
-//
-// Deprecated: use [CreateFileFuncs] instead
-func AddFileFuncs(f map[string]interface{}) {
- for k, v := range CreateFileFuncs(context.Background()) {
- f[k] = v
- }
-}
-
-// CreateFileFuncs -
-func CreateFileFuncs(ctx context.Context) map[string]interface{} {
- fsys, err := datafs.FSysForPath(ctx, "/")
- if err != nil {
- fsys = datafs.WrapWdFS(osfs.NewFS())
- }
-
- ns := &FileFuncs{
- ctx: ctx,
- fs: fsys,
- }
-
- return map[string]interface{}{
- "file": func() interface{} { return ns },
- }
-}
-
-// FileFuncs -
-type FileFuncs struct {
- ctx context.Context
- fs fs.FS
-}
-
-// Read -
-func (f *FileFuncs) Read(path interface{}) (string, error) {
- b, err := fs.ReadFile(f.fs, conv.ToString(path))
- return string(b), err
-}
-
-// Stat -
-func (f *FileFuncs) Stat(path interface{}) (fs.FileInfo, error) {
- return fs.Stat(f.fs, conv.ToString(path))
-}
-
-// Exists -
-func (f *FileFuncs) Exists(path interface{}) bool {
- _, err := f.Stat(conv.ToString(path))
- return err == nil
-}
-
-// IsDir -
-func (f *FileFuncs) IsDir(path interface{}) bool {
- i, err := f.Stat(conv.ToString(path))
- return err == nil && i.IsDir()
-}
-
-// ReadDir -
-func (f *FileFuncs) ReadDir(path interface{}) ([]string, error) {
- des, err := fs.ReadDir(f.fs, conv.ToString(path))
- if err != nil {
- return nil, err
- }
-
- names := make([]string, len(des))
- for i, de := range des {
- names[i] = de.Name()
- }
-
- return names, nil
-}
-
-// Walk -
-func (f *FileFuncs) Walk(path interface{}) ([]string, error) {
- files := make([]string, 0)
- err := fs.WalkDir(f.fs, conv.ToString(path), func(subpath string, d fs.DirEntry, err error) error {
- if err != nil {
- return err
- }
-
- // fs.WalkDir always uses slash-separated paths, even on Windows. We
- // need to convert them to the OS-specific separator as that was the
- // previous behavior.
- subpath = filepath.FromSlash(subpath)
-
- files = append(files, subpath)
- return nil
- })
- return files, err
-}
-
-// Write -
-func (f *FileFuncs) Write(path interface{}, data interface{}) (s string, err error) {
- type byteser interface{ Bytes() []byte }
-
- var content []byte
- fname := conv.ToString(path)
-
- if b, ok := data.([]byte); ok {
- content = b
- } else if b, ok := data.(byteser); ok {
- content = b.Bytes()
- } else {
- content = []byte(conv.ToString(data))
- }
-
- err = iohelpers.WriteFile(f.fs, fname, content)
-
- return "", err
-}