mirror of
https://codeberg.org/scip/epuppy.git
synced 2025-12-17 04:20:59 +01:00
Add dump feature, enhance code quality (#1)
This commit is contained in:
25
cmd/store.go
25
cmd/store.go
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user