Skip to content

Commit

Permalink
Refactor app (#34)
Browse files Browse the repository at this point in the history
* remove homepath from app

* rename functions

* remove LoadKeyInfo

* move validatePassphrase to store

* fix store interface

* start metrics from command instead of inside app

* fix add key

* fix keys

* add comment
  • Loading branch information
nkitlabs authored Feb 27, 2025
1 parent 078edbd commit 7b46176
Show file tree
Hide file tree
Showing 25 changed files with 335 additions and 488 deletions.
2 changes: 1 addition & 1 deletion cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $ %s chains list
$ %s ch l`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
if app.Config == nil {
return fmt.Errorf("config does not exist: %s", app.HomePath)
return fmt.Errorf("config is not initialized")
}

i := 1
Expand Down
28 changes: 20 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"

"github.com/bandprotocol/falcon/relayer"
"github.com/bandprotocol/falcon/relayer/config"
)

// configCmd returns a command that manages global configuration file
Expand Down Expand Up @@ -37,7 +39,7 @@ $ %s config show --home %s
$ %s cfg list`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
if app.Config == nil {
return fmt.Errorf("config does not exist: %s", app.HomePath)
return fmt.Errorf("config is not initialized")
}

b, err := toml.Marshal(app.Config)
Expand All @@ -63,23 +65,33 @@ func configInitCmd(app *relayer.App) *cobra.Command {
$ %s config init --home %s
$ %s cfg i`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flagHome)
filePath, err := cmd.Flags().GetString(flagFile)
if err != nil {
return err
}

file, err := cmd.Flags().GetString(flagFile)
if err != nil {
return err
// Parse the config from the file if file's path is given
var cfg *config.Config
if filePath != "" {
b, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("cannot read a config file %s: %w", filePath, err)
}

cfg, err = config.ParseConfig(b)
if err != nil {
return fmt.Errorf("parsing config error %w", err)
}
}

if err := app.InitConfigFile(home, file); err != nil {
if err := app.SaveConfig(cfg); err != nil {
return err
}

return app.InitPassphrase()
passphrase := os.Getenv(PassphraseEnvKey)
return app.SavePassphrase(passphrase)
},
}
cmd.Flags().StringP(flagFile, "f", "", "fetch toml data from specified file")
cmd.Flags().StringP(flagFile, "f", "", "input config .toml file path")
return cmd
}
2 changes: 1 addition & 1 deletion cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestConfigShowNotInit(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "show")
require.ErrorContains(t, res.Err, "config does not exist:")
require.ErrorContains(t, res.Err, "config is not initialized")
}

func TestConfigInitDefault(t *testing.T) {
Expand Down
32 changes: 19 additions & 13 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"

"github.com/bandprotocol/falcon/relayer"
chainstypes "github.com/bandprotocol/falcon/relayer/chains/types"
)

const (
Expand Down Expand Up @@ -99,21 +100,26 @@ $ %s k a eth test-key`, appName, appName)),
}
}

// Add the key to the app
keyOutput, err := app.AddKey(
chainName,
keyName,
input.Mnemonic,
input.PrivateKey,
uint32(input.CoinType),
uint(input.Account),
uint(input.Index),
)
if err != nil {
return err
var key *chainstypes.Key
if input.PrivateKey != "" {
key, err = app.AddKeyByPrivateKey(chainName, keyName, input.PrivateKey)
if err != nil {
return err
}
} else {
key, err = app.AddKeyByMnemonic(
chainName, keyName,
input.Mnemonic,
uint32(input.CoinType),
uint(input.Account),
uint(input.Index),
)
if err != nil {
return err
}
}

out, err := json.MarshalIndent(keyOutput, "", " ")
out, err := json.MarshalIndent(key, "", " ")
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var defaultHome = filepath.Join(os.Getenv("HOME"), ".falcon")
// NewRootCmd returns the root command for falcon.
func NewRootCmd(log *zap.Logger) *cobra.Command {
passphrase := os.Getenv(PassphraseEnvKey)
app := falcon.NewApp(log, defaultHome, false, nil, passphrase, nil)
homePath := defaultHome
app := falcon.NewApp(log, false, nil, passphrase, nil)

// RootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Expand All @@ -50,7 +51,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) (err error) {
// set up store
app.Store, err = store.NewFileSystem(app.HomePath)
app.Store, err = store.NewFileSystem(homePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -85,7 +86,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {
}

// Register --home flag
rootCmd.PersistentFlags().StringVar(&app.HomePath, flagHome, defaultHome, "set home directory")
rootCmd.PersistentFlags().StringVar(&homePath, flagHome, defaultHome, "set home directory")
if err := viper.BindPFlag(flagHome, rootCmd.PersistentFlags().Lookup(flagHome)); err != nil {
panic(err)
}
Expand Down
15 changes: 13 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/spf13/cobra"

"github.com/bandprotocol/falcon/internal/relayermetrics"
"github.com/bandprotocol/falcon/relayer"
)

Expand All @@ -31,12 +32,22 @@ $ %s start 1 12 # start relaying data from specific tunnelIDs.`, appName, a
tunnelIDs = append(tunnelIDs, tunnelID)
}

metricsListenAddrFlag, err := cmd.Flags().GetString(flagMetricsListenAddr)
metricsListenAddr, err := cmd.Flags().GetString(flagMetricsListenAddr)
if err != nil {
return err
}

return app.Start(cmd.Context(), tunnelIDs, metricsListenAddrFlag)
// setup metrics server
if metricsListenAddr == "" {
metricsListenAddr = app.Config.Global.MetricsListenAddr
}
if metricsListenAddr != "" {
if err := relayermetrics.StartMetricsServer(cmd.Context(), app.Log, metricsListenAddr); err != nil {
return err
}
}

return app.Start(cmd.Context(), tunnelIDs)
},
}

Expand Down
82 changes: 42 additions & 40 deletions internal/relayertest/mocks/chain_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/relayertest/mocks/chain_provider_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions internal/relayertest/mocks/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7b46176

Please sign in to comment.