handle context cancelation

This commit is contained in:
2025-11-13 16:05:16 +00:00
parent 54fced39aa
commit f3d0f5d71a
7 changed files with 50 additions and 29 deletions

View File

@@ -0,0 +1,50 @@
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"git.naterciomoniz.net/applications/broker2anexoj/internal"
"git.naterciomoniz.net/applications/broker2anexoj/internal/trading212"
"golang.org/x/sync/errgroup"
)
func main() {
err := run(context.Background())
if err != nil {
slog.Error("found a fatal issue", slog.Any("err", err))
os.Exit(1)
}
}
func run(ctx context.Context) error {
ctx, cancel := signal.NotifyContext(ctx, os.Kill, os.Interrupt)
defer cancel()
eg, ctx := errgroup.WithContext(ctx)
f, err := os.Open("test.csv")
if err != nil {
return fmt.Errorf("open statement: %w", err)
}
reader := trading212.NewRecordReader(f)
writer := internal.NewStdOutLogger()
eg.Go(func() error {
return internal.BuildReport(ctx, reader, writer)
})
err = eg.Wait()
if err != nil {
return err
}
slog.Info("Finish processing statement")
return nil
}