From 242dc4b0466dab910d51507677d910e7e3c2f035 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 27 Mar 2024 13:34:26 +0100 Subject: [PATCH] refactor: return commands with functions --- main.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 04026f9..9b0e34c 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ func main() { rootCmd := &ffcli.Command{ ShortUsage: "govbox ", ShortHelp: "Set of commands for GovGen proposals.", - Subcommands: []*ffcli.Command{tallyCmd, accountsCmd, genesisCmd, autoStakingCmd, distributionCmd}, + Subcommands: []*ffcli.Command{tallyCmd(), accountsCmd(), genesisCmd(), autoStakingCmd(), distributionCmd()}, Exec: func(ctx context.Context, args []string) error { return flag.ErrHelp }, @@ -27,8 +27,8 @@ func main() { } } -var ( - tallyCmd = &ffcli.Command{ +func tallyCmd() *ffcli.Command { + return &ffcli.Command{ Name: "tally", ShortUsage: "govbox tally ", ShortHelp: "Print the comparison between the tally result and the tally computed from ", @@ -54,8 +54,10 @@ var ( return nil }, } +} - accountsCmd = &ffcli.Command{ +func accountsCmd() *ffcli.Command { + return &ffcli.Command{ Name: "accounts", ShortUsage: "govbox accounts ", ShortHelp: "Consolidate the data in into a single file /accounts.json", @@ -102,8 +104,10 @@ var ( return nil }, } +} - genesisCmd = &ffcli.Command{ +func genesisCmd() *ffcli.Command { + return &ffcli.Command{ Name: "genesis", ShortUsage: "govbox genesis ", ShortHelp: "Convert /accounts.json to /bank.genesis", @@ -130,8 +134,10 @@ The command must be run once /accounts.json is generated by the return nil }, } +} - autoStakingCmd = &ffcli.Command{ +func autoStakingCmd() *ffcli.Command { + return &ffcli.Command{ Name: "autostaking", ShortUsage: "govbox autostaking ", ShortHelp: "Experimental command to evaluate auto-staking algorithms", @@ -144,16 +150,23 @@ The command must be run once /accounts.json is generated by the return autoStaking(filepath.Join(datapath, "genesis.json")) }, } +} - distributionCmd = &ffcli.Command{ +func distributionCmd() *ffcli.Command { + fs := flag.NewFlagSet("distribution", flag.ContinueOnError) + chartMode := fs.Bool("chart", false, "Outputs a chart instead of Markdown tables") + + cmd := &ffcli.Command{ Name: "distribution", ShortUsage: "govbox distribution ", ShortHelp: "Convert /accounts.json into /airdrop.json", LongHelp: `Generate the ATONE distribution described in GovGen PROP 001`, + FlagSet: fs, Exec: func(ctx context.Context, args []string) error { if len(args) == 0 { return flag.ErrHelp } + fs.Parse(args) var ( datapath = args[0] accountsFile = filepath.Join(datapath, "accounts.json") @@ -174,8 +187,8 @@ The command must be run once /accounts.json is generated by the if err := os.WriteFile(airdropFile, bz, 0o666); err != nil { return err } - printAirdropStats(airdrop) - return nil + return printAirdropStats(airdrop, *chartMode) }, } -) + return cmd +}