[feature] support nested configuration files, and setting ALL configuration variables by CLI and env (#4109)

This updates our configuration code generator to now also include map marshal and unmarshalers. So we now have much more control over how things get read from pflags, and stored / read from viper configuration. This allows us to set ALL configuration variables by CLI and environment now, AND support nested configuration files. e.g.

```yaml
advanced:
    scraper-deterrence = true

http-client:
    allow-ips = ["127.0.0.1"]
```

is the same as

```yaml
advanced-scraper-deterrence = true

http-client-allow-ips = ["127.0.0.1"]
```

This also starts cleaning up of our jumbled Configuration{} type by moving the advanced configuration options into their own nested structs, also as a way to show what it's capable of. It's worth noting however that nesting only works if the Go types are nested too (as this is how we hint to our code generator to generate the necessary flattening code :p).

closes #3195

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4109
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2025-05-06 15:51:45 +00:00 committed by kim
commit 6acf56cde9
30 changed files with 4764 additions and 1184 deletions

View file

@ -32,11 +32,8 @@ var Config action.GTSAction = func(ctx context.Context) (err error) {
// Marshal configuration to a raw JSON map
config.Config(func(cfg *config.Configuration) {
raw, err = cfg.MarshalMap()
raw = cfg.MarshalMap()
})
if err != nil {
return err
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

View file

@ -43,16 +43,16 @@ type preRunArgs struct {
// env vars or cli flag.
func preRun(a preRunArgs) error {
if err := config.BindFlags(a.cmd); err != nil {
return fmt.Errorf("error binding flags: %s", err)
return fmt.Errorf("error binding flags: %w", err)
}
if err := config.Reload(); err != nil {
return fmt.Errorf("error reloading config: %s", err)
if err := config.LoadConfigFile(); err != nil {
return fmt.Errorf("error loading config file: %w", err)
}
if !a.skipValidation {
if err := config.Validate(); err != nil {
return fmt.Errorf("invalid config: %s", err)
return fmt.Errorf("invalid config: %w", err)
}
}

View file

@ -19,7 +19,6 @@ package main
import (
configaction "code.superseriousbusiness.org/gotosocial/cmd/gotosocial/action/debug/config"
"code.superseriousbusiness.org/gotosocial/internal/config"
"github.com/spf13/cobra"
)
@ -39,7 +38,6 @@ func debugCommands() *cobra.Command {
return run(cmd.Context(), configaction.Config)
},
}
config.AddServerFlags(debugConfigCmd)
debugCmd.AddCommand(debugConfigCmd)
return debugCmd
}

View file

@ -23,10 +23,9 @@ import (
godebug "runtime/debug"
"strings"
"github.com/spf13/cobra"
_ "code.superseriousbusiness.org/gotosocial/docs"
"code.superseriousbusiness.org/gotosocial/internal/config"
"github.com/spf13/cobra"
)
// Version is the version of GoToSocial being used.
@ -41,24 +40,18 @@ func main() {
// override version in config store
config.SetSoftwareVersion(version)
// instantiate the root command
rootCmd := &cobra.Command{
Use: "gotosocial",
Short: "GoToSocial - a fediverse social media server",
Long: "GoToSocial - a fediverse social media server\n\nFor help, see: https://docs.gotosocial.org.\n\nCode: https://codeberg.org/superseriousbusiness/gotosocial",
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// before running any other cmd funcs, we must load config-path
return config.LoadEarlyFlags(cmd)
},
SilenceErrors: true,
SilenceUsage: true,
}
rootCmd := new(cobra.Command)
rootCmd.Use = "gotosocial"
rootCmd.Short = "GoToSocial - a fediverse social media server"
rootCmd.Long = "GoToSocial - a fediverse social media server\n\nFor help, see: https://docs.gotosocial.org.\n\nCode: https://codeberg.org/superseriousbusiness/gotosocial"
rootCmd.Version = version
rootCmd.SilenceErrors = true
rootCmd.SilenceUsage = true
// attach global flags to the root command so that they can be accessed from any subcommand
config.AddGlobalFlags(rootCmd)
// Register global flags with root.
config.RegisterGlobalFlags(rootCmd)
// add subcommands
// Add subcommands with their flags.
rootCmd.AddCommand(serverCommands())
rootCmd.AddCommand(debugCommands())
rootCmd.AddCommand(adminCommands())
@ -70,7 +63,7 @@ func main() {
log.Fatal("gotosocial must be built and run with the DEBUG enviroment variable set to enable and access testrig")
}
// run
// Run the prepared root command.
if err := rootCmd.Execute(); err != nil {
log.Fatalf("error executing command: %s", err)
}

View file

@ -19,7 +19,6 @@ package main
import (
"code.superseriousbusiness.org/gotosocial/cmd/gotosocial/action/server"
"code.superseriousbusiness.org/gotosocial/internal/config"
"github.com/spf13/cobra"
)
@ -39,7 +38,6 @@ func serverCommands() *cobra.Command {
return run(cmd.Context(), server.Start)
},
}
config.AddServerFlags(serverStartCmd)
serverCmd.AddCommand(serverStartCmd)
serverMaintenanceCmd := &cobra.Command{
@ -52,7 +50,6 @@ func serverCommands() *cobra.Command {
return run(cmd.Context(), server.Maintenance)
},
}
config.AddServerFlags(serverMaintenanceCmd)
serverCmd.AddCommand(serverMaintenanceCmd)
return serverCmd