Skip to content

Commit

Permalink
fix: error handling on config file
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed May 4, 2022
1 parent ba32216 commit 594941f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -55,7 +56,8 @@ func ReadPersistentFlags(cmd *cobra.Command) error {

if err := v.ReadInConfig(); err != nil {
// It's okay if there isn't a config file
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
var configFileNotFound viper.ConfigFileNotFoundError
if !errors.As(err, &configFileNotFound) {
return err
}
}
Expand Down
46 changes: 23 additions & 23 deletions cmd/send.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package cmd

import (
"context"
"context"

"okp4/cosmos-faucet/pkg/client"
"okp4/cosmos-faucet/pkg/client"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

// NewSendCommand returns a CLI command to interactively send amount token(s) to given address.
func NewSendCommand() *cobra.Command {
sendCmd := &cobra.Command{
Use: "send <address>",
Short: "Send tokens to a given address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
faucet, err := client.NewFaucet(context.Background(), config)
if err != nil {
return err
}

defer func(faucet *client.Faucet) {
_ = faucet.Close()
}(faucet)

return faucet.SendTxMsg(args[0])
},
}

return sendCmd
sendCmd := &cobra.Command{
Use: "send <address>",
Short: "Send tokens to a given address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
faucet, err := client.NewFaucet(context.Background(), config)
if err != nil {
return err
}

defer func(faucet *client.Faucet) {
_ = faucet.Close()
}(faucet)

return faucet.SendTxMsg(args[0])
},
}

return sendCmd
}

func init() {
rootCmd.AddCommand(NewSendCommand())
rootCmd.AddCommand(NewSendCommand())
}
1 change: 0 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func NewStartCommand() *cobra.Command {
Use: "start",
Short: "Start the REST api",
RunE: func(cmd *cobra.Command, args []string) error {

faucet, err := client.NewFaucet(context.Background(), config)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ func NewFaucet(ctx context.Context, config pkg.Config) (*Faucet, error) {
if err != nil {
return nil, err
}

fromAddr := types.AccAddress(fromPrivKey.PubKey().Address())
account, err := cosmos.GetAccount(ctx, grpcConn, fromAddr.String())
if err != nil {
return nil, err
}

return &Faucet{
Context: ctx,
Expand Down

0 comments on commit 594941f

Please sign in to comment.