summaryrefslogtreecommitdiff
path: root/env
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-01-22 09:06:33 -0500
committerGitHub <noreply@github.com>2024-01-22 09:06:33 -0500
commit0ac3aa24bf2e4ada9c26fd9ef5b7f0ae8c6b6cfb (patch)
tree9a95f27eec1e77ef8bfefcb2810f7e41681627a5 /env
parentf837061f953bda1e8b42095c6dba0496de11d993 (diff)
Use go-fsimpl to read from datasources (#1336)
* Use go-fsimpl to read from datasources Signed-off-by: Dave Henderson <dhenderson@gmail.com> * trying to fix windows bug Signed-off-by: Dave Henderson <dhenderson@gmail.com> * attempts to fix some of the path madness Signed-off-by: Dave Henderson <dhenderson@gmail.com> * remove 'HOME' from expected env vars Signed-off-by: Dave Henderson <dhenderson@gmail.com> * more tweaks Signed-off-by: Dave Henderson <dhenderson@gmail.com> * lint fix Signed-off-by: Dave Henderson <dhenderson@gmail.com> --------- Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'env')
-rw-r--r--env/env.go52
-rw-r--r--env/env_test.go91
2 files changed, 2 insertions, 141 deletions
diff --git a/env/env.go b/env/env.go
index e9ab3632..598361bf 100644
--- a/env/env.go
+++ b/env/env.go
@@ -2,10 +2,6 @@
package env
import (
- "io/fs"
- "os"
- "strings"
-
osfs "github.com/hack-pad/hackpadfs/os"
"github.com/hairyhenderson/gomplate/v4/internal/datafs"
)
@@ -16,55 +12,11 @@ import (
// Otherwise the provided default (or an emptry string) is returned.
func Getenv(key string, def ...string) string {
fsys := datafs.WrapWdFS(osfs.NewFS())
- return getenvVFS(fsys, key, def...)
+ return datafs.GetenvFsys(fsys, key, def...)
}
// ExpandEnv - like os.ExpandEnv, except supports `_FILE` vars as well
func ExpandEnv(s string) string {
fsys := datafs.WrapWdFS(osfs.NewFS())
- return expandEnvVFS(fsys, s)
-}
-
-// expandEnvVFS -
-func expandEnvVFS(fsys fs.FS, s string) string {
- return os.Expand(s, func(s string) string {
- return getenvVFS(fsys, s)
- })
-}
-
-// getenvVFS - a convenience function intended for internal use only!
-func getenvVFS(fsys fs.FS, key string, def ...string) string {
- val := getenvFile(fsys, key)
- if val == "" && len(def) > 0 {
- return def[0]
- }
-
- return val
-}
-
-func getenvFile(fsys fs.FS, key string) string {
- val := os.Getenv(key)
- if val != "" {
- return val
- }
-
- p := os.Getenv(key + "_FILE")
- if p != "" {
- val, err := readFile(fsys, p)
- if err != nil {
- return ""
- }
- return strings.TrimSpace(val)
- }
-
- return ""
-}
-
-func readFile(fsys fs.FS, p string) (string, error) {
- b, err := fs.ReadFile(fsys, p)
- if err != nil {
- return "", err
- }
-
- return string(b), nil
+ return datafs.ExpandEnvFsys(fsys, s)
}
diff --git a/env/env_test.go b/env/env_test.go
index faaaa603..9f637944 100644
--- a/env/env_test.go
+++ b/env/env_test.go
@@ -1,14 +1,8 @@
package env
import (
- "errors"
- "io/fs"
"os"
"testing"
- "testing/fstest"
-
- "github.com/hack-pad/hackpadfs"
- "github.com/hairyhenderson/gomplate/v4/internal/datafs"
"github.com/stretchr/testify/assert"
)
@@ -19,25 +13,6 @@ func TestGetenv(t *testing.T) {
assert.Equal(t, "default value", Getenv("BLAHBLAHBLAH", "default value"))
}
-func TestGetenvFile(t *testing.T) {
- fsys := fs.FS(fstest.MapFS{
- "tmp": &fstest.MapFile{Mode: fs.ModeDir | 0o777},
- "tmp/foo": &fstest.MapFile{Data: []byte("foo")},
- "tmp/unreadable": &fstest.MapFile{Data: []byte("foo"), Mode: 0o000},
- })
- fsys = datafs.WrapWdFS(fsys)
-
- t.Setenv("FOO_FILE", "/tmp/foo")
- assert.Equal(t, "foo", getenvVFS(fsys, "FOO", "bar"))
-
- t.Setenv("FOO_FILE", "/tmp/missing")
- assert.Equal(t, "bar", getenvVFS(fsys, "FOO", "bar"))
-
- fsys = writeOnly(fsys)
- t.Setenv("FOO_FILE", "/tmp/unreadable")
- assert.Equal(t, "bar", getenvVFS(fsys, "FOO", "bar"))
-}
-
func TestExpandEnv(t *testing.T) {
assert.Empty(t, ExpandEnv("${FOOBARBAZ}"))
assert.Equal(t, os.Getenv("USER"), ExpandEnv("$USER"))
@@ -45,69 +20,3 @@ func TestExpandEnv(t *testing.T) {
assert.Equal(t, os.Getenv("USER")+": "+os.Getenv("HOME"),
ExpandEnv("$USER: ${HOME}"))
}
-
-func TestExpandEnvFile(t *testing.T) {
- fsys := fs.FS(fstest.MapFS{
- "tmp": &fstest.MapFile{Mode: fs.ModeDir | 0o777},
- "tmp/foo": &fstest.MapFile{Data: []byte("foo")},
- "tmp/unreadable": &fstest.MapFile{Data: []byte("foo"), Mode: 0o000},
- })
- fsys = datafs.WrapWdFS(fsys)
-
- t.Setenv("FOO_FILE", "/tmp/foo")
- assert.Equal(t, "foo is foo", expandEnvVFS(fsys, "foo is $FOO"))
-
- t.Setenv("FOO_FILE", "/tmp/missing")
- assert.Equal(t, "empty", expandEnvVFS(fsys, "${FOO}empty"))
-
- fsys = writeOnly(fsys)
- t.Setenv("FOO_FILE", "/tmp/unreadable")
- assert.Equal(t, "", expandEnvVFS(fsys, "${FOO}"))
-}
-
-// Maybe extract this into a separate package sometime...
-// writeOnly - represents a filesystem that's writeable, but read operations fail
-func writeOnly(fsys fs.FS) fs.FS {
- return &woFS{fsys}
-}
-
-type woFS struct {
- fsys fs.FS
-}
-
-func (fsys woFS) Open(name string) (fs.File, error) {
- f, err := fsys.fsys.Open(name)
- return writeOnlyFile(f), err
-}
-
-func (fsys woFS) ReadDir(_ string) ([]fs.DirEntry, error) {
- return nil, ErrWriteOnly
-}
-
-func (fsys woFS) Stat(_ string) (fs.FileInfo, error) {
- return nil, ErrWriteOnly
-}
-
-func writeOnlyFile(f fs.File) fs.File {
- if f == nil {
- return nil
- }
-
- return &woFile{f}
-}
-
-type woFile struct {
- fs.File
-}
-
-// Write -
-func (f woFile) Write(p []byte) (n int, err error) {
- return hackpadfs.WriteFile(f.File, p)
-}
-
-// Read is disabled and returns ErrWriteOnly
-func (f woFile) Read([]byte) (n int, err error) {
- return 0, ErrWriteOnly
-}
-
-var ErrWriteOnly = errors.New("filesystem is write-only")