forked from forbole/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
76 lines (62 loc) · 1.89 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package genesis
import (
"github.com/spf13/cobra"
parsecmdtypes "github.com/gotabit/juno/v5/cmd/parse/types"
"github.com/gotabit/juno/v5/modules"
nodeconfig "github.com/gotabit/juno/v5/node/config"
"github.com/gotabit/juno/v5/types/utils"
)
const (
flagPath = "genesis-file-path"
)
// NewGenesisCmd returns the Cobra command allowing to parse the genesis file
func NewGenesisCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "genesis-file",
Short: "Parse the genesis file",
Long: `
Parse the genesis file only.
Note that the modules built will NOT have access to the node as they are only supposed to deal with the genesis
file itself and not the on-chain data.`,
RunE: func(cmd *cobra.Command, args []string) error {
// Read the configuration
cfg, err := parsecmdtypes.ReadConfig(parseConfig)
if err != nil {
return err
}
// Set the node to be of type None so that the node won't be built
cfg.Node.Type = nodeconfig.TypeNone
// Build the parsing context
parseCtx, err := parsecmdtypes.GetParserContext(cfg, parseConfig)
if err != nil {
return err
}
// Get the file path
genesisFilePath := cfg.Parser.GenesisFilePath
customPath, _ := cmd.Flags().GetString(flagPath)
if customPath != "" {
genesisFilePath = customPath
}
// Read the genesis file
genDoc, err := utils.ReadGenesisFileGenesisDoc(genesisFilePath)
if err != nil {
return err
}
genState, err := utils.GetGenesisState(genDoc)
if err != nil {
return err
}
for _, module := range parseCtx.Modules {
if module, ok := module.(modules.GenesisModule); ok {
err = module.HandleGenesis(genDoc, genState)
if err != nil {
return err
}
}
}
return nil
},
}
cmd.Flags().String(flagPath, "", "Path to the genesis file to be used. If empty, the path will be taken from the config file")
return cmd
}