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) {
recs, err, decodeErrs := r.ReadFile(fpath)
recs, decodeErrs, err := r.ReadFile(fpath)
if err != nil {
return nil, err
}
@ -66,16 +66,16 @@ func (r *RecIO) ReadAndFixFile(fpath string, maxErrors int) ([]record.V1, error)
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
file, err := os.Open(fpath)
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()
reader := bufio.NewReader(file)
decodeErrs := []error{}
var decodeErrs []error
for {
var line string
line, err = reader.ReadString('\n')
@ -93,13 +93,14 @@ func (r *RecIO) ReadFile(fpath string) ([]record.V1, error, []error) {
}
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",
"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) {

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

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

Loading…
Cancel
Save