From 6acf56cde9fde972cf7c78f15d00ade262752c19 Mon Sep 17 00:00:00 2001 From: kim Date: Tue, 6 May 2025 15:51:45 +0000 Subject: [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 Co-committed-by: kim --- cmd/gotosocial/action/debug/config/config.go | 5 +---- cmd/gotosocial/common.go | 8 +++---- cmd/gotosocial/debug.go | 2 -- cmd/gotosocial/main.go | 31 +++++++++++----------------- cmd/gotosocial/server.go | 3 --- 5 files changed, 17 insertions(+), 32 deletions(-) (limited to 'cmd') diff --git a/cmd/gotosocial/action/debug/config/config.go b/cmd/gotosocial/action/debug/config/config.go index bc2c379ca..f79f5234d 100644 --- a/cmd/gotosocial/action/debug/config/config.go +++ b/cmd/gotosocial/action/debug/config/config.go @@ -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("", " ") diff --git a/cmd/gotosocial/common.go b/cmd/gotosocial/common.go index bc27f0147..bfa5b656d 100644 --- a/cmd/gotosocial/common.go +++ b/cmd/gotosocial/common.go @@ -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) } } diff --git a/cmd/gotosocial/debug.go b/cmd/gotosocial/debug.go index 4b819e8a6..f519e63bd 100644 --- a/cmd/gotosocial/debug.go +++ b/cmd/gotosocial/debug.go @@ -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 } diff --git a/cmd/gotosocial/main.go b/cmd/gotosocial/main.go index a79b6a75c..5124147a7 100644 --- a/cmd/gotosocial/main.go +++ b/cmd/gotosocial/main.go @@ -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) } diff --git a/cmd/gotosocial/server.go b/cmd/gotosocial/server.go index 8b316aca8..859292402 100644 --- a/cmd/gotosocial/server.go +++ b/cmd/gotosocial/server.go @@ -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 -- cgit v1.2.3