From 1284010185f6223c02ef8066dd9af5eb9198fe9c Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Sun, 1 Oct 2023 09:51:42 -0400 Subject: [PATCH] chore: refactor to use flag for command line Signed-off-by: Chris Gianelloni --- cmd/bursa/main.go | 40 ++++++++++++++++++++++++++++++++++++++-- internal/cli/cli.go | 40 ++++++++++++++++++---------------------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/cmd/bursa/main.go b/cmd/bursa/main.go index 1b7539c..4699bbd 100644 --- a/cmd/bursa/main.go +++ b/cmd/bursa/main.go @@ -15,17 +15,53 @@ package main import ( + "flag" "fmt" "os" + + "github.com/blinklabs-io/bursa/internal/config" + "github.com/blinklabs-io/bursa/internal/logging" ) func main() { + fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError) + fs.Usage = func() { + fmt.Fprintf( + flag.CommandLine.Output(), + "Usage: bursa [-h] [subcommand] [args]\n\nSubcommands:\n\n", + ) + fmt.Fprintf( + flag.CommandLine.Output(), + " - %-18s %s\n", + "cli", + "run a terminal command", + ) + } + _ = fs.Parse(os.Args[1:]) // ignore parse errors + + // Load Config + _, err := config.LoadConfig() + if err != nil { + fmt.Printf("Failed to load config: %s\n", err) + os.Exit(1) + } + // Configure logging + logging.Setup() + logger := logging.GetLogger() + // Sync logger on exit + defer func() { + if err := logger.Sync(); err != nil { + // ignore error + return + } + }() + var subCommand string // Parse subcommand (default: "cli") - if len(os.Args) < 2 { + if len(fs.Args()) < 1 { subCommand = "cli" } else { - subCommand = os.Args[1] + subCommand = fs.Arg(0) } switch subCommand { diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 780b8f0..2690941 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -15,6 +15,7 @@ package cli import ( + "flag" "fmt" "os" @@ -49,24 +50,16 @@ func NewDefaultWallet(mnemonic string) (*bursa.Wallet, error) { } func Run() { - // Load Config - cfg, err := config.LoadConfig() - if err != nil { - fmt.Printf("Failed to load config: %s\n", err) - os.Exit(1) + fs := flag.NewFlagSet("cli", flag.ExitOnError) + flagOutput := fs.String("output", "", "output directory for files, otherwise uses STDOUT") + if len(os.Args) >= 2 { + _ = fs.Parse(os.Args[2:]) // ignore parse errors } - // Configure logging - logging.Setup() - logger := logging.GetLogger() - // Sync logger on exit - defer func() { - if err := logger.Sync(); err != nil { - // ignore error - return - } - }() + cfg := config.GetConfig() + logger := logging.GetLogger() // Load mnemonic + var err error mnemonic := cfg.Mnemonic if mnemonic == "" { mnemonic, err = bursa.NewMnemonic() @@ -80,12 +73,15 @@ func Run() { } logger.Infof("Loaded mnemonic and generated address...") - fmt.Printf("MNEMONIC=%s\n", w.Mnemonic) - fmt.Printf("PAYMENT_ADDRESS=%s\n", w.PaymentAddress) - fmt.Printf("STAKE_ADDRESS=%s\n", w.StakeAddress) - fmt.Printf("payment.vkey=%s\n", w.PaymentVKey) - fmt.Printf("payment.skey=%s\n", w.PaymentSKey) - fmt.Printf("stake.vkey=%s\n", w.StakeVKey) - fmt.Printf("stake.skey=%s\n", w.StakeSKey) + if *flagOutput == "" { + fmt.Printf("MNEMONIC=%s\n", w.Mnemonic) + fmt.Printf("PAYMENT_ADDRESS=%s\n", w.PaymentAddress) + fmt.Printf("STAKE_ADDRESS=%s\n", w.StakeAddress) + + fmt.Printf("payment.vkey=%s\n", bursa.GetKeyFile(w.PaymentVKey)) + fmt.Printf("payment.skey=%s\n", bursa.GetKeyFile(w.PaymentSKey)) + fmt.Printf("stake.vkey=%s\n", bursa.GetKeyFile(w.StakeVKey)) + fmt.Printf("stake.skey=%s\n", bursa.GetKeyFile(w.StakeSKey)) + } // TODO: output to files }