forked from TrueCloudLab/neoneo-go
5ef08f60ae
It's not needed any more with Go 1.13 as we have wrapping/unwrapping in base packages. All errors.Wrap calls are replaced with fmt.Errorf, some strings are improved along the way.
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/go-yaml/yaml"
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
|
)
|
|
|
|
const userAgentFormat = "/NEO-GO:%s/"
|
|
|
|
// Version the version of the node, set at build time.
|
|
var Version string
|
|
|
|
// Config top level struct representing the config
|
|
// for the node.
|
|
type Config struct {
|
|
ProtocolConfiguration ProtocolConfiguration `yaml:"ProtocolConfiguration"`
|
|
ApplicationConfiguration ApplicationConfiguration `yaml:"ApplicationConfiguration"`
|
|
}
|
|
|
|
// GenerateUserAgent creates user agent string based on build time environment.
|
|
func (c Config) GenerateUserAgent() string {
|
|
return fmt.Sprintf(userAgentFormat, Version)
|
|
}
|
|
|
|
// Load attempts to load the config from the given
|
|
// path for the given netMode.
|
|
func Load(path string, netMode netmode.Magic) (Config, error) {
|
|
configPath := fmt.Sprintf("%s/protocol.%s.yml", path, netMode)
|
|
return LoadFile(configPath)
|
|
}
|
|
|
|
// LoadFile loads config from the provided path.
|
|
func LoadFile(configPath string) (Config, error) {
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
return Config{}, fmt.Errorf("config '%s' doesn't exist", configPath)
|
|
}
|
|
|
|
configData, err := ioutil.ReadFile(configPath)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("unable to read config: %w", err)
|
|
}
|
|
|
|
config := Config{
|
|
ApplicationConfiguration: ApplicationConfiguration{
|
|
PingInterval: 30,
|
|
PingTimeout: 90,
|
|
},
|
|
}
|
|
|
|
err = yaml.Unmarshal(configData, &config)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("failed to unmarshal config YAML: %w", err)
|
|
}
|
|
|
|
return config, nil
|
|
}
|