Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor to use flag for command line #17

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading