Straightened out the configuration file processing and introduced command-line argument processing
This commit is contained in:
@@ -12,10 +12,31 @@ package config
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
argparse "github.com/alexflint/go-arg"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// AMSTERDAM_VERSION contains the version number of Amsterdam.
|
||||
const AMSTERDAM_VERSION = "0.0.1"
|
||||
|
||||
// AmCLI is the command-line interface arguments structure.
|
||||
type AmCLI struct {
|
||||
ConfigFile string `arg:"-C,--config" help:"Location of the configuration file."`
|
||||
}
|
||||
|
||||
// Description (from argparse.Described) returns the description string for the application.
|
||||
func (*AmCLI) Description() string {
|
||||
return "Amsterdam Web Communities System Server"
|
||||
}
|
||||
|
||||
// Version (from argparse.Versioned) returns the version number string for the application.
|
||||
func (*AmCLI) Version() string {
|
||||
return "Amsterdam " + AMSTERDAM_VERSION
|
||||
}
|
||||
|
||||
// AmConfig holds the configuration of the application as read from YAML.
|
||||
type AmConfig struct {
|
||||
Site struct {
|
||||
@@ -29,6 +50,9 @@ type AmConfig struct {
|
||||
//go:embed default.yaml
|
||||
var defaultConfigData []byte
|
||||
|
||||
// defaultConfig holds the default configuration data.
|
||||
var defaultConfig AmConfig
|
||||
|
||||
// GlobalConfig holds the global configuration.
|
||||
var GlobalConfig AmConfig
|
||||
|
||||
@@ -40,3 +64,49 @@ func init() {
|
||||
}
|
||||
GlobalConfig = defaultConfig
|
||||
}
|
||||
|
||||
/* overlayString is a helper that takes a loaded or defaulted string and returns it.
|
||||
* Parameters:
|
||||
* loaded - The string loaded from a configuration file.
|
||||
* defaulted - The default value of this string.
|
||||
* Returns:
|
||||
* loaded if it's not empty, otherwise defaulted.
|
||||
*/
|
||||
func overlayString(loaded string, defaulted string) string {
|
||||
if loaded == "" {
|
||||
return defaulted
|
||||
}
|
||||
return loaded
|
||||
}
|
||||
|
||||
/* overlayConfig takes two configuration structures and overlays them to create the third.
|
||||
* Parameters:
|
||||
* dest - Points to the destination copnfiguration structure.
|
||||
* loaded - Points to the loaded configuration structure.
|
||||
* defaults - Points to the default configuration structure.
|
||||
*/
|
||||
func overlayConfig(dest *AmConfig, loaded *AmConfig, defaults *AmConfig) {
|
||||
dest.Site.Title = overlayString(loaded.Site.Title, defaults.Site.Title)
|
||||
dest.Rendering.TemplateDir = overlayString(loaded.Rendering.TemplateDir, defaults.Rendering.TemplateDir)
|
||||
}
|
||||
|
||||
// SetupConfig loads the command line arguments, loads the config file, and prepares GlobalConfig.
|
||||
func SetupConfig() {
|
||||
var args AmCLI
|
||||
argparse.MustParse(&args)
|
||||
|
||||
if args.ConfigFile != "" {
|
||||
// load the data and use it to unmarshal the loaded configuration
|
||||
data, err := os.ReadFile(args.ConfigFile)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("unable to load configuration file %s: %v", args.ConfigFile, err))
|
||||
}
|
||||
var loadedConfig AmConfig
|
||||
if err = yaml.Unmarshal(data, &loadedConfig); err != nil {
|
||||
panic(fmt.Sprintf("unable to load configuration file %s: %v", args.ConfigFile, err))
|
||||
}
|
||||
overlayConfig(&GlobalConfig, &loadedConfig, &defaultConfig)
|
||||
} else {
|
||||
GlobalConfig = defaultConfig // just copy over the defaults
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ require (
|
||||
|
||||
require (
|
||||
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
|
||||
github.com/alexflint/go-arg v1.6.0 // indirect
|
||||
github.com/alexflint/go-scalar v1.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
|
||||
@@ -2,6 +2,10 @@ github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 h1:sR+/8Yb4s
|
||||
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
|
||||
github.com/CloudyKit/jet/v6 v6.3.1 h1:6IAo5Cx21xrHVaR8zzXN5gJatKV/wO7Nf6bfCnCSbUw=
|
||||
github.com/CloudyKit/jet/v6 v6.3.1/go.mod h1:lf8ksdNsxZt7/yH/3n4vJQWA9RUq4wpaHtArHhGVMOw=
|
||||
github.com/alexflint/go-arg v1.6.0 h1:wPP9TwTPO54fUVQl4nZoxbFfKCcy5E6HBCumj1XVRSo=
|
||||
github.com/alexflint/go-arg v1.6.0/go.mod h1:A7vTJzvjoaSTypg4biM5uYNTkJ27SkNTArtYXnlqVO8=
|
||||
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
|
||||
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -18,6 +22,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
@@ -35,6 +36,7 @@ func setupEcho() *echo.Echo {
|
||||
|
||||
// main is Ye Olde Main Function.
|
||||
func main() {
|
||||
config.SetupConfig()
|
||||
ui.SetupTemplates()
|
||||
e := setupEcho()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user