Skip to content

Commit

Permalink
Merge pull request #17 from blinklabs-io/chore/refactor-to-flag
Browse files Browse the repository at this point in the history
chore: refactor to use flag for command line
  • Loading branch information
wolf31o2 authored Oct 1, 2023
2 parents fa6cf4f + 1284010 commit 9562de8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
40 changes: 38 additions & 2 deletions cmd/bursa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 18 additions & 22 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cli

import (
"flag"
"fmt"
"os"

Expand Down Expand Up @@ -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()
Expand All @@ -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
}

0 comments on commit 9562de8

Please sign in to comment.