-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathconfig.go
56 lines (50 loc) · 1.64 KB
/
config.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
package config
import (
"fmt"
"os"
"path/filepath"
"github.com/davecgh/go-spew/spew"
"github.com/jftuga/ellipsis"
"gopkg.in/yaml.v2"
)
// Config is used to configure a Scribe instance and information about chains and contracts.
type Config struct {
// Chains stores all chain information
Chains ChainConfigs `yaml:"chains"`
// RPCURL is the url of the omnirpc.
RPCURL string `yaml:"rpc_url"`
// Verbose is used to enable verbose logging.
Verbose bool `yaml:"verbose"`
}
// IsValid makes sure the config is valid. This is done by calling IsValid() on each
// submodule. If any method returns an error that is returned here and the entirety
// of IsValid returns false. Any warnings are logged by the submodules respective loggers.
func (c *Config) IsValid() (ok bool, err error) {
if ok, err = c.Chains.IsValid(); !ok {
return false, err
}
if c.RPCURL == "" {
return false, fmt.Errorf("%w: rpc url cannot be empty", ErrRequiredField)
}
return true, nil
}
// Encode gets the encoded config.yaml file.
func (c Config) Encode() ([]byte, error) {
output, err := yaml.Marshal(&c)
if err != nil {
return nil, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(spew.Sdump(c), 20), err)
}
return output, nil
}
// DecodeConfig parses in a config from a file.
func DecodeConfig(filePath string) (cfg Config, err error) {
input, err := os.ReadFile(filepath.Clean(filePath))
if err != nil {
return Config{}, fmt.Errorf("failed to read file: %w", err)
}
err = yaml.Unmarshal(input, &cfg)
if err != nil {
return Config{}, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(string(input), 30), err)
}
return cfg, nil
}