pcmt/app/settings/settings.go
leo fc4460d5e1
All checks were successful
continuous-integration/drone/push Build is passing
go: delete pertinent ENVs after loading settings
2023-05-12 22:43:36 +02:00

230 lines
5.2 KiB
Go

package settings
import (
"os"
"git.dotya.ml/mirre-mt/pcmt/config"
"git.dotya.ml/mirre-mt/pcmt/slogging"
"golang.org/x/exp/slog"
)
type Settings struct {
host string
port int
appName string
isLive bool
isDevel bool
sessionCookieName string
sessionCookieSecret string
assetsPath string
templatesPath string
version string
dbConnstring string
dbType string
dbIsSetUp bool
}
// cleantgt is a list of ENV vars pertaining to pcmt.
var cleantgt = []string{
"PCMT_LIVE",
"PCMT_DEVEL",
"PCMT_CONNSTRING",
"PCMT_DBTYPE",
}
// New returns a new instance of the settings struct.
func New() *Settings {
return &Settings{}
}
// Consolidate reconciles whatever values are set in config and via flags and
// sets it to one place that should be regarded as a single source of truth -
// the settings struct. Order of preference for values is (from higher to
// lower) as follows: flag -> Env var -> configuration file.
func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, devel *bool, version string) {
log := *slogging.Logger() // have a local copy.
log.Logger = log.Logger.With(
slog.Group("pcmt extra", slog.String("module", "app/settings")),
)
log.Debug("starting to consolidate settings")
log.Debug("parsing config values")
s.SetHost(conf.Host)
s.SetPort(conf.Port)
s.SetAppName(conf.AppName)
s.SetIsLive(conf.LiveMode)
s.SetIsDevel(conf.DevelMode)
s.SetSessionCookieName(conf.Session.CookieName)
s.SetSessionCookieSecret(conf.Session.CookieSecret)
log.Debug("checking flag overrides")
overrideMsg := "overriding '%s' based on a flag: %+v"
if isFlagPassed("host") {
if h := *host; h != "unset" && h != conf.Host {
log.Debugf(overrideMsg, "host", h)
s.SetHost(h)
}
}
if isFlagPassed("port") {
if p := *port; p > 0 && p < 65536 && p != conf.Port {
log.Debugf(overrideMsg, "port", p)
s.SetPort(p)
}
}
if isFlagPassed("devel") {
if d := *devel; d != conf.DevelMode {
log.Debugf(overrideMsg, "develMode", d)
s.SetIsDevel(d)
}
}
s.SetVersion(version)
}
// Host returns the host.
func (s *Settings) Host() string {
return s.host
}
// Port returns the port.
func (s *Settings) Port() int {
return s.port
}
// AppName returns the appName.
func (s *Settings) AppName() string {
return s.appName
}
// IsLive returns the value of isLive of the receiver.
func (s *Settings) IsLive() bool {
return s.isLive
}
// IsDevel returns the value of isDevel of the receiver.
func (s *Settings) IsDevel() bool {
return s.isDevel
}
// SessionCookieName returns the sessionCookieName.
func (s *Settings) SessionCookieName() string {
return s.sessionCookieName
}
// SessionCookieSecret returns the sessionCookieSecret.
func (s *Settings) SessionCookieSecret() string {
return s.sessionCookieSecret
}
// AssetsPath returns the assetsPath.
func (s *Settings) AssetsPath() string {
return s.assetsPath
}
// TemplatesPath returns the templatesPath.
func (s *Settings) TemplatesPath() string {
return s.templatesPath
}
// Version returns the version.
func (s *Settings) Version() string {
return s.version
}
// DbIsSetUp returns the dbIsSetUp.
func (s *Settings) DbIsSetUp() bool {
return s.dbIsSetUp
}
// DbConnstring returns the dbConnString.
func (s *Settings) DbConnstring() string {
return s.dbConnstring
}
// DbType returns the dbType.
func (s *Settings) DbType() string {
return s.dbType
}
// SetHost sets the host.
func (s *Settings) SetHost(host string) {
s.host = host
}
// SetPort sets the port.
func (s *Settings) SetPort(port int) {
s.port = port
}
// SetAppName sets the appName.
func (s *Settings) SetAppName(appName string) {
s.appName = appName
}
// SetIsLive sets the value of isLive of the receiver.
func (s *Settings) SetIsLive(live bool) {
s.isLive = live
}
// SetIsDevel sets the value of isDevel of the receiver.
func (s *Settings) SetIsDevel(devel bool) {
s.isDevel = devel
}
// SetSessionCookieName sets the sessionCookieName.
func (s *Settings) SetSessionCookieName(sessionCookieName string) {
s.sessionCookieName = sessionCookieName
}
// SetSessionCookieSecret sets the sessionCookieSecret.
func (s *Settings) SetSessionCookieSecret(sessionCookieSecret string) {
s.sessionCookieSecret = sessionCookieSecret
}
// SetAssetsPath sets the assetsPath.
func (s *Settings) SetAssetsPath(assetsPath string) {
s.assetsPath = assetsPath
}
// SetTemplatesPath sets the templatesPath.
func (s *Settings) SetTemplatesPath(templatesPath string) {
s.templatesPath = templatesPath
}
// SetVersion sets the version.
func (s *Settings) SetVersion(version string) {
s.version = version
}
// SetDbConnstring sets the dbConnString.
func (s *Settings) SetDbConnstring(connstring string) {
s.dbConnstring = connstring
}
// SetDbType sets the dbType.
func (s *Settings) SetDbType(dbType string) {
s.dbType = dbType
}
// SetDbIsSetUp sets the dbIsSetUp.
func (s *Settings) SetDbIsSetUp(is bool) {
s.dbIsSetUp = is
}
// EraseENVs attempts to clear environment vars pertaining to pcmt.
func (s *Settings) EraseENVs() error {
for _, v := range cleantgt {
err := os.Unsetenv(v)
if err != nil {
return err
}
}
return nil
}