Skip to content

Commit

Permalink
v1.0.0-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Bowman committed May 23, 2024
1 parent 750de82 commit 5b1ae31
Show file tree
Hide file tree
Showing 10 changed files with 1,349 additions and 1,222 deletions.
122 changes: 38 additions & 84 deletions icq-relayer/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,52 @@
package cmd

import (
"fmt"
"os"
"path"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/quicksilver-zone/quicksilver/icq-relayer/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/strangelove-ventures/lens/client"
"gopkg.in/yaml.v2"
)

// initConfig reads in config file and ENV variables if set.
func initConfig(cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
if err != nil {
return err
}
cfg = &config.Config{}
cfgPath := path.Join(home, "config.yaml")
_, err = os.Stat(cfgPath)
if err != nil {
if !os.IsNotExist(err) { // Return immediately
return err
}

if err := config.CreateConfig(home); err != nil {
return err
}
}

viper.SetConfigFile(cfgPath)
err = viper.ReadInConfig()
if err != nil {
fmt.Println("Failed to read in config:", err)
os.Exit(1)
}
// home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
// if err != nil {
// return err
// }
// cfg = &types.Config{}
// cfgPath := path.Join(home, "config.yaml")
// _, err = os.Stat(cfgPath)
// if err != nil {
// if !os.IsNotExist(err) { // Return immediately
// return err
// }

// if err := types.CreateConfig(home); err != nil {
// return err
// }
// }

// viper.SetConfigFile(cfgPath)
// err = viper.ReadInConfig()
// if err != nil {
// fmt.Println("Failed to read in config:", err)
// os.Exit(1)
// }

// read the config file bytes
file, err := os.ReadFile(viper.ConfigFileUsed())
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}

// unmarshall them into the struct
if err = yaml.Unmarshal(file, cfg); err != nil {
fmt.Println("Error unmarshalling config:", err)
os.Exit(1)
}

// instantiate chain client
// TODO: this is a bit of a hack, we should probably have a
// better way to inject modules into the client
cfg.Cl = make(map[string]*client.ChainClient)
for name, chain := range cfg.Chains {
chain.Modules = append([]module.AppModuleBasic{}, ModuleBasics...)
cl, err := client.NewChainClient(nil, chain, home, os.Stdin, os.Stdout)
if err != nil {
fmt.Println("Error creating chain client:", err)
os.Exit(1)
}
cfg.Cl[name] = cl
}

// override chain if needed
if cmd.PersistentFlags().Changed("chain") {
defaultChain, err := cmd.PersistentFlags().GetString("chain")
if err != nil {
return err
}

cfg.DefaultChain = defaultChain
}

if cmd.PersistentFlags().Changed("output") {
output, err := cmd.PersistentFlags().GetString("output")
if err != nil {
return err
}

// Should output be a global configuration item?
for chain := range cfg.Chains {
cfg.Chains[chain].OutputFormat = output
}
}
// file, err := os.ReadFile(viper.ConfigFileUsed())
// if err != nil {
// fmt.Println("Error reading file:", err)
// os.Exit(1)
// }

// // unmarshall them into the struct
// if err = yaml.Unmarshal(file, cfg); err != nil {
// fmt.Println("Error unmarshalling config:", err)
// os.Exit(1)
// }

// validate configuration
if err = config.ValidateConfig(cfg); err != nil {
fmt.Println("Error parsing chain config:", err)
os.Exit(1)
}
// if err = types.ValidateConfig(cfg); err != nil {
// fmt.Println("Error parsing chain config:", err)
// os.Exit(1)
// }
return nil
}
Loading

0 comments on commit 5b1ae31

Please sign in to comment.