gengitkan/internal/config/config.go

81 lines
1.2 KiB
Go
Raw Permalink Normal View History

2019-11-28 11:50:51 +01:00
package config
import (
"io"
"os"
ini "gopkg.in/ini.v1"
)
type Config struct {
Debug bool
HTTP HTTPConfig
Gitea GiteaConfig
2019-12-01 22:12:13 +01:00
Data DataConfig
2019-11-28 11:50:51 +01:00
}
type HTTPConfig struct {
Address string
PublicDir string
}
type GiteaConfig struct {
ClientID string
ClientSecret string
RedirectURL string
LogoutURL string
AuthURL string
TokenURL string
Scopes []string
APIBaseURL string
}
2019-12-01 22:12:13 +01:00
type DataConfig struct {
DBPath string
}
2019-11-28 11:50:51 +01:00
// NewFromFile retrieves the configuration from the given file
func NewFromFile(filepath string) (*Config, error) {
config := NewDefault()
cfg, err := ini.Load(filepath)
if err != nil {
return nil, err
}
cfg.ValueMapper = os.ExpandEnv
if err := cfg.MapTo(config); err != nil {
return nil, err
}
return config, nil
}
func NewDefault() *Config {
return &Config{
Debug: false,
HTTP: HTTPConfig{
Address: ":3000",
2020-04-30 10:32:12 +02:00
PublicDir: "${GENGITKAN_HTTP_PUBDIR}",
2019-11-28 11:50:51 +01:00
},
Gitea: GiteaConfig{},
2019-12-01 22:12:13 +01:00
Data: DataConfig{
2020-04-30 10:32:12 +02:00
DBPath: "${GENGITKAN_DATA_DBPATH}",
2019-12-01 22:12:13 +01:00
},
2019-11-28 11:50:51 +01:00
}
}
func Dump(config *Config, w io.Writer) error {
cfg := ini.Empty()
if err := cfg.ReflectFrom(config); err != nil {
return err
}
if _, err := cfg.WriteTo(w); err != nil {
return err
}
return nil
}