Skip to content

Commit

Permalink
Merge pull request #47 from babarot/babarot/fix
Browse files Browse the repository at this point in the history
Add detail error handling and logs
  • Loading branch information
babarot authored Jan 27, 2025
2 parents 4aa6cb3 + abc8ee7 commit 710bf2a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
13 changes: 13 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ 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 All @@ -147,7 +150,17 @@ func (c CLI) Restore() error {
slog.Debug("cil.restore started")
defer slog.Debug("cil.restore finished")

if len(c.history.Files) == 0 {
fmt.Printf("The history is empty. Let's try deleting a file first\n")
return nil
}

files := c.history.Filter()
if len(files) == 0 {
fmt.Printf("Could not find any files to display. The history may be empty, or the history.exclude conditions may be too strict\n")
return nil
}

files, err := ui.RenderList(files, c.config.UI)
if err != nil {
return err
Expand Down
26 changes: 17 additions & 9 deletions internal/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ func New(c config.History) History {
func (h *History) Open() error {
slog.Debug("opening history file", "path", h.path)

parentDir := filepath.Dir(h.path)
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
slog.Warn("mkdir", "dir", parentDir)
_ = os.Mkdir(parentDir, 0755)
}

if _, err := os.Stat(h.path); os.IsNotExist(err) {
backupFile := h.path + ".backup"
slog.Warn("history file not found", "path", h.path)
slog.Warn("create a new history file if this is the initial run or restoring from backup")
if _, err := os.Stat(backupFile); !os.IsNotExist(err) {
slog.Warn("backup file found! attempting to restore from backup", "path", backupFile)
err := os.Rename(backupFile, h.path)
Expand All @@ -69,19 +74,28 @@ func (h *History) Open() error {
}
}

f, err := os.Open(h.path)
f, err := os.OpenFile(h.path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
slog.Error("err", "error", err)
return err
}
defer f.Close()

if stat, err := f.Stat(); err == nil && stat.Size() == 0 {
slog.Warn("history is empty")
return nil
}

if err := json.NewDecoder(f).Decode(&h); err != nil {
slog.Error("err", "error", err)
return err
}

slog.Debug("history version", "version", h.Version)
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)
Expand All @@ -95,9 +109,6 @@ func (h *History) backup() error {

func (h *History) update(files []File) error {
slog.Debug("updating history file", "path", h.path)
defer func() {
_ = h.backup()
}()
f, err := os.Create(h.path)
if err != nil {
return err
Expand All @@ -110,9 +121,6 @@ func (h *History) update(files []File) error {

func (h *History) Save(files []File) error {
slog.Debug("saving history file", "path", h.path)
defer func() {
_ = h.backup()
}()
f, err := os.Create(h.path)
if err != nil {
return err
Expand Down
8 changes: 3 additions & 5 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type inventoryLoadedMsg struct {

func (m Model) loadInventory() tea.Msg {
files := m.files
slog.Debug("loadInventory starts")
slog.Debug("loadInventory starts", "len(files)", len(files))
if len(files) == 0 {
return errorMsg{errors.New("no deleted files found")}
}
Expand Down Expand Up @@ -310,8 +310,8 @@ func (m Model) View() string {
s := ""

if m.err != nil {
s += fmt.Sprintf("error happen %s", m.err)
return s
slog.Error("rendering of the view has stopped", "error", m.err)
return m.err.Error()
}

switch m.viewType {
Expand Down Expand Up @@ -388,8 +388,6 @@ func RenderList(filteredFiles []history.File, cfg config.UI) ([]history.File, er
l.DisableQuitKeybindings()
l.SetShowStatusBar(false)
l.SetShowTitle(false)
// l.AdditionalShortHelpKeys = keys.ListKeys.ShortHelp
// l.AdditionalFullHelpKeys = keys.ListKeys.FullHelp

m := Model{
listKeys: keys.ListKeys,
Expand Down

0 comments on commit 710bf2a

Please sign in to comment.