summaryrefslogtreecommitdiff
path: root/file/file.go
blob: 6b7a8e8a3a880baa0b708fcbb60dc546ec90062b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Package file contains functions for working with files and directories on the local filesystem
package file

import (
	"fmt"
	"io/fs"

	"github.com/hairyhenderson/gomplate/v4/internal/datafs"
	"github.com/hairyhenderson/gomplate/v4/internal/iohelpers"

	osfs "github.com/hack-pad/hackpadfs/os"
)

// fsys for legacy functions - see deprecation notices
var fsys = datafs.WrapWdFS(osfs.NewFS())

// Read the contents of the referenced file, as a string.
//
// Deprecated: (as of 4.0.0) use [io/fs#ReadFile] instead
func Read(filename string) (string, error) {
	bytes, err := fs.ReadFile(fsys, filename)
	if err != nil {
		err = fmt.Errorf("read failed for %s: %w", filename, err)
		return "", err
	}
	return string(bytes), nil
}

// ReadDir gets a directory listing.
//
// Deprecated: (as of 4.0.0) use [io/fs#ReadDir] instead
func ReadDir(path string) ([]string, error) {
	des, err := fs.ReadDir(fsys, path)
	if err != nil {
		return nil, err
	}

	names := make([]string, len(des))
	for i, de := range des {
		names[i] = de.Name()
	}

	return names, nil
}

// Write the given content to the file, truncating any existing file, and
// creating the directory structure leading up to it if necessary.
//
// Deprecated: (as of 4.0.0) use [os#WriteFile] instead
func Write(filename string, content []byte) error {
	return iohelpers.WriteFile(fsys, filename, content)
}