summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-08-03 14:09:21 +0300
committerPaul Buetow <paul@buetow.org>2024-08-03 14:09:21 +0300
commit5664c4ad14fca54a42356541847e6ae3f9613a87 (patch)
tree297757ae2f4038dd649513c40e28f80f755c8690
parenta10d20b6ce8e462aa1b5c5ffb3ce17111e4faf64 (diff)
can compose and submit in one go
-rw-r--r--internal/client/tui/compose.go29
-rw-r--r--internal/client/tui/finishedmsg.go8
-rw-r--r--internal/client/tui/submit.go29
-rw-r--r--internal/client/tui/tui.go10
-rw-r--r--internal/easyhttp/easyhttp.go2
5 files changed, 55 insertions, 23 deletions
diff --git a/internal/client/tui/compose.go b/internal/client/tui/compose.go
index d918677..c2d5c16 100644
--- a/internal/client/tui/compose.go
+++ b/internal/client/tui/compose.go
@@ -1,7 +1,9 @@
package tui
import (
+ "context"
"fmt"
+ "log"
"os"
"os/exec"
"time"
@@ -10,21 +12,34 @@ import (
tea "github.com/charmbracelet/bubbletea"
)
-func composeAction(conf config.ClientConfig, queue bool) tea.Cmd {
+type composePostAction int
+
+const (
+ noPostAction composePostAction = iota
+ queueAfterCompose
+ submitAfterCompose
+)
+
+func composeAction(ctx context.Context, conf config.ClientConfig, postAction composePostAction) tea.Cmd {
err := ensureDirectoryExists(conf.DataDir)
composeFile := fmt.Sprintf("%s/%s", conf.DataDir, conf.ComposeFile)
+ log.Println("Composing", composeFile)
return openEditor(conf.Editor, composeFile, func() error {
if err != nil {
return err
}
- // ye
- if !queue {
- return nil
+
+ switch postAction {
+ case submitAfterCompose:
+ return submitEntryNoCmd(ctx, conf, composeFile)
+ case queueAfterCompose:
+ timestamp := time.Now().Format("20060102-150405")
+ queuedFile := fmt.Sprintf("%s/queued-%s.txt", conf.DataDir, timestamp)
+ return os.Rename(composeFile, queuedFile)
}
- timestamp := time.Now().Format("20060102-150405")
- queuedFile := fmt.Sprintf("%s/queued-%s.txt", conf.DataDir, timestamp)
- return os.Rename(composeFile, queuedFile)
+
+ return nil
})
}
diff --git a/internal/client/tui/finishedmsg.go b/internal/client/tui/finishedmsg.go
index c475ce7..dca703f 100644
--- a/internal/client/tui/finishedmsg.go
+++ b/internal/client/tui/finishedmsg.go
@@ -3,8 +3,8 @@ package tui
import tea "github.com/charmbracelet/bubbletea"
type finishedMsg struct {
- cb func() error
- err error
+ cb func() error
+ err error
}
func (f finishedMsg) Error() string {
@@ -14,8 +14,8 @@ func (f finishedMsg) Error() string {
func finished(cb func() error, err error) tea.Cmd {
return func() tea.Msg {
return finishedMsg{
- cb: cb,
- err: err,
+ cb: cb,
+ err: err,
}
}
}
diff --git a/internal/client/tui/submit.go b/internal/client/tui/submit.go
index 574fb54..3ab4ab5 100644
--- a/internal/client/tui/submit.go
+++ b/internal/client/tui/submit.go
@@ -3,6 +3,7 @@ package tui
import (
"context"
"fmt"
+ "log"
"os"
"time"
@@ -15,25 +16,37 @@ import (
func submitAction(ctx context.Context, conf config.ClientConfig) tea.Cmd {
composeFile := fmt.Sprintf("%s/%s", conf.DataDir, conf.ComposeFile)
+ log.Println("Submitting", composeFile)
return submitEntry(ctx, conf, composeFile, func() error {
// This is the cb to call when the entry was submitted succesfully
- timestamp := time.Now().Format("20060102-150405")
- submittedFile := fmt.Sprintf("%s/submitted-%s.txt", conf.DataDir, timestamp)
- return os.Rename(composeFile, submittedFile)
+ return nil
})
}
-func submitEntry(ctx context.Context, conf client.ClientConfig, filePath string, cb func() error) tea.Cmd {
+func submitEntry(ctx context.Context, conf client.ClientConfig, composeFile string, cb func() error) tea.Cmd {
+ return finished(cb, submitEntryNoCmd(ctx, conf, composeFile))
+}
+
+// TODO: Rename all functions returning a Cmd so that they have a Cmd suffix
+func submitEntryNoCmd(ctx context.Context, conf client.ClientConfig, composeFile string) error {
servers, err := conf.Servers()
if err != nil {
- return finished(cb, err)
+ return err
}
- ent, err := types.NewEntryFromTextFile(filePath)
+ ent, err := types.NewEntryFromTextFile(composeFile)
if err != nil {
- return finished(cb, err)
+ return err
}
- return finished(cb, easyhttp.PostData(ctx, "submit", conf.APIKey, &ent, servers...))
+ if err := easyhttp.PostData(ctx, "submit", conf.APIKey, &ent, servers...); err != nil {
+ return err
+ }
+
+ timestamp := time.Now().Format("20060102-150405")
+ submittedFile := fmt.Sprintf("%s/submitted-%s.txt", conf.DataDir, timestamp)
+
+ log.Println("Renaming", composeFile, "to", submittedFile)
+ return os.Rename(composeFile, submittedFile)
}
diff --git a/internal/client/tui/tui.go b/internal/client/tui/tui.go
index b06f585..2ca2c10 100644
--- a/internal/client/tui/tui.go
+++ b/internal/client/tui/tui.go
@@ -3,6 +3,7 @@ package tui
import (
"context"
"fmt"
+ "log"
config "codeberg.org/snonux/gos/internal/config/client"
tea "github.com/charmbracelet/bubbletea"
@@ -75,18 +76,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
switch m.cursor {
case cursorCompose:
- return m, composeAction(m.conf, false)
+ return m, composeAction(m.ctx, m.conf, noPostAction)
case cursorSubmit:
return m, submitAction(m.ctx, m.conf)
case cursorComposeAndSubmit:
- return m, tea.Sequence(composeAction(m.conf, false), submitAction(m.ctx, m.conf))
+ return m, composeAction(m.ctx, m.conf, submitAfterCompose)
}
case "1":
- return m, composeAction(m.conf, false)
+ return m, composeAction(m.ctx, m.conf, noPostAction)
case "2":
return m, submitAction(m.ctx, m.conf)
case "3":
- return m, tea.Sequence(composeAction(m.conf, false), submitAction(m.ctx, m.conf))
+ return m, composeAction(m.ctx, m.conf, submitAfterCompose)
case "a":
m.altscreenActive = !m.altscreenActive
@@ -124,6 +125,7 @@ func (m model) View() string {
}
if m.err != nil {
+ log.Println(m.err)
s += "\n"
s += errroStyle.Render(fmt.Sprintf("\nERROR: %s\n", m.err))
s += "\n"
diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go
index c44cce3..2968454 100644
--- a/internal/easyhttp/easyhttp.go
+++ b/internal/easyhttp/easyhttp.go
@@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
+ "log"
"net/http"
"sync"
)
@@ -87,6 +88,7 @@ func PostData[T any](ctx context.Context, uri, apiKey string, data *T, servers .
var wg sync.WaitGroup
for _, server := range servers {
+ log.Println("Submitting data to", server)
wg.Add(1)
go func(server string) {
defer wg.Done()