Rich Enhanced Shell History - Contextual shell history for zsh and bash
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
resh/internal/recio/write.go

46 lines
893 B

package recio
import (
"encoding/json"
"fmt"
"os"
"github.com/curusarn/resh/internal/record"
"github.com/curusarn/resh/internal/recordint"
)
// TODO: better errors
func (r *RecIO) WriteFile(fpath string, data []record.V1) error {
file, err := os.Create(fpath)
if err != nil {
return err
}
defer file.Close()
for _, rec := range data {
jsn, err := encodeV1Record(rec)
if err != nil {
return err
}
_, err = file.Write(jsn)
if err != nil {
return err
}
}
return nil
}
func (r *RecIO) EditRecordFlagsInFile(fpath string, idx int, rec recordint.Flag) error {
// FIXME: implement
// open file "not as append"
// scan to the correct line
return nil
}
func encodeV1Record(rec record.V1) ([]byte, error) {
jsn, err := json.Marshal(rec)
if err != nil {
return nil, fmt.Errorf("failed to encode json: %w", err)
}
return append(jsn, []byte("\n")...), nil
}