From c77cac13c61264104dd61ca861bf92f30c7d1b7d Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Wed, 18 Oct 2023 16:24:14 -0400 Subject: [PATCH] feat: write output files to directory Signed-off-by: Chris Gianelloni --- .gitignore | 9 +++++++++ cmd/bursa/main.go | 7 ++++--- go.mod | 1 + go.sum | 2 ++ internal/cli/cli.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7feb49e..49a21b6 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,12 @@ go.work # binary /bursa + +# output files +payment.addr +payment.skey +payment.vkey +seed.txt +stake.addr +stake.skey +stake.vkey diff --git a/cmd/bursa/main.go b/cmd/bursa/main.go index 0a1f356..7b9232a 100644 --- a/cmd/bursa/main.go +++ b/cmd/bursa/main.go @@ -28,7 +28,7 @@ func main() { fs.Usage = func() { fmt.Fprintf( flag.CommandLine.Output(), - "Usage: bursa [-h] [subcommand] [args]\n\nSubcommands:\n\n", + "Usage: bursa [-h] [args]\n\nSubcommands:\n\n", ) fmt.Fprintf( flag.CommandLine.Output(), @@ -63,9 +63,10 @@ func main() { }() var subCommand string - // Parse subcommand (default: "cli") + // Parse subcommand if len(fs.Args()) < 1 { - subCommand = "cli" + fs.Usage() + os.Exit(1) } else { subCommand = fs.Arg(0) } diff --git a/go.mod b/go.mod index ed939e4..4586c7a 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/swaggo/swag v1.16.2 github.com/tyler-smith/go-bip39 v1.1.0 go.uber.org/zap v1.26.0 + golang.org/x/sync v0.1.0 ) require ( diff --git a/go.sum b/go.sum index 9374c7a..2739771 100644 --- a/go.sum +++ b/go.sum @@ -431,6 +431,8 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 0a30359..deae556 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -18,6 +18,9 @@ import ( "flag" "fmt" "os" + "path/filepath" + + "golang.org/x/sync/errgroup" "github.com/blinklabs-io/bursa" "github.com/blinklabs-io/bursa/internal/config" @@ -58,5 +61,45 @@ func Run() { 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 + } else { + fmt.Printf("Output dir: %v\n", *flagOutput) + _, err := os.Stat(*flagOutput) + if os.IsNotExist(err) { + err = os.MkdirAll(*flagOutput, 0755) + if err != nil { + panic(err) + } + } + var fileMap = []map[string]string{ + {"seed.txt": w.Mnemonic}, + {"payment.addr": w.PaymentAddress}, + {"stake.addr": w.StakeAddress}, + {"payment.vkey": bursa.GetKeyFile(w.PaymentVKey)}, + {"payment.skey": bursa.GetKeyFile(w.PaymentSKey)}, + {"stake.vkey": bursa.GetKeyFile(w.StakeVKey)}, + {"stake.skey": bursa.GetKeyFile(w.StakeSKey)}, + } + var g errgroup.Group + for _, m := range fileMap { + for k, v := range m { + k := k + v := v + g.Go(func() error { + path := filepath.Join(*flagOutput, k) + err = os.WriteFile(path, []byte(v), 0666) + if err != nil { + return err + } + return err + }) + } + } + err = g.Wait() + if err != nil { + logger.Fatalf("error occurred: %s", err) + os.Exit(1) + } + logger.Infof("wrote output files to %s", *flagOutput) + + } }