From 7fc95c18f169f0bfe0a5bc8a239ec5853619ec59 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 28 Jul 2024 18:46:41 +0300 Subject: initial posting real data over --- internal/client/tui/submit.go | 9 ++++++--- internal/types/entry.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/internal/client/tui/submit.go b/internal/client/tui/submit.go index 90143d6..7e1f15c 100644 --- a/internal/client/tui/submit.go +++ b/internal/client/tui/submit.go @@ -14,17 +14,20 @@ import ( func submitAction(ctx context.Context, conf config.ClientConfig) tea.Cmd { composeFile := fmt.Sprintf("%s/%s", conf.DataDir, conf.ComposeFile) - return submitMessage(ctx, conf, composeFile, func() error { + return submitEntry(ctx, conf, composeFile, func() error { // This is the callback to call return nil }) } -func submitMessage(ctx context.Context, conf client.ClientConfig, filePath string, callback func() error) 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 - err = easyhttp.PostData(ctx, "submit", conf.APIKey, &entry, servers...) + entry, err = types.NewEntryFromFile(filePath) + if err == nil { + err = easyhttp.PostData(ctx, "submit", conf.APIKey, &entry, servers...) + } } return func() tea.Msg { diff --git a/internal/types/entry.go b/internal/types/entry.go index ca2a742..695c786 100644 --- a/internal/types/entry.go +++ b/internal/types/entry.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "os" "strings" "sync" ) @@ -63,6 +64,21 @@ func NewEntryFromCopy(other Entry) (Entry, error) { return e.Update(other) } +func NewEntryFromFile(filePath string) (Entry, error) { + var e Entry + data, err := os.ReadFile(filePath) + if err != nil { + return e, err + } + e.Body = string(data) + if e.ID == "" { + e.ID = fmt.Sprintf("%x", sha256.Sum256([]byte(e.Body))) + } + e.initialize() + e.Checksum() + return e, nil +} + func (e *Entry) initialize() { e.mu = &sync.Mutex{} e.checksumDirty = true -- cgit v1.2.3