2020-12-21 15:56:56 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/caarlos0/env/v6"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-12-22 15:00:42 +01:00
|
|
|
HTTP HTTPConfig `yaml:"http"`
|
|
|
|
Data DataConfig `yaml:"data"`
|
|
|
|
Powow PowowConfig `ymal:"powow"`
|
2020-12-21 15:56:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPConfig struct {
|
|
|
|
Address string `yaml:"address" env:"FAKESMS_HTTP_ADDRESS"`
|
|
|
|
TemplateDir string `yaml:"templateDir" env:"FAKESMS_HTTP_TEMPLATEDIR"`
|
|
|
|
PublicDir string `yaml:"publicDir" env:"FAKESMS_HTTP_PUBLICDIR"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataConfig struct {
|
|
|
|
Path string `yaml:"path" env:"FAKESMS_DATA_PATH"`
|
|
|
|
}
|
|
|
|
|
2020-12-22 15:00:42 +01:00
|
|
|
type PowowConfig struct {
|
|
|
|
APIKey string `yaml:"apiKey" env:"FAKESMS_POWOW_API_KEY"`
|
|
|
|
SMS []PowowSMS `yaml:"sms"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PowowSMS struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
From string `yaml:"from"`
|
|
|
|
Content string `yaml:"content"`
|
|
|
|
ShortLink bool `yaml:"shortLink"`
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:56:56 +01:00
|
|
|
// NewFromFile retrieves the configuration from the given file
|
|
|
|
func NewFromFile(filepath string) (*Config, error) {
|
|
|
|
config := NewDefault()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not read file '%s'", filepath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(data, config); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not unmarshal configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithEnvironment(conf *Config) error {
|
|
|
|
if err := env.Parse(conf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDumpDefault() *Config {
|
|
|
|
config := NewDefault()
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefault() *Config {
|
|
|
|
return &Config{
|
|
|
|
HTTP: HTTPConfig{
|
2020-12-22 15:00:42 +01:00
|
|
|
Address: ":3000",
|
2020-12-21 15:56:56 +01:00
|
|
|
TemplateDir: "template",
|
|
|
|
PublicDir: "public",
|
|
|
|
},
|
|
|
|
Data: DataConfig{
|
|
|
|
Path: "fakesms.db",
|
|
|
|
},
|
2020-12-22 15:00:42 +01:00
|
|
|
Powow: PowowConfig{
|
|
|
|
APIKey: "powow",
|
|
|
|
SMS: []PowowSMS{
|
|
|
|
{
|
|
|
|
Name: "Powow SMS",
|
|
|
|
From: "FakeSMS",
|
|
|
|
ShortLink: false,
|
|
|
|
Content: `Bonjour %Subscriber:Firstname%,
|
|
|
|
|
|
|
|
Lorem ipsum dolor sit amet...
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-21 15:56:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Dump(config *Config, w io.Writer) error {
|
|
|
|
data, err := yaml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not dump config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|