Skip to content

Commit

Permalink
Merge branch 'v3/cosmos-stargate' of github.com:desmos-labs/juno into…
Browse files Browse the repository at this point in the history
… v3/cosmos-v0.44.x
  • Loading branch information
RiccardoM committed Apr 11, 2022
2 parents dee0e17 + dba26c5 commit 94a8a46
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
13 changes: 12 additions & 1 deletion cmd/init/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package init

import (
"fmt"
"io/ioutil"
"os"

"github.com/forbole/juno/v3/types/config"
Expand Down Expand Up @@ -45,11 +46,21 @@ func NewInitCmd(cfg *Config) *cobra.Command {

// Get the config from the flags
yamlCfg := cfg.GetConfigCreator()(cmd)
return config.Write(yamlCfg, configFilePath)
return writeConfig(yamlCfg, configFilePath)
},
}

cmd.Flags().Bool(flagReplace, false, "overrides any existing configuration")

return cmd
}

// writeConfig allows to write the given configuration into the file present at the given path
func writeConfig(cfg WritableConfig, path string) error {
bz, err := cfg.GetBytes()
if err != nil {
return err
}

return ioutil.WriteFile(path, bz, 0600)
}
10 changes: 8 additions & 2 deletions cmd/init/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import (
"github.com/forbole/juno/v3/types/config"
)

// WritableConfig represents a configuration that can be written to a file
type WritableConfig interface {
// GetBytes returns the bytes to be written to the config file when initializing it
GetBytes() ([]byte, error)
}

// ConfigCreator represents a function that builds a Config instance from the flags that have been specified by the
// user inside the given command.
type ConfigCreator = func(cmd *cobra.Command) config.Config
type ConfigCreator = func(cmd *cobra.Command) WritableConfig

// DefaultConfigCreator represents the default configuration creator that builds a Config instance using the values
// specified using the default flags.
func DefaultConfigCreator(_ *cobra.Command) config.Config {
func DefaultConfigCreator(_ *cobra.Command) WritableConfig {
return config.DefaultConfig()
}

Expand Down
7 changes: 6 additions & 1 deletion modules/pruning/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ type Module struct {

// NewModule builds a new Module instance
func NewModule(cfg config.Config, db database.Database, logger logging.Logger) *Module {
pruningCfg, err := ParseConfig(cfg.GetBytes())
bz, err := cfg.GetBytes()
if err != nil {
panic(err)
}

pruningCfg, err := ParseConfig(bz)
if err != nil {
panic(err)
}
Expand Down
7 changes: 6 additions & 1 deletion modules/telemetry/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ type Module struct {

// NewModule returns a new Module implementation
func NewModule(cfg config.Config) *Module {
telemetryCfg, err := ParseConfig(cfg.GetBytes())
bz, err := cfg.GetBytes()
if err != nil {
panic(err)
}

telemetryCfg, err := ParseConfig(bz)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions types/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func DefaultConfig() Config {
)
}

func (c Config) GetBytes() []byte {
return c.bytes
func (c Config) GetBytes() ([]byte, error) {
return c.bytes, nil
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down
13 changes: 0 additions & 13 deletions types/config/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package config

import (
"io/ioutil"
"path"

"gopkg.in/yaml.v3"
)

var (
Expand All @@ -15,13 +12,3 @@ var (
func GetConfigFilePath() string {
return path.Join(HomePath, "config.yaml")
}

// Write allows to write the given configuration into the file present at the given path
func Write(cfg Config, path string) error {
bz, err := yaml.Marshal(&cfg)
if err != nil {
return err
}

return ioutil.WriteFile(path, bz, 0666)
}

0 comments on commit 94a8a46

Please sign in to comment.