From c4454028242e384927d74bf32ae6230be52f8119 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 28 Jul 2024 23:10:29 +0300 Subject: refactor --- internal/client/tui/finishedmsg.go | 11 +++++++++++ internal/client/tui/submit.go | 17 +++++++---------- internal/types/entry.go | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) (limited to 'internal') diff --git a/internal/client/tui/finishedmsg.go b/internal/client/tui/finishedmsg.go index 9609b93..6f702f3 100644 --- a/internal/client/tui/finishedmsg.go +++ b/internal/client/tui/finishedmsg.go @@ -1,5 +1,7 @@ package tui +import tea "github.com/charmbracelet/bubbletea" + type finishedMsg struct { callback func() error err error @@ -8,3 +10,12 @@ type finishedMsg struct { func (f finishedMsg) Error() string { return f.err.Error() } + +func finished(callback func() error, err error) tea.Cmd { + return func() tea.Msg { + return finishedMsg{ + callback: callback, + err: err, + } + } +} diff --git a/internal/client/tui/submit.go b/internal/client/tui/submit.go index 49e1007..3d908a0 100644 --- a/internal/client/tui/submit.go +++ b/internal/client/tui/submit.go @@ -26,17 +26,14 @@ func submitAction(ctx context.Context, conf config.ClientConfig) tea.Cmd { func submitEntry(ctx context.Context, conf client.ClientConfig, filePath string, callback func() error) tea.Cmd { servers, err := conf.Servers() - if err == nil { - var entry types.Entry - if entry, err = types.NewEntryFromFile(filePath); err == nil { - err = easyhttp.PostData(ctx, "submit", conf.APIKey, &entry, servers...) - } + if err != nil { + return finished(callback, err) } - return func() tea.Msg { - return finishedMsg{ - callback: callback, - err: err, - } + ent, err := types.NewEntryFromTextFile(filePath) + if err != nil { + return finished(callback, err) } + + return finished(callback, easyhttp.PostData(ctx, "submit", conf.APIKey, &ent, servers...)) } diff --git a/internal/types/entry.go b/internal/types/entry.go index 21a12ee..fd1e12a 100644 --- a/internal/types/entry.go +++ b/internal/types/entry.go @@ -65,7 +65,7 @@ func NewEntryFromCopy(other Entry) (Entry, error) { return e.Update(other) } -func NewEntryFromFile(filePath string) (Entry, error) { +func NewEntryFromTextFile(filePath string) (Entry, error) { var e Entry data, err := os.ReadFile(filePath) if err != nil { -- cgit v1.2.3