forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_balances_export.go
78 lines (66 loc) · 1.8 KB
/
genesis_balances_export.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
77
78
package main
import (
"context"
"fmt"
"os"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
)
// newBalancesExportCmd creates the genesis balances export subcommand
func newBalancesExportCmd(balancesCfg *balancesCfg, io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "export",
ShortUsage: "balances export [flags] <output-path>",
ShortHelp: "exports the balances from the genesis.json",
LongHelp: "Exports the balances from the genesis.json to an output file",
},
commands.NewEmptyConfig(),
func(_ context.Context, args []string) error {
return execBalancesExport(balancesCfg, io, args)
},
)
}
func execBalancesExport(cfg *balancesCfg, io commands.IO, args []string) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}
// Load the genesis state
if genesis.AppState == nil {
return errAppStateNotSet
}
state := genesis.AppState.(gnoland.GnoGenesisState)
if len(state.Balances) == 0 {
io.Println("No genesis balances to export")
return nil
}
// Make sure the output file path is specified
if len(args) == 0 {
return errNoOutputFile
}
// Open output file
outputFile, err := os.OpenFile(
args[0],
os.O_RDWR|os.O_CREATE|os.O_APPEND,
0o755,
)
if err != nil {
return fmt.Errorf("unable to create output file, %w", err)
}
// Save the balances
for _, balance := range state.Balances {
if _, err = outputFile.WriteString(
fmt.Sprintf("%s\n", balance),
); err != nil {
return fmt.Errorf("unable to write to output, %w", err)
}
}
io.Printfln(
"Exported %d balances",
len(state.Balances),
)
return nil
}