accept dir for http args
All checks were successful
cicd / cicd (push) Successful in 12s

This commit is contained in:
bel
2023-10-28 10:24:29 -06:00
parent 4b2af6b85e
commit 476c44f5cd
4 changed files with 45 additions and 6 deletions

View File

@@ -3,7 +3,9 @@ package ledger
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"unicode"
)
@@ -18,6 +20,28 @@ func NewFiles(p string, q ...string) (Files, error) {
return f, err
}
func (files Files) paths() []string {
result := make([]string, 0, len(files))
for i := range files {
if info, err := os.Stat(files[i]); err == nil && info.IsDir() {
if err := filepath.WalkDir(files[i], func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
result = append(result, p)
}
return nil
}); err != nil {
panic(err)
}
} else {
result = append(result, files[i])
}
}
return result
}
func (files Files) Add(payee string, delta Delta) error {
currencyValue := fmt.Sprintf("%s%.2f", delta.Currency, delta.Value)
if delta.Currency != USD {
@@ -35,7 +59,7 @@ func (files Files) append(s string) error {
return err
}
f, err := os.OpenFile(string(files[0]), os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
f, err := os.OpenFile(string(files.paths()[0]), os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
@@ -54,7 +78,7 @@ func (files Files) trimTrainlingWhitespace() error {
return nil
}
f, err := os.OpenFile(string(files[0]), os.O_CREATE|os.O_WRONLY, os.ModePerm)
f, err := os.OpenFile(string(files.paths()[0]), os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
@@ -64,7 +88,7 @@ func (files Files) trimTrainlingWhitespace() error {
}
func (files Files) _lastNonWhitespacePos() (int, error) {
f, err := os.Open(string(files[0]))
f, err := os.Open(string(files.paths()[0]))
if os.IsNotExist(err) {
return -1, nil
}

View File

@@ -275,3 +275,18 @@ func TestFileDeltas(t *testing.T) {
})
}
}
func TestFilesOfDir(t *testing.T) {
d := t.TempDir()
files := Files([]string{d, "/dev/null"})
if paths := files.paths(); len(paths) != 1 {
t.Error(paths)
}
os.WriteFile(path.Join(d, "1"), []byte{}, os.ModePerm)
os.Mkdir(path.Join(d, "d2"), os.ModePerm)
os.WriteFile(path.Join(d, "d2", "2"), []byte{}, os.ModePerm)
if paths := files.paths(); len(paths) != 3 || paths[0] != path.Join(d, "1") || paths[1] != path.Join(d, "d2", "2") {
t.Error(paths)
}
}

View File

@@ -35,8 +35,8 @@ func (t transactionRecipient) empty() bool {
func (files Files) transactions() ([]transaction, error) {
result := make([]transaction, 0)
for i := range files {
some, err := files._transactions(files[i])
for _, path := range files.paths() {
some, err := files._transactions(path)
if err != nil {
return nil, err
}