|
1 |
| -package main |
| 1 | +package grogu |
2 | 2 |
|
3 |
| -import "github.com/bandprotocol/chain/v2/grogu" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/cosmos/cosmos-sdk/client/flags" |
| 10 | + "github.com/cosmos/cosmos-sdk/crypto/keyring" |
| 11 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 12 | + "github.com/cosmos/cosmos-sdk/version" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/spf13/viper" |
| 15 | + |
| 16 | + band "github.com/bandprotocol/chain/v2/app" |
| 17 | + "github.com/bandprotocol/chain/v2/cmd/grogu/cmd" |
| 18 | + "github.com/bandprotocol/chain/v2/grogu/context" |
| 19 | +) |
4 | 20 |
|
5 | 21 | func main() {
|
6 |
| - grogu.Main() |
| 22 | + appConfig := sdk.GetConfig() |
| 23 | + band.SetBech32AddressPrefixesAndBip44CoinTypeAndSeal(appConfig) |
| 24 | + |
| 25 | + ctx := &context.Context{} |
| 26 | + rootCmd := createRootCmd(ctx) |
| 27 | + |
| 28 | + if err := rootCmd.Execute(); err != nil { |
| 29 | + fmt.Println(err) |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func createRootCmd(ctx *context.Context) *cobra.Command { |
| 35 | + rootCmd := &cobra.Command{ |
| 36 | + Use: "grogu", |
| 37 | + Short: "BandChain daemon to submit signal prices for feeds module", |
| 38 | + } |
| 39 | + |
| 40 | + rootCmd.AddCommand( |
| 41 | + cmd.ConfigCmd(), |
| 42 | + cmd.KeysCmd(ctx), |
| 43 | + cmd.RunCmd(ctx), |
| 44 | + version.NewVersionCommand(), |
| 45 | + ) |
| 46 | + |
| 47 | + rootCmd.PersistentPreRunE = createPersistentPreRunE(rootCmd, ctx) |
| 48 | + rootCmd.PersistentFlags().String(flags.FlagHome, getDefaultHome(), "home directory") |
| 49 | + return rootCmd |
| 50 | +} |
| 51 | + |
| 52 | +func createPersistentPreRunE(rootCmd *cobra.Command, ctx *context.Context) func( |
| 53 | + cmd *cobra.Command, |
| 54 | + args []string, |
| 55 | +) error { |
| 56 | + return func(_ *cobra.Command, _ []string) error { |
| 57 | + home, err := rootCmd.PersistentFlags().GetString(flags.FlagHome) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + ctx.Home = home |
| 62 | + |
| 63 | + if err = os.MkdirAll(home, os.ModePerm); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + cdc := band.MakeEncodingConfig().Marshaler |
| 68 | + ctx.Keyring, err = keyring.New("band", keyring.BackendTest, home, nil, cdc) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + return initConfig(ctx, rootCmd) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func initConfig(c *context.Context, _ *cobra.Command) error { |
| 78 | + viper.SetConfigFile(path.Join(c.Home, "config.yaml")) |
| 79 | + |
| 80 | + // If the config file cannot be read, only cmd flags will be used. |
| 81 | + _ = viper.ReadInConfig() |
| 82 | + if err := viper.Unmarshal(&c.Config); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func getDefaultHome() string { |
| 89 | + userHomeDir, err := os.UserHomeDir() |
| 90 | + if err != nil { |
| 91 | + panic(err) |
| 92 | + } |
| 93 | + |
| 94 | + return filepath.Join(userHomeDir, ".grogu") |
7 | 95 | }
|
0 commit comments