Skip to content

Commit

Permalink
fix(core): Post all not returning errors
Browse files Browse the repository at this point in the history
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
  • Loading branch information
coolapso committed Oct 19, 2024
1 parent e019273 commit 0ef1d84
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 0ef1d84

Please sign in to comment.