forked from ava-labs/avalanchego
/
flags.go
69 lines (59 loc) · 1.86 KB
/
flags.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package tmpnet
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cast"
"github.com/MetalBlockchain/metalgo/utils/perms"
)
// Defines a mapping of flag keys to values intended to be supplied to
// an invocation of an AvalancheGo node.
type FlagsMap map[string]interface{}
// Utility function simplifying construction of a FlagsMap from a file.
func ReadFlagsMap(path string, description string) (*FlagsMap, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %w", description, err)
}
flagsMap := &FlagsMap{}
if err := json.Unmarshal(bytes, flagsMap); err != nil {
return nil, fmt.Errorf("failed to unmarshal %s: %w", description, err)
}
return flagsMap, nil
}
// SetDefaults ensures the effectiveness of flag overrides by only
// setting values supplied in the defaults map that are not already
// explicitly set.
func (f FlagsMap) SetDefaults(defaults FlagsMap) {
for key, value := range defaults {
if _, ok := f[key]; !ok {
f[key] = value
}
}
}
// GetStringVal simplifies retrieving a map value as a string.
func (f FlagsMap) GetStringVal(key string) (string, error) {
rawVal, ok := f[key]
if !ok {
return "", nil
}
val, err := cast.ToStringE(rawVal)
if err != nil {
return "", fmt.Errorf("failed to cast value for %q: %w", key, err)
}
return val, nil
}
// Write simplifies writing a FlagsMap to the provided path. The
// description is used in error messages.
func (f FlagsMap) Write(path string, description string) error {
bytes, err := DefaultJSONMarshal(f)
if err != nil {
return fmt.Errorf("failed to marshal %s: %w", description, err)
}
if err := os.WriteFile(path, bytes, perms.ReadWrite); err != nil {
return fmt.Errorf("failed to write %s: %w", description, err)
}
return nil
}