Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func deleteRecordCommand(t *core.Timetrace) *cobra.Command {
}
}

if err := t.BackupRecord(start); err != nil {
if err := t.BackupRecord(*record); err != nil {
out.Err("Failed to backup record before deletion: %s", err.Error())
return
}
Expand Down
15 changes: 13 additions & 2 deletions cli/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"errors"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -84,9 +85,10 @@ func editRecordCommand(t *core.Timetrace) *cobra.Command {

var recordTime time.Time
var err error
var rec *core.Record
// if more aliases are needed, this should be expanded to a switch
if strings.ToLower(args[0]) == "latest" {
rec, err := t.LoadLatestRecord()
rec, err = t.LoadLatestRecord()
if err != nil {
out.Err("Error on loading last record: %s", err.Error())
return
Expand All @@ -98,6 +100,10 @@ func editRecordCommand(t *core.Timetrace) *cobra.Command {
out.Err("Failed to parse date argument: %s", err.Error())
return
}
rec, err = t.LoadRecord(recordTime)
if err != nil {
return
}
}

if options.Revert {
Expand All @@ -109,12 +115,17 @@ func editRecordCommand(t *core.Timetrace) *cobra.Command {
return
}

if err := t.BackupRecord(recordTime); err != nil {
if err := t.BackupRecord(*rec); err != nil {
out.Err("Failed to backup record before edit: %s", err.Error())
return
}

if options.Minus == "" && options.Plus == "" {
fmt.Printf("Warning: Directly editing the record can lead to undefined behavior.\nIf you change the start time of the record, the underlying file will be renamed automatically. Make sure it doesn't collide with other records. If you want to change the end time, use --minus or --plus instead.\nContinue? ")
if !askForConfirmation() {
out.Info("Editting record aborted.")
return
}
out.Info("Opening %s in default editor", recordTime)
if err := t.EditRecordManual(recordTime); err != nil {
out.Err("Failed to edit record: %s", err.Error())
Expand Down
51 changes: 40 additions & 11 deletions core/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,9 @@ func (t *Timetrace) SaveRecord(record Record, force bool) error {
}

// BackupRecord creates a backup of the given record file
func (t *Timetrace) BackupRecord(recordKey time.Time) error {
path := t.fs.RecordFilepath(recordKey)
record, err := t.loadRecord(path)
if err != nil {
return err
}
func (t *Timetrace) BackupRecord(record Record) error {
// create a new .bak filepath from the record struct
backupPath := t.fs.RecordBackupFilepath(recordKey)
backupPath := t.fs.RecordBackupFilepath(record.Start)

backupFile, err := os.OpenFile(backupPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
Expand All @@ -123,9 +118,19 @@ func (t *Timetrace) RevertRecord(recordKey time.Time) error {
return err
}

path := t.fs.RecordFilepath(recordKey)
oldPath := t.fs.RecordFilepath(recordKey)
newPath := t.fs.RecordFilepath(record.Start)
if err = os.Rename(oldPath, newPath); err != nil {
return err
}

oldBackupPath := t.fs.RecordBackupFilepath(recordKey)
newBackupPath := t.fs.RecordBackupFilepath(record.Start)
if err = os.Rename(oldBackupPath, newBackupPath); err != nil {
return err
}

file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
file, err := os.OpenFile(newPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
Expand Down Expand Up @@ -155,18 +160,42 @@ func (t *Timetrace) DeleteRecord(record Record) error {
// EditRecordManual opens the record file in the preferred or default editor.
func (t *Timetrace) EditRecordManual(recordTime time.Time) error {
path := t.fs.RecordFilepath(recordTime)
backupPath := t.fs.RecordBackupFilepath(recordTime)

if _, err := t.loadRecord(path); err != nil {
rec, err := t.loadRecord(path)
if err != nil {
return err
}
originalStart := rec.Start

editor := t.editorFromEnvironment()
cmd := exec.Command(editor, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
if err = cmd.Run(); err != nil {
return err
}

edittedRec, err := t.loadRecord(path)
if err != nil {
return err
}

newStart := edittedRec.Start
if originalStart == newStart {
return nil
}

newPath := t.fs.RecordFilepath(newStart)
newBackupPath := t.fs.RecordBackupFilepath(newStart)

if err = os.Rename(path, newPath); err != nil {
return err
}

return os.Rename(backupPath, newBackupPath)
}

// EditRecord loads the record internally, applies the option values and saves the record
Expand Down