-
Notifications
You must be signed in to change notification settings - Fork 670
/
cmd.go
56 lines (47 loc) · 1.16 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package genesis
import (
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/vms/example/xsvm/genesis"
)
var errUnknownEncoding = errors.New("unknown encoding")
func Command() *cobra.Command {
c := &cobra.Command{
Use: "genesis",
Short: "Creates a chain's genesis and prints it to stdout",
RunE: genesisFunc,
}
flags := c.Flags()
AddFlags(flags)
return c
}
func genesisFunc(c *cobra.Command, args []string) error {
flags := c.Flags()
config, err := ParseFlags(flags, args)
if err != nil {
return err
}
genesisBytes, err := genesis.Codec.Marshal(genesis.Version, config.Genesis)
if err != nil {
return err
}
switch config.Encoding {
case binaryEncoding:
_, err = os.Stdout.Write(genesisBytes)
return err
case hexEncoding:
encoded, err := formatting.Encode(formatting.Hex, genesisBytes)
if err != nil {
return err
}
_, err = fmt.Println(encoded)
return err
default:
return fmt.Errorf("%w: %q", errUnknownEncoding, config.Encoding)
}
}