44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Contract struct {
|
|
Network string `yaml:"network"`
|
|
Address string `yaml:"address"`
|
|
MaxTokenId string `yaml:"max_token_id"`
|
|
FromBlock string `yaml:"from_block"` // kept for backward compatibility, maps to MaxTokenId
|
|
}
|
|
|
|
type File struct {
|
|
Contracts map[string]Contract `yaml:"contracts"`
|
|
}
|
|
|
|
func Load(path string) (*File, error) {
|
|
if path == "" { return &File{Contracts: map[string]Contract{}}, nil }
|
|
f, err := os.Open(path)
|
|
if err != nil { return nil, err }
|
|
defer f.Close()
|
|
b, err := io.ReadAll(f)
|
|
if err != nil { return nil, err }
|
|
var cfg File
|
|
if err := yaml.Unmarshal(b, &cfg); err != nil { return nil, err }
|
|
if cfg.Contracts == nil { cfg.Contracts = map[string]Contract{} }
|
|
return &cfg, nil
|
|
}
|
|
|
|
func (f *File) Get(name string) (Contract, bool) {
|
|
c, ok := f.Contracts[name]
|
|
return c, ok
|
|
}
|
|
|
|
func (c Contract) Validate() error {
|
|
if c.Network == "" || c.Address == "" { return errors.New("missing network or address") }
|
|
return nil
|
|
}
|