package main import ( "context" "flag" "fmt" "os" "time" "gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/config" "gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/dashboard" "gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/store" ) // briefCommand is the `school brief` entry point. Phase 5 builds and deletes the // stored brief (the summary half of FR-10); sending it is Phase 7. func briefCommand(args []string) { if len(args) == 0 { usage("school brief build --child NAME | school brief list") } switch args[0] { case "build": briefBuild(args[1:]) case "list": briefList() default: usage("school brief build --child NAME | school brief list") } } func briefBuild(args []string) { fs := flag.NewFlagSet("brief build", flag.ContinueOnError) var child string fs.StringVar(&child, "child", "", "child name (Harry or Finn)") _ = fs.Parse(args) if child == "" { fmt.Fprintln(os.Stderr, "school brief build: --child is required") os.Exit(2) } if err := runBriefBuild(child); err != nil { fmt.Fprintln(os.Stderr, "school brief:", err) os.Exit(1) } } func runBriefBuild(child string) error { ctx := context.Background() cfg, err := config.LoadDB() if err != nil { return err } st, err := store.Open(cfg.DBPath) if err != nil { return err } defer st.Close() if err := st.Migrate(ctx); err != nil { return err } c, err := st.ChildByName(ctx, child) if err != nil { return err } if c == nil { return fmt.Errorf("unknown child %q", child) } items, err := st.ItemsForChild(ctx, c.ID) if err != nil { return err } text := dashboard.Render(c.Name, dashboard.Classify(items, time.Now(), cfg.Location), cfg.Location) id, err := st.CreateSummary(ctx, c.Name, text) if err != nil { return err } fmt.Printf("brief=%d recipient=%s bytes=%d\n", id, c.Name, len(text)) return nil } func briefList() { ctx := context.Background() cfg, err := config.LoadDB() if err != nil { fmt.Fprintln(os.Stderr, "school brief:", err) os.Exit(1) } st, err := store.Open(cfg.DBPath) if err != nil { fmt.Fprintln(os.Stderr, "school brief:", err) os.Exit(1) } defer st.Close() summaries, err := st.ListSummaries(ctx, 20) if err != nil { fmt.Fprintln(os.Stderr, "school brief:", err) os.Exit(1) } if len(summaries) == 0 { fmt.Println("no briefs stored") return } for _, sm := range summaries { status := "draft" if sm.SentAt != nil { status = "sent " + sm.SentAt.Format("2006-01-02 15:04") } fmt.Printf("%d\t%s\t%s\t%s\n", sm.ID, sm.Recipient, status, firstLine(sm.Text)) } } func firstLine(s string) string { for i, r := range s { if r == '\n' { return s[:i] } } return s } func usage(msg string) { fmt.Fprintln(os.Stderr, "usage:", msg) os.Exit(2) }