package main import ( "context" "errors" "flag" "fmt" "io" "strconv" "strings" "time" "expenses/internal/expense" ) // addrFlag adds the -addr flag that every client command has. func addrFlag(flags *flag.FlagSet) *string { return flags.String("addr", defaultAddr(), "address of the server (env EXPENSES_ADDR)") } // serverURL turns an -addr value into the server's base URL. Both // localhost:8080 and http://localhost:8080/ give http://localhost:8080. func serverURL(addr string) string { addr = strings.TrimSuffix(addr, "/") if strings.HasPrefix(addr, "http://") || strings.HasPrefix(addr, "https://") { return addr } return "http://" + addr } // expenses add [-addr host:port] [-date 2026-11-25] -category name [-note text] amount func runAdd(ctx context.Context, args []string, out io.Writer) error { flags := flag.NewFlagSet("add", flag.ExitOnError) addr := addrFlag(flags) date := flags.String("date", time.Now().Format(expense.DateLayout), "day of the expense, like 2026-11-25") category := flags.String("category", "", "category, like food or rent") note := flags.String("note", "", "a short note") flags.Parse(args) client := newAPIClient(serverURL(*addr)) if flags.NArg() != 1 { return errors.New("add needs one amount, like 12.50, after the flags") } cents, err := expense.ParseAmount(flags.Arg(0)) if err != nil { return err } // Check here too, so that a mistake is reported without a request. The // server checks again, because other programs can call it too. e, err := expense.New(*date, *category, *note, cents) if err != nil { return err } added, err := client.Add(ctx, e) if err != nil { return err } line := fmt.Sprintf("Added expense %d: %s %s %s %s", added.ID, added.Date, added.Category, expense.FormatCents(added.Cents), added.Note) fmt.Fprintln(out, strings.TrimRight(line, " ")) return nil } // expenses list [-addr host:port] [-month 2026-11] func runList(ctx context.Context, args []string, out io.Writer) error { flags := flag.NewFlagSet("list", flag.ExitOnError) addr := addrFlag(flags) month := flags.String("month", "", "only this month, like 2026-11") flags.Parse(args) client := newAPIClient(serverURL(*addr)) list, err := client.List(ctx, *month) if err != nil { return err } if len(list) == 0 { fmt.Fprintln(out, "No expenses.") return nil } fmt.Fprintf(out, "%4s %-10s %-12s %10s %s\n", "ID", "DATE", "CATEGORY", "AMOUNT", "NOTE") for _, e := range list { // TrimRight removes the spaces left at the end when there is no note. line := fmt.Sprintf("%4d %-10s %-12s %10s %s", e.ID, e.Date, e.Category, expense.FormatCents(e.Cents), e.Note) fmt.Fprintln(out, strings.TrimRight(line, " ")) } return nil } // expenses report [-addr host:port] [-month 2026-11] func runReport(ctx context.Context, args []string, out io.Writer) error { flags := flag.NewFlagSet("report", flag.ExitOnError) addr := addrFlag(flags) month := flags.String("month", "", "only this month, like 2026-11") flags.Parse(args) client := newAPIClient(serverURL(*addr)) report, err := client.Report(ctx, *month) if err != nil { return err } period := "in total" if *month != "" { period = "in " + *month } fmt.Fprintf(out, "%d expenses %s\n", report.Count, period) if report.Count == 0 { return nil } fmt.Fprintln(out) fmt.Fprintln(out, "By category") for _, sum := range report.ByCategory { fmt.Fprintf(out, " %-12s %10s\n", sum.Name, expense.FormatCents(sum.Cents)) } fmt.Fprintln(out) fmt.Fprintln(out, "By month") for _, sum := range report.ByMonth { fmt.Fprintf(out, " %-12s %10s\n", sum.Name, expense.FormatCents(sum.Cents)) } fmt.Fprintln(out) fmt.Fprintf(out, " %-12s %10s\n", "Total", expense.FormatCents(report.TotalCents)) return nil } // expenses delete [-addr host:port] id func runDelete(ctx context.Context, args []string, out io.Writer) error { flags := flag.NewFlagSet("delete", flag.ExitOnError) addr := addrFlag(flags) flags.Parse(args) client := newAPIClient(serverURL(*addr)) if flags.NArg() != 1 { return errors.New("delete needs one id, like 3, after the flags") } id, err := strconv.Atoi(flags.Arg(0)) if err != nil { return fmt.Errorf("the id %q must be a whole number", flags.Arg(0)) } err = client.Delete(ctx, id) if err != nil { return err } fmt.Fprintf(out, "Deleted expense %d.\n", id) return nil }