better errors

pull/184/head
Simon Let 3 years ago
parent 4fbbec7e05
commit 5e0af13e62
  1. 17
      internal/recio/read.go
  2. 32
      internal/recio/write.go
  3. 12
      record/v1.go

@ -15,7 +15,7 @@ import (
) )
func (r *RecIO) ReadAndFixFile(fpath string, maxErrors int) ([]record.V1, error) { func (r *RecIO) ReadAndFixFile(fpath string, maxErrors int) ([]record.V1, error) {
recs, err, decodeErrs := r.ReadFile(fpath) recs, decodeErrs, err := r.ReadFile(fpath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -66,16 +66,16 @@ func (r *RecIO) ReadAndFixFile(fpath string, maxErrors int) ([]record.V1, error)
return recs, nil return recs, nil
} }
func (r *RecIO) ReadFile(fpath string) ([]record.V1, error, []error) { func (r *RecIO) ReadFile(fpath string) ([]record.V1, []error, error) {
var recs []record.V1 var recs []record.V1
file, err := os.Open(fpath) file, err := os.Open(fpath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to open history file: %w", err), nil return nil, nil, fmt.Errorf("failed to open history file: %w", err)
} }
defer file.Close() defer file.Close()
reader := bufio.NewReader(file) reader := bufio.NewReader(file)
decodeErrs := []error{} var decodeErrs []error
for { for {
var line string var line string
line, err = reader.ReadString('\n') line, err = reader.ReadString('\n')
@ -93,13 +93,14 @@ func (r *RecIO) ReadFile(fpath string) ([]record.V1, error, []error) {
} }
recs = append(recs, *rec) recs = append(recs, *rec)
} }
if err != io.EOF {
r.sugar.Error("Error while loading file", zap.Error(err))
}
r.sugar.Infow("Loaded resh history records", r.sugar.Infow("Loaded resh history records",
"recordCount", len(recs), "recordCount", len(recs),
) )
return recs, nil, decodeErrs if err != io.EOF {
r.sugar.Error("Error while reading file", zap.Error(err))
return recs, decodeErrs, err
}
return recs, decodeErrs, nil
} }
func (r *RecIO) decodeLine(line string) (*record.V1, error) { func (r *RecIO) decodeLine(line string) (*record.V1, error) {

@ -8,35 +8,47 @@ import (
"github.com/curusarn/resh/record" "github.com/curusarn/resh/record"
) )
// TODO: better errors
func (r *RecIO) OverwriteFile(fpath string, recs []record.V1) error { func (r *RecIO) OverwriteFile(fpath string, recs []record.V1) error {
file, err := os.Create(fpath) file, err := os.Create(fpath)
if err != nil { if err != nil {
return err return fmt.Errorf("could not create/truncate file: %w", err)
} }
defer file.Close() err = writeRecords(file, recs)
return writeRecords(file, recs) if err != nil {
return fmt.Errorf("error while writing records: %w", err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("could not close file: %w", err)
}
return nil
} }
// TODO: better errors
func (r *RecIO) AppendToFile(fpath string, recs []record.V1) error { func (r *RecIO) AppendToFile(fpath string, recs []record.V1) error {
file, err := os.OpenFile(fpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) file, err := os.OpenFile(fpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
return err return fmt.Errorf("could not open/create file: %w", err)
} }
defer file.Close() err = writeRecords(file, recs)
return writeRecords(file, recs) if err != nil {
return fmt.Errorf("error while writing records: %w", err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("could not close file: %w", err)
}
return nil
} }
func writeRecords(file *os.File, recs []record.V1) error { func writeRecords(file *os.File, recs []record.V1) error {
for _, rec := range recs { for _, rec := range recs {
jsn, err := encodeV1Record(rec) jsn, err := encodeV1Record(rec)
if err != nil { if err != nil {
return err return fmt.Errorf("could not encode record: %w", err)
} }
_, err = file.Write(jsn) _, err = file.Write(jsn)
if err != nil { if err != nil {
return err return fmt.Errorf("could not write json: %w", err)
} }
} }
return nil return nil

@ -1,10 +1,10 @@
package record package record
type V1 struct { type V1 struct {
// flags // future-proofing so that we can add this later without version bump
// deleted, favorite // deleted, favorite
// FIXME: is this the best way? .. what about string, separate fields, or something similar Deleted bool `json:"deleted,omitempty"`
Flags int `json:"flags"` Favorite bool `json:"favorite,omitempty"`
// cmdline, exitcode // cmdline, exitcode
CmdLine string `json:"cmdLine"` CmdLine string `json:"cmdLine"`
@ -21,10 +21,8 @@ type V1 struct {
Pwd string `json:"pwd"` Pwd string `json:"pwd"`
RealPwd string `json:"realPwd"` RealPwd string `json:"realPwd"`
// hostname + logname (not sure if we actually need logname) // Device is set during installation/setup
// Logname string `json:"logname"` // It is stored in RESH configuration
// Device is usually hostname but not stricly hostname
// It can be configured in RESH configuration
Device string `json:"device"` Device string `json:"device"`
// git info // git info

Loading…
Cancel
Save