diff options
| author | 2022-05-16 14:13:19 +0200 | |
|---|---|---|
| committer | 2022-05-16 14:13:19 +0200 | |
| commit | b915a418115c7873913bafbc0337b6050c473d6a (patch) | |
| tree | c101d927d881d22d5320064ad8414919a646dda4 /cmd/gotosocial/common.go | |
| parent | [docs] Mention `AmbientCapabilities=CAP_NET_BIND_SERVICE` in example systemd ... (diff) | |
| download | gotosocial-b915a418115c7873913bafbc0337b6050c473d6a.tar.xz | |
[feature] Basic config validation (#562)
* add optional config validation
* clarify that host and protocol are required
* add validation for host and protocol
* pass prerunArgs as a struct (validate by default)
Diffstat (limited to 'cmd/gotosocial/common.go')
| -rw-r--r-- | cmd/gotosocial/common.go | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/cmd/gotosocial/common.go b/cmd/gotosocial/common.go index 0c65be657..48d56a7a2 100644 --- a/cmd/gotosocial/common.go +++ b/cmd/gotosocial/common.go @@ -28,15 +28,22 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/log" ) +type preRunArgs struct { + cmd *cobra.Command + skipValidation bool +} + // preRun should be run in the pre-run stage of every cobra command. // The goal here is to initialize the viper config store, and also read in // the config file (if present). // +// Config then undergoes basic validation if 'skipValidation' is not true. +// // The order of these is important: the init-config function reads the location // of the config file from the viper store so that it can be picked up by either // env vars or cli flag. -func preRun(cmd *cobra.Command) error { - if err := config.InitViper(cmd.Flags()); err != nil { +func preRun(a preRunArgs) error { + if err := config.InitViper(a.cmd.Flags()); err != nil { return fmt.Errorf("error initializing viper: %s", err) } @@ -44,6 +51,12 @@ func preRun(cmd *cobra.Command) error { return fmt.Errorf("error initializing config: %s", err) } + if !a.skipValidation { + if err := config.Validate(); err != nil { + return fmt.Errorf("invalid config: %s", err) + } + } + return nil } |
