142 lines
3.6 KiB
Go
142 lines
3.6 KiB
Go
|
/*
|
||
|
Copyright 2022 CAcert Inc.
|
||
|
SPDX-License-Identifier: Apache-2.0
|
||
|
|
||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
|
|
||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package config
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"gopkg.in/yaml.v3"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
defaultSerialTimeout = 5 * time.Second
|
||
|
defaultHealthInterval = 30 * time.Second
|
||
|
defaultHealthStart = 5 * time.Second
|
||
|
defaultResponseAnnounceTimeout = 30 * time.Second
|
||
|
defaultResponseDataTimeout = 2 * time.Second
|
||
|
)
|
||
|
|
||
|
type SettingsError struct {
|
||
|
msg string
|
||
|
}
|
||
|
|
||
|
func (se SettingsError) Error() string {
|
||
|
return fmt.Sprintf("invalid Settings %s", se.msg)
|
||
|
}
|
||
|
|
||
|
type Serial struct {
|
||
|
Device string `yaml:"device"`
|
||
|
Baud int `yaml:"baud"`
|
||
|
Timeout time.Duration `yaml:"timeout"`
|
||
|
}
|
||
|
|
||
|
type ClientConfig struct {
|
||
|
Serial Serial `yaml:"serial"`
|
||
|
HealthInterval time.Duration `yaml:"health-interval"`
|
||
|
HealthStart time.Duration `yaml:"health-start"`
|
||
|
ResponseAnnounceTimeout time.Duration `yaml:"response-announce-timeout"`
|
||
|
ResponseDataTimeout time.Duration `yaml:"response-data-timeout"`
|
||
|
}
|
||
|
|
||
|
func (c *ClientConfig) UnmarshalYAML(n *yaml.Node) error {
|
||
|
data := struct {
|
||
|
Serial Serial `yaml:"serial"`
|
||
|
HealthInterval time.Duration `yaml:"health-interval"`
|
||
|
HealthStart time.Duration `yaml:"health-start"`
|
||
|
ResponseAnnounceTimeout time.Duration `yaml:"response-announce-timeout"`
|
||
|
ResponseDataTimeout time.Duration `yaml:"response-data-timeout"`
|
||
|
}{}
|
||
|
|
||
|
err := n.Decode(&data)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not decode YAML: %w", err)
|
||
|
}
|
||
|
|
||
|
if data.Serial.Device == "" {
|
||
|
return SettingsError{"you must specify a serial 'device'"}
|
||
|
}
|
||
|
|
||
|
if data.Serial.Baud == 0 {
|
||
|
data.Serial.Baud = 115200
|
||
|
}
|
||
|
|
||
|
if data.Serial.Timeout == 0 {
|
||
|
data.Serial.Timeout = defaultSerialTimeout
|
||
|
}
|
||
|
|
||
|
c.Serial = data.Serial
|
||
|
|
||
|
if data.HealthInterval == 0 {
|
||
|
data.HealthInterval = defaultHealthInterval
|
||
|
}
|
||
|
|
||
|
c.HealthInterval = data.HealthInterval
|
||
|
|
||
|
if data.HealthStart == 0 {
|
||
|
data.HealthStart = defaultHealthStart
|
||
|
}
|
||
|
|
||
|
c.HealthStart = data.HealthStart
|
||
|
|
||
|
if data.ResponseAnnounceTimeout == 0 {
|
||
|
data.ResponseAnnounceTimeout = defaultResponseAnnounceTimeout
|
||
|
}
|
||
|
|
||
|
c.ResponseAnnounceTimeout = data.ResponseAnnounceTimeout
|
||
|
|
||
|
if data.ResponseDataTimeout == 0 {
|
||
|
data.ResponseDataTimeout = defaultResponseDataTimeout
|
||
|
}
|
||
|
|
||
|
c.ResponseDataTimeout = data.ResponseDataTimeout
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func New(clientConfigFile string) (*ClientConfig, error) {
|
||
|
configFile, err := os.Open(clientConfigFile)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not open signer client configuration file %s: %w", clientConfigFile, err)
|
||
|
}
|
||
|
|
||
|
defer func() { _ = configFile.Close() }()
|
||
|
|
||
|
clientConfig, err := loadConfiguration(configFile)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not load client configuration from %s: %w", clientConfigFile, err)
|
||
|
}
|
||
|
|
||
|
return clientConfig, nil
|
||
|
}
|
||
|
|
||
|
func loadConfiguration(r io.Reader) (*ClientConfig, error) {
|
||
|
var config *ClientConfig
|
||
|
|
||
|
decoder := yaml.NewDecoder(r)
|
||
|
err := decoder.Decode(&config)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("could not parse YAML configuration: %w", err)
|
||
|
}
|
||
|
|
||
|
return config, nil
|
||
|
}
|