Allow helmfile statefile to passed by stdin (#520)

* Allow helmfile statefile to passed by stdin

Signed-off-by: xiaomudk <xiaomudk@gmail.com>
This commit is contained in:
xiaomudk
2022-11-22 11:34:05 +08:00
committed by GitHub
parent ce7f38ee96
commit 7844145ee3
6 changed files with 131 additions and 17 deletions
+32 -1
View File
@@ -1,11 +1,27 @@
package filesystem
import (
"io"
"io/fs"
"os"
"path/filepath"
"time"
)
type fileStat struct {
name string
size int64
mode fs.FileMode
modTime time.Time
}
func (fs fileStat) Name() string { return fs.name }
func (fs fileStat) Size() int64 { return fs.size }
func (fs fileStat) Mode() fs.FileMode { return fs.mode }
func (fs fileStat) ModTime() time.Time { return fs.modTime }
func (fs fileStat) IsDir() bool { return fs.mode.IsDir() }
func (fs fileStat) Sys() any { return nil }
type FileSystem struct {
ReadFile func(string) ([]byte, error)
ReadDir func(string) ([]fs.DirEntry, error)
@@ -22,7 +38,6 @@ type FileSystem struct {
func DefaultFileSystem() *FileSystem {
dfs := FileSystem{
ReadFile: os.ReadFile,
ReadDir: os.ReadDir,
DeleteFile: os.Remove,
Stat: os.Stat,
@@ -32,6 +47,8 @@ func DefaultFileSystem() *FileSystem {
Abs: filepath.Abs,
}
dfs.Stat = dfs.stat
dfs.ReadFile = dfs.readFile
dfs.FileExistsAt = dfs.fileExistsAtDefault
dfs.DirectoryExistsAt = dfs.directoryExistsDefault
dfs.FileExists = dfs.fileExistsDefault
@@ -78,6 +95,20 @@ func FromFileSystem(params FileSystem) *FileSystem {
return dfs
}
func (filesystem *FileSystem) stat(name string) (os.FileInfo, error) {
if name == "-" {
return fileStat{mode: 0}, nil
}
return os.Stat(name)
}
func (filesystem *FileSystem) readFile(name string) ([]byte, error) {
if name == "-" {
return io.ReadAll(os.Stdin)
}
return os.ReadFile(name)
}
func (filesystem *FileSystem) fileExistsAtDefault(path string) bool {
fileInfo, err := filesystem.Stat(path)
return err == nil && fileInfo.Mode().IsRegular()
+70 -14
View File
@@ -4,30 +4,19 @@ import (
"errors"
"io/fs"
"os"
"path"
"strings"
"testing"
"time"
)
type TestFileInfo struct {
mode fs.FileMode
}
func (tfi TestFileInfo) Name() string { return "" }
func (tfi TestFileInfo) Size() int64 { return 0 }
func (tfi TestFileInfo) Mode() fs.FileMode { return tfi.mode }
func (tfi TestFileInfo) ModTime() time.Time { return time.Time{} }
func (tfi TestFileInfo) IsDir() bool { return tfi.mode.IsDir() }
func (tfi TestFileInfo) Sys() any { return nil }
func NewTestFileSystem() FileSystem {
replaceffs := FileSystem{
Stat: func(s string) (os.FileInfo, error) {
if strings.HasPrefix(s, "existing_file") {
return TestFileInfo{mode: 0}, nil
return fileStat{mode: 0}, nil
}
if strings.HasPrefix(s, "existing_dir") {
return TestFileInfo{mode: fs.ModeDir}, nil
return fileStat{mode: fs.ModeDir}, nil
}
return nil, errors.New("Error")
},
@@ -46,6 +35,12 @@ func TestFs_fileExistsDefault(t *testing.T) {
if exists {
t.Errorf("Not expected file %s, found", "non_existing_file.txt")
}
dfs := DefaultFileSystem()
exists, _ = dfs.FileExists("-")
if !exists {
t.Errorf("Not expected file %s, not found", "-")
}
}
func TestFs_fileExistsAtDefault(t *testing.T) {
@@ -65,6 +60,12 @@ func TestFs_fileExistsAtDefault(t *testing.T) {
if exists {
t.Errorf("Not expected file %s, found", "existing_dir")
}
dfs := DefaultFileSystem()
exists = dfs.FileExistsAt("-")
if !exists {
t.Errorf("Not expected file %s, not found", "-")
}
}
func TestFs_directoryExistsDefault(t *testing.T) {
@@ -80,6 +81,61 @@ func TestFs_directoryExistsDefault(t *testing.T) {
}
}
func TestFsTeadFile(t *testing.T) {
cases := []struct {
name string
content []byte
path string
wantError string
}{
{
name: "read file",
content: []byte("hello helmfile"),
path: "helmfile.yaml",
},
{
name: "read file from stdin",
content: []byte("hello helmfile"),
path: "-",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
dir := t.TempDir()
yamlPath := path.Join(dir, c.path)
dfs := DefaultFileSystem()
tmpfile, err := os.Create(yamlPath)
if err != nil {
t.Errorf("create file %s error: %v", yamlPath, err)
}
_, err = tmpfile.Write(c.content)
if err != nil {
t.Errorf(" write to file %s error: %v", yamlPath, err)
}
readPath := yamlPath
if c.path == "-" {
readPath = c.path
oldOsStdin := os.Stdin
defer func() { os.Stdin = oldOsStdin }()
os.Stdin = tmpfile
}
if _, err = tmpfile.Seek(0, 0); err != nil {
t.Errorf("file %s seek error: %v", yamlPath, err)
}
want, err := dfs.readFile(readPath)
if err != nil {
t.Errorf("read file %s error: %v", readPath, err)
} else {
if string(c.content) != string(want) {
t.Errorf("nexpected error: unexpected=%s, got=%v", string(c.content), string(want))
}
}
})
}
}
func TestFs_DefaultBuilder(t *testing.T) {
ffs := DefaultFileSystem()
if ffs.ReadFile == nil ||