From 2f61b07aea82fa0f5b98686bdfeba2e297c7b21b Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Mon, 7 Sep 2026 03:13:01 +0200 Subject: refactor(cardmarket): move handlers to own directory --- main.go | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 68e4adf..4fb8231 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "errors" "flag" "fmt" "io" @@ -10,6 +11,7 @@ import ( "os" "codeberg.org/dijitol/trading_post/src/cardmarket" + "codeberg.org/dijitol/trading_post/src/cardmarket/handlers" "codeberg.org/dijitol/trading_post/src/cli" "codeberg.org/dijitol/trading_post/src/config" "codeberg.org/dijitol/trading_post/src/oauth" @@ -52,7 +54,7 @@ func Run( "account", "[account] outputs information about the owner of the credentials", func(_ *flag.FlagSet) error { - account, err := client.GetAccountInformation(ctx) + account, err := handlers.GetAccountInformation(ctx, client) if err != nil { return err } @@ -62,7 +64,34 @@ func Run( return err } - fmt.Fprintln(stdout, string(out)) + _, _ = fmt.Fprintln(stdout, string(out)) + + return nil + }, + ) + sg.AddSubcommand( + "vacation", + "[vacation] sets the vacation state", + func(fs *flag.FlagSet) error { + onVacation := fs.Bool("onVacation", false, "flag if the user is on vacation or not") + cancelOrders := fs.Bool("cancelOrders", false, "flag to cancel open orders, resp. request cancellation for open orders") + relistItems := fs.Bool("relistItems", false, "flag to relist items for cancelled orders; only applies if (cancelOrders) is also provided and set to true for all orders, that are effectively cancelled") + + if err := fs.Parse(args[2:]); err != nil { + return err + } + + if err := handlers.SetVacationState( + ctx, + client, + *onVacation, + cancelOrders, + relistItems, + ); err != nil { + return err + } + + _, _ = fmt.Fprintln(stdout, "") return nil }, @@ -71,7 +100,7 @@ func Run( "games", "[games] outputs all the games", func(_ *flag.FlagSet) error { - games, err := client.GetGames(ctx) + games, err := handlers.GetGames(ctx, client) if err != nil { return err } @@ -81,18 +110,24 @@ func Run( return err } - fmt.Fprintln(stdout, string(out)) + _, _ = fmt.Fprintln(stdout, string(out)) return nil }, ) if len(args) < 2 { // 1st if program name, 2nd is subcommand - return fmt.Errorf("error: missing subcommand - usage: `trading_post [command]`\n%s", sg.Usage()) + return fmt.Errorf("error: missing subcommand\n\nusage: `trading_post [subcommand]`\n%s", sg.Usage()) + } + + subcmd := args[1] + + if subcmd == "--help" || subcmd == "-h" { + return fmt.Errorf("usage: `trading_post [subcommand]`\n%s", sg.Usage()) } - if err := sg.ExecSubcommand(args[1]); err != nil { - return err + if err := sg.ExecSubcommand(subcmd); err != nil { + return errors.Join(err, fmt.Errorf("\nusage: `trading_post [subcommand]`\n%s", sg.Usage())) } return nil -- cgit v1.2.3