summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-07-28 23:10:29 +0300
committerPaul Buetow <paul@buetow.org>2024-07-28 23:10:29 +0300
commitc4454028242e384927d74bf32ae6230be52f8119 (patch)
tree34462e7c3d8aa5c6cd59d369a2cd83477dc068bc /internal
parent8072daab7a5221842cf823d9b314deb49b9ba6ff (diff)
refactor
Diffstat (limited to 'internal')
-rw-r--r--internal/client/tui/finishedmsg.go11
-rw-r--r--internal/client/tui/submit.go17
-rw-r--r--internal/types/entry.go2
3 files changed, 19 insertions, 11 deletions
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 {