From bfa17633390b7d7074bd584019e808eca4dd59a2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 3 Jul 2024 11:45:14 +0300 Subject: add no servers configured error --- internal/client/tui/submit.go | 10 +++++++++- internal/config/client/client.go | 17 +++++++++++++++-- internal/easyhttp/easyhttp.go | 12 ++++++++---- internal/easyhttp/saferrors.go | 8 ++++---- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/internal/client/tui/submit.go b/internal/client/tui/submit.go index 5507000..1176e3c 100644 --- a/internal/client/tui/submit.go +++ b/internal/client/tui/submit.go @@ -5,6 +5,8 @@ import ( "codeberg.org/snonux/gos/internal/config/client" config "codeberg.org/snonux/gos/internal/config/client" + "codeberg.org/snonux/gos/internal/easyhttp" + "codeberg.org/snonux/gos/internal/types" tea "github.com/charmbracelet/bubbletea" ) @@ -18,10 +20,16 @@ func submitAction(conf config.ClientConfig) tea.Cmd { } func submitMessage(conf client.ClientConfig, filePath string, callback func() error) tea.Cmd { + servers, err := conf.Servers() + if err == nil { + var entry types.Entry + err = easyhttp.PostData("/submit", conf.APIKey, &entry, servers...) + } + return func() tea.Msg { return finishedMsg{ callback: callback, - err: fmt.Errorf("This is a sample error"), + err: err, } } } diff --git a/internal/config/client/client.go b/internal/config/client/client.go index 3ceb053..5571898 100644 --- a/internal/config/client/client.go +++ b/internal/config/client/client.go @@ -1,6 +1,7 @@ package client import ( + "errors" "fmt" "os" "strings" @@ -30,6 +31,18 @@ func New(configFile string) (ClientConfig, error) { return conf, nil } -func (conf ClientConfig) Servers() []string { - return strings.Split(conf.Server, ",") +func (conf ClientConfig) Servers() ([]string, error) { + var servers []string + + for _, server := range strings.Split(conf.Server, ",") { + if server != "" { + servers = append(servers, server) + } + } + + if len(servers) == 0 { + return servers, errors.New("no server(s) configured") + } + + return servers, nil } diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go index bb963ad..7b72b9e 100644 --- a/internal/easyhttp/easyhttp.go +++ b/internal/easyhttp/easyhttp.go @@ -1,11 +1,11 @@ package easyhttp import ( - "errors" + "encoding/json" "fmt" "io" "net/http" - "encoding/json" + "sync" ) func Get(uri, apiKey string) ([]byte, error) { @@ -50,14 +50,18 @@ func PostData[T any](uri, apiKey string, data *T, servers ...string) error { if len(servers) == 0 { return fmt.Errorf("no server configured") } - var errs SafeErrors + var errs safErrors + var wg sync.WaitGroup for _, server := range servers { + wg.Add(1) go func(server string){ - errs.Append(postData[T](fmt.Sprintf("%s/%s"), apiKey, data)) + defer wg.Done() + errs.Append(postData[T](fmt.Sprintf("%s/%s", server, uri), apiKey, data)) }(server) } + wg.Wait() return errs.Join() } diff --git a/internal/easyhttp/saferrors.go b/internal/easyhttp/saferrors.go index 32dd77f..464c90d 100644 --- a/internal/easyhttp/saferrors.go +++ b/internal/easyhttp/saferrors.go @@ -6,12 +6,12 @@ import ( ) // Safe errors -type SafErrors struct { +type safErrors struct { errs []error mutex sync.Mutex } -func (errs *SafErrors) Append(err error) { +func (errs *safErrors) Append(err error) { if err == nil { return } @@ -20,12 +20,12 @@ func (errs *SafErrors) Append(err error) { errs.errs = append(errs.errs, err) } -func (errs *SafErrors) Join() error { +func (errs *safErrors) Join() error { errs.mutex.Lock() defer errs.mutex.Unlock() return errors.Join(errs.errs...) } -func (errs *SafErrors) Error() string { +func (errs *safErrors) Error() string { return errs.Join().Error() } -- cgit v1.2.3