package main // This is the starter. runAdd and runList are finished. The TODOs are in // runReport and runDelete. import ( "context" "errors" "flag" "fmt" "io" "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 { // TODO: a flag set called "report" with -addr and -month, as in runList. // Get the report with client.Report. Print "3 expenses in 2026-11", or // "3 expenses in total" with no month, and stop there if the count is 0. // Then print an empty line and "By category", one line per category in the // form " %-12s %10s\n", the same for "By month", and last an empty line // and the "Total" line in the same form. Use expense.FormatCents for every // amount. return errors.New("TODO: report is not written yet") } // expenses delete [-addr host:port] id func runDelete(ctx context.Context, args []string, out io.Writer) error { // TODO: a flag set called "delete" with -addr. After the flags there must // be exactly one argument, a whole number (strconv.Atoi). Delete it with // client.Delete, and print "Deleted expense 3." return errors.New("TODO: delete is not written yet") }