From 0ef1d840efb2aef7c19dedde436482e0348579df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Cola=C3=A7o?= Date: Sat, 19 Oct 2024 18:38:49 +0200 Subject: [PATCH] fix(core): Post all not returning errors Fix concurrency issue causing post all to hang forever when a error was returned Fix Issue causing errors not being printed correctly when posting to multiple social media --- cmd/root.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 94bd479..026e746 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -54,8 +54,8 @@ var rootCmd = &cobra.Command{ os.Exit(0) } - if errors := postAll(text, mediaPath); errors != nil { - for err := range errors { + if errors := postAll(text, mediaPath); len(errors) > 0 { + for _, err := range errors { fmt.Println(err) } os.Exit(1) @@ -152,43 +152,41 @@ func Execute() { } } -func postAll(text, mediaPath string) []error { +func postAll(text, mediaPath string) (errors []error) { var wg sync.WaitGroup errChan := make(chan error, 2) wg.Add(2) - go func(wg *sync.WaitGroup, errChan chan<- error) { + go func() { defer wg.Done() if err := postX(text, mediaPath); err != nil { errChan <- err } errChan <- nil - }(&wg, errChan) + }() - go func(wg *sync.WaitGroup, errChan chan<- error) { + go func() { defer wg.Done() if err := postMastodon(text, mediaPath); err != nil { errChan <- err } errChan <- nil - }(&wg, errChan) + }() - wg.Wait() - close(errChan) + go func() { + wg.Wait() + close(errChan) + }() - var errors []error - if len(errChan) >= 0 { - for err := range errChan { - if err != nil { - errors = append(errors, err) - } + for err := range errChan { + if err != nil { + errors = append(errors, err) } - return errors } - return nil + return errors } func init() {