Add dump feature, enhance code quality (#1)

This commit is contained in:
T.v.Dein
2025-10-16 12:24:47 +02:00
committed by GitHub
parent d9ed4dee10
commit 6099806e22
10 changed files with 125 additions and 423 deletions

View File

@@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
@@ -21,20 +22,28 @@ func StoreProgress(conf *Config, progress int) error {
cfgpath := conf.GetConfigDir()
if err := Mkdir(cfgpath); err != nil {
return err
return fmt.Errorf("failed to mkdir config path %s: %w", cfgpath, err)
}
filename := filepath.Join(cfgpath, Slug(conf.Document))
fd, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
return fmt.Errorf("failed to open state file %s: %w", filename, err)
}
defer fd.Close()
defer func() {
if err := fd.Close(); err != nil {
log.Fatal(err)
}
}()
_, err = fd.WriteString(fmt.Sprintf("%d\n", progress))
if err := fd.Truncate(0); err != nil {
return fmt.Errorf("failed to truncate state file %s: %w", filename, err)
}
_, err = fmt.Fprintf(fd, "%d\n", progress)
if err != nil {
return err
return fmt.Errorf("failed to write to state file %s: %w", filename, err)
}
return nil
@@ -49,7 +58,11 @@ func GetProgress(conf *Config) (int64, error) {
if err != nil {
return 0, nil // ignore errors and return no progress
}
defer fd.Close()
defer func() {
if err := fd.Close(); err != nil {
log.Fatal(err)
}
}()
scanner := bufio.NewScanner(fd)
var line string