Skip to content

Commit

Permalink
Merge pull request #49 from babarot/babarot/history
Browse files Browse the repository at this point in the history
Refactor history package
  • Loading branch information
babarot authored Jan 27, 2025
2 parents 495a2a3 + 00247fe commit 9e85cc8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ github:
repo: gomi
release:
name: gomi
tag: v1.2.0
tag: v1.2.3
command:
link:
- from: gomi
Expand Down
5 changes: 1 addition & 4 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ func (c CLI) Run(args []string) error {
if err := c.history.Open(); err != nil {
return err
}
defer func() {
_ = c.history.Backup()
}()

switch {
case c.option.Version:
Expand Down Expand Up @@ -290,7 +287,7 @@ func (c CLI) Put(args []string) error {

// Save the files after all tasks are done
defer func() {
err := c.history.Save(files)
err := c.history.Update(files)
if err != nil {
slog.Error("failed to update history", "error", err)
}
Expand Down
71 changes: 46 additions & 25 deletions internal/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func New(c config.History) History {

func (h *History) Open() error {
slog.Debug("opening history file", "path", h.path)
defer func() {
_ = h.backup()
slog.Debug("backed up")
}()

parentDir := filepath.Dir(h.path)
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
Expand Down Expand Up @@ -95,42 +99,77 @@ func (h *History) Open() error {
return nil
}

func (h *History) Backup() error {
func (h *History) backup() error {
backupFile := h.path + ".backup"
slog.Debug("backing up history", "path", backupFile)
f, err := os.Create(backupFile)
if err != nil {
return err
}
defer f.Close()
h.setVersion()
return json.NewEncoder(f).Encode(&h)
}

func (h *History) update(files []File) error {
// Update updates the history by appending the given files to the existing ones.
// It overwrites the current history file with the updated data and sets the version before saving.
// A backup of the current state is created before the update is applied.
func (h *History) Update(files []File) error {
slog.Debug("updating history file", "path", h.path)
defer func() {
_ = h.backup()
slog.Debug("backed up")
}()
f, err := os.Create(h.path)
if err != nil {
return err
}
defer f.Close()
h.Files = files
h.Files = append(h.Files, files...)
h.setVersion()
return json.NewEncoder(f).Encode(&h)
}

func (h *History) Save(files []File) error {
// Save saves the current history to the file, overwriting the existing data.
// A backup is performed before saving the history to ensure the current state is preserved.
// Unlike 'update', it does not modify the list of files or set the version.
func (h *History) Save() error {
slog.Debug("saving history file", "path", h.path)
defer func() {
_ = h.backup()
slog.Debug("backed up")
}()
f, err := os.Create(h.path)
if err != nil {
return err
}
defer f.Close()
h.Files = append(h.Files, files...)
h.setVersion()
return json.NewEncoder(f).Encode(&h)
}

func (h *History) Remove(target File) error {
defer func() {
_ = h.backup()
slog.Debug("backed up")
}()
slog.Debug("deleting file from history file", "path", h.path, "file", target)
var files []File
for _, file := range h.Files {
if file.ID == target.ID {
slog.Debug("target file found", "id", file.ID, "name", file.Name)
continue
}
files = append(files, file)
}
h.Files = files
return h.Save()
}

func (h *History) setVersion() {
if h.Version == 0 {
h.Version = historyVersion
}
}

func (h History) Filter() []File {
// do not overwrite original slices
// because remove them from history file actually
Expand Down Expand Up @@ -193,24 +232,6 @@ func (h History) Filter() []File {
return files
}

func (h *History) Remove(target File) error {
slog.Debug("deleting file from history file", "path", h.path, "file", target)
var files []File
for _, file := range h.Files {
if file.ID == target.ID {
continue
}
files = append(files, file)
}
return h.update(files)
}

func (h *History) setVersion() {
if h.Version == 0 {
h.Version = historyVersion
}
}

func FileInfo(runID string, arg string) (File, error) {
name := filepath.Base(arg)
from, err := filepath.Abs(arg)
Expand Down

0 comments on commit 9e85cc8

Please sign in to comment.