Skip to content
Merged
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
13 changes: 7 additions & 6 deletions cmd/backups/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backups

import (
"fmt"
"os"

"github.com/DylanDevelops/tmpo/internal/storage"
"github.com/DylanDevelops/tmpo/internal/ui"
Expand All @@ -14,34 +13,34 @@ func CreateCmd() *cobra.Command {
Use: "create",
Short: "Create a new backup",
Long: `Create a new backup of your entire database to save all your data to be restored from later.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ui.NewlineAbove()

db, err := storage.Initialize()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("%v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}
defer db.Close()

running, err := db.GetRunningEntry()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("checking for active timer: %v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}
if running != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf(`timer is running for %s — stop it before creating a backup`, ui.Bold(running.ProjectName)))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}

backup, err := db.CreateBackup()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("creating backup: %v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}

ui.PrintSuccess(ui.EmojiBackup, "Backup created successfully")
Expand All @@ -50,6 +49,8 @@ func CreateCmd() *cobra.Command {
ui.PrintInfo(2, "Path", backup.Path)
ui.PrintInfo(2, "Size", ui.FormatFileSize(backup.Size))
ui.NewlineBelow()

return nil
},
}

Expand Down
18 changes: 10 additions & 8 deletions cmd/backups/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ func DeleteCmd() *cobra.Command {
Use: "delete",
Short: "Delete a backup",
Long: `Permanently delete an existing backup. This action cannot be undone.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ui.NewlineAbove()

backups, err := storage.ListBackups()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("listing backups: %v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}

if len(backups) == 0 {
ui.PrintInfo(0, ui.EmojiInfo+" No backups found", "")
ui.PrintMuted(2, "Run 'tmpo backup create' to create one.")
ui.NewlineBelow()
return
return nil
}

var selected *storage.BackupInfo
Expand All @@ -51,7 +51,7 @@ func DeleteCmd() *cobra.Command {
if selected == nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("no backup found with ID %d", id))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}
} else {
for i := range backups {
Expand All @@ -63,7 +63,7 @@ func DeleteCmd() *cobra.Command {
if selected == nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("no backup found with filename %q", deleteIDFlag))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}
}
} else {
Expand All @@ -89,7 +89,7 @@ func DeleteCmd() *cobra.Command {
idx, _, err := prompt.Run()
if err != nil {
ui.NewlineBelow()
return
return nil
}

selected = &backups[idx]
Expand All @@ -103,17 +103,19 @@ func DeleteCmd() *cobra.Command {
if _, err := confirmPrompt.Run(); err != nil {
ui.PrintInfo(0, ui.EmojiInfo+" Deletion cancelled", "")
ui.NewlineBelow()
return
return nil
}

if err := os.Remove(selected.Path); err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("deleting backup: %v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}

ui.PrintSuccess(ui.EmojiSuccess, fmt.Sprintf("Deleted %s", selected.Filename))
ui.NewlineBelow()

return nil
},
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/backups/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backups

import (
"fmt"
"os"

"github.com/DylanDevelops/tmpo/internal/settings"
"github.com/DylanDevelops/tmpo/internal/storage"
Expand All @@ -15,21 +14,21 @@ func ListCmd() *cobra.Command {
Use: "list",
Short: "Lists all existing backups",
Long: `Lists all existing backups which can be used to restore.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ui.NewlineAbove()

backups, err := storage.ListBackups()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("listing backups: %v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}

if len(backups) == 0 {
ui.PrintInfo(0, ui.EmojiInfo+" No backups found", "")
ui.PrintMuted(2, "Run 'tmpo backup create' to create one.")
ui.NewlineBelow()
return
return nil
}

fmt.Printf(" %s%-4s %-28s %-8s %s%s\n",
Expand Down Expand Up @@ -59,6 +58,8 @@ func ListCmd() *cobra.Command {
}

ui.NewlineBelow()

return nil
},
}

Expand Down
19 changes: 10 additions & 9 deletions cmd/backups/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backups

import (
"fmt"
"os"
"strconv"

"github.com/DylanDevelops/tmpo/internal/settings"
Expand All @@ -21,21 +20,21 @@ func RestoreCmd() *cobra.Command {
Use: "restore",
Short: "Restore from a backup",
Long: `Restore your database from an existing backup.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ui.NewlineAbove()

backups, err := storage.ListBackups()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("listing backups: %v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}

if len(backups) == 0 {
ui.PrintInfo(0, ui.EmojiInfo+" No backups found", "")
ui.PrintMuted(2, "Run 'tmpo backup create' to create one.")
ui.NewlineBelow()
return
return nil
}

var selected *storage.BackupInfo
Expand All @@ -51,7 +50,7 @@ func RestoreCmd() *cobra.Command {
if selected == nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("no backup found with ID %d", id))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}
} else {
for i := range backups {
Expand All @@ -63,7 +62,7 @@ func RestoreCmd() *cobra.Command {
if selected == nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("no backup found with filename %q", restoreIDFlag))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}
}
} else {
Expand All @@ -89,7 +88,7 @@ func RestoreCmd() *cobra.Command {
idx, _, err := prompt.Run()
if err != nil {
ui.NewlineBelow()
return
return nil
}

selected = &backups[idx]
Expand All @@ -110,17 +109,19 @@ func RestoreCmd() *cobra.Command {
if _, err := confirmPrompt.Run(); err != nil {
ui.PrintInfo(0, ui.EmojiInfo+" Restore cancelled", "")
ui.NewlineBelow()
return
return nil
}

if err := storage.RestoreBackup(selected.Path); err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("restoring backup: %v", err))
ui.NewlineBelow()
os.Exit(1)
return ui.ErrHandled
}

ui.PrintSuccess(ui.EmojiBackup, fmt.Sprintf("Restored from %s", selected.Filename))
ui.NewlineBelow()

return nil
},
}

Expand Down
19 changes: 10 additions & 9 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"os"
"strings"

"github.com/DylanDevelops/tmpo/internal/settings"
Expand All @@ -17,14 +16,14 @@ func ConfigCmd() *cobra.Command {
Aliases: []string{"settings", "preferences"},
Short: "Configure global tmpo settings",
Long: `Set up global configuration for tmpo including currency, date/time format, and timezone.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
ui.NewlineAbove()

// Load current global config
currentConfig, err := settings.LoadGlobalConfig()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("Failed to load config: %v", err))
os.Exit(1)
return err
}

// Display header
Expand Down Expand Up @@ -69,7 +68,7 @@ func ConfigCmd() *cobra.Command {
currencyInput, err := currencyPrompt.Run()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("%v", err))
os.Exit(1)
return err
}

currencyCode := strings.ToUpper(strings.TrimSpace(currencyInput))
Expand All @@ -88,7 +87,7 @@ func ConfigCmd() *cobra.Command {
_, dateFormat, err := dateFormatSelect.Run()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("%v", err))
os.Exit(1)
return err
}

// Keep current format if selected
Expand All @@ -107,7 +106,7 @@ func ConfigCmd() *cobra.Command {
_, timeFormat, err := timeFormatSelect.Run()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("%v", err))
os.Exit(1)
return err
}

// Keep current format if selected
Expand All @@ -127,7 +126,7 @@ func ConfigCmd() *cobra.Command {
timezoneInput, err := timezonePrompt.Run()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("%v", err))
os.Exit(1)
return err
}

timezone := strings.TrimSpace(timezoneInput)
Expand All @@ -146,7 +145,7 @@ func ConfigCmd() *cobra.Command {
exportPathInput, err := exportPathPrompt.Run()
if err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("%v", err))
os.Exit(1)
return err
}

exportPath := strings.TrimSpace(exportPathInput)
Expand All @@ -169,7 +168,7 @@ func ConfigCmd() *cobra.Command {
// Save the config
if err := newConfig.Save(); err != nil {
ui.PrintError(ui.EmojiError, fmt.Sprintf("Failed to save config: %v", err))
os.Exit(1)
return err
}

// Display success message
Expand Down Expand Up @@ -197,6 +196,8 @@ func ConfigCmd() *cobra.Command {
ui.PrintInfo(4, ui.Bold("Export path"), exportPathDisplay)

ui.NewlineBelow()

return nil
},
}

Expand Down
Loading