package main import ( "fmt" "github.com/sirupsen/logrus" "go.bug.st/serial" "gopkg.in/yaml.v2" "io/ioutil" ) type ClientConfig struct { SerialAddress string `yaml:"serial_address"` BaudRate int `yaml:"serial_baudrate"` BufferSize uint16 `yaml:"serial_buffer_size"` Paranoid bool `yaml:"paranoid"` Debug bool `yaml:"debug"` GNUPGBinary string `yaml:"gnupg_bin"` OpenSSLBinary string `yaml:"openssl_bin"` MySQLDSN string `yaml:"mysql_dsn"` } var defaultConfig = ClientConfig{ SerialAddress: "/dev/ttyUSB0", BaudRate: 115200, BufferSize: 2048, Paranoid: false, Debug: false, OpenSSLBinary: "/usr/bin/openssl", GNUPGBinary: "/usr/bin/gpg", MySQLDSN: ":@/database?parseTime=true", } func generateExampleConfig(configFile string) (config *ClientConfig, err error) { config = &defaultConfig configBytes, err := yaml.Marshal(config) if err != nil { logrus.Errorf("could not generate configuration data") return } logrus.Infof("example data for %s:\n\n---\n%s\n", configFile, configBytes) return } func readConfig(configFile string) (config *ClientConfig, err error) { source, err := ioutil.ReadFile(configFile) if err != nil { logrus.Errorf("opening configuration file failed: %v", err) if exampleConfig, err := generateExampleConfig(configFile); err != nil { return nil, err } else { logrus.Info("starting with default config") return exampleConfig, nil } } if err := yaml.Unmarshal(source, &config); err != nil { return nil, fmt.Errorf("loading configuration file failed: %v", err) } return config, nil } func fillSerialMode(clientConfig *ClientConfig) *serial.Mode { return &serial.Mode{ BaudRate: clientConfig.BaudRate, DataBits: 8, StopBits: serial.OneStopBit, Parity: serial.NoParity, } }