-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenesis.go
More file actions
95 lines (87 loc) · 2.61 KB
/
genesis.go
File metadata and controls
95 lines (87 loc) · 2.61 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/sei-protocol/seictl/internal/patch"
"github.com/urfave/cli/v3"
)
var genesisCmd = cli.Command{
Name: "genesis",
Usage: "Manage Sei genesis JSON file",
Commands: []*cli.Command{
{
Name: "patch",
Usage: "Apply a merge-patch to the Sei genesis JSON file",
MutuallyExclusiveFlags: []cli.MutuallyExclusiveFlags{outputterFlags},
Arguments: []cli.Argument{
&cli.StringArg{
Name: "file",
Destination: &destinations.genesis.patch.file,
Config: cli.StringConfig{
TrimSpace: true,
},
},
},
Action: func(ctx context.Context, command *cli.Command) error {
var patchBytes []byte
if destinations.genesis.patch.file == "" {
// Read full multi-line input from stdin
var buffer bytes.Buffer
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
buffer.Write(scanner.Bytes())
buffer.WriteByte('\n')
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("reading input from stdin: %w", err)
}
patchBytes = buffer.Bytes()
} else {
var err error
patchBytes, err = os.ReadFile(destinations.genesis.patch.file)
if err != nil {
return fmt.Errorf("reading patch file: %w", err)
}
}
patchBytes = []byte(strings.TrimSpace(string(patchBytes)))
if len(patchBytes) == 0 {
return nil
}
patchData := make(map[string]any)
if err := json.Unmarshal(patchBytes, &patchData); err != nil {
return fmt.Errorf("parsing patch: %w", err)
}
if destinations.home == "" {
userHome, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("failed to get user home directory: %w", err)
}
destinations.home = filepath.Clean(filepath.Join(userHome, ".sei"))
}
genesisPath := filepath.Join(destinations.home, "config", "genesis.json")
genesisBytes, err := os.ReadFile(genesisPath)
if err != nil {
return fmt.Errorf("reading genesis file: %w", err)
}
genesis := make(map[string]any)
if err := json.Unmarshal(genesisBytes, &genesis); err != nil {
return fmt.Errorf("parsing genesis: %w", err)
}
patchedGenesis := patch.Merge(genesis, patchData)
var prettyPatchedGenesis bytes.Buffer
encoder := json.NewEncoder(&prettyPatchedGenesis)
encoder.SetIndent("", " ")
if err := encoder.Encode(patchedGenesis); err != nil {
return fmt.Errorf("marshalling patched genesis: %w", err)
}
return output(genesisPath, prettyPatchedGenesis)
},
},
},
}