// Package config defines all application configuration
package config

import (
	"os"
	"path/filepath"

	"github.com/BurntSushi/toml"

	"quantex.com/qfixpt/src/app"
	"quantex.com/qfixpt/src/common/tracerr"
)

// Read the config from disk
func Read(files []string) (cfg app.Config, err error) {
	for _, file := range files {
		var d []byte

		if file != "" {
			if d, err = os.ReadFile(filepath.Clean(file)); err != nil {
				return app.Config{}, tracerr.Errorf("%v", err.Error())
			}
		}

		if _, err = toml.Decode(string(d), &cfg); err != nil {
			return app.Config{}, tracerr.Errorf("%v", err.Error())
		}
	}

	return cfg, nil
}
