Skip to content

Commit

Permalink
fix preliminary ledger file closing
Browse files Browse the repository at this point in the history
  • Loading branch information
mput committed Feb 18, 2024
1 parent ea28bab commit 9c33da3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
27 changes: 16 additions & 11 deletions app/bot/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ type Ledger struct {
strict bool
}

func NewLedger(repo repo.RepoService, mainFile string, strict bool) *Ledger {
func NewLedger(rs repo.RepoService, mainFile string, strict bool) *Ledger {
return &Ledger{
repo: repo,
repo: rs,
mainFile: mainFile,
strict: strict,
}
}

const ledgerBinary = "ledger"

func resolveIncludesReader(repo repo.RepoService, file string) (io.ReadCloser, error) {
ledgerFile, err := repo.OpenReader(file)
func resolveIncludesReader(rs repo.RepoService, file string) (io.ReadCloser, error) {
ledgerFile, err := rs.OpenReader(file)
if err != nil {
return nil, err
}
Expand All @@ -43,32 +43,37 @@ func resolveIncludesReader(repo repo.RepoService, file string) (io.ReadCloser, e
if top {
defer w.Close()
}
if err != nil {
w.CloseWithError(fmt.Errorf("unable to open file: %v", err))
return
}
defer ledgerFile.Close()

lcnt := 0
scanner := bufio.NewScanner(ledReader)
for scanner.Scan() {
line := scanner.Text()
lcnt++
linetr := strings.TrimSpace(line)
if strings.HasPrefix(linetr, "include") {
path := strings.TrimPrefix(linetr, "include")
path = strings.TrimSpace(path)
ir, rerr := repo.OpenReader(path)
ir, rerr := rs.OpenReader(path)
if rerr == nil {
f(ir, false)
continue
}
slog.Warn("unable to open include file", "file", path, "error", rerr)
// line will be used as is so ledger will report the error if it's actually an include
slog.Warn("unable to open include file", "file", path, "error", rerr)
}
_, err := fmt.Fprintln(w, line)
if err != nil {
slog.Error("unable to write to pipe", "error", err)
w.CloseWithError(fmt.Errorf("unable to write to pipe: %v", err))
return
}
}
if err := scanner.Err(); err != nil {
slog.Error("unable to read ledger file", "error", err)
w.CloseWithError(fmt.Errorf("unable to read ledger file: %v", err))
return
}
slog.Debug("finish ledger file read", "file", file, "top", top, "lines", lcnt)
}

go f(ledgerFile, true)
Expand Down
8 changes: 8 additions & 0 deletions app/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repo
import (
"fmt"
"io"
"log/slog"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
Expand Down Expand Up @@ -40,6 +41,13 @@ func NewInMemoryRepo(url, token string) (*InMemoryRepo, error) {
if err != nil {
return nil, fmt.Errorf("unable to clone %s: %v", url, err)
}
// git.Repository
ref, err := r.Head()
if err != nil {
return nil, err
}

slog.Info("repo clonned", "head", ref.Hash())

return &InMemoryRepo{
url: url,
Expand Down

0 comments on commit 9c33da3

Please sign in to comment.