2020-04-23 08:47:27 +07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2022-02-28 10:15:01 +07:00
|
|
|
"fmt"
|
2020-04-23 08:47:27 +07:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2020-04-23 08:47:27 +07:00
|
|
|
)
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
func newValidateConfigCmd() (cmd *cobra.Command) {
|
|
|
|
cmd = &cobra.Command{
|
2022-02-28 10:15:01 +07:00
|
|
|
Use: "validate-config",
|
2021-08-03 16:55:21 +07:00
|
|
|
Short: "Check a configuration against the internal configuration validation mechanisms",
|
2022-02-28 10:15:01 +07:00
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: cmdValidateConfigRunE,
|
2021-08-03 16:55:21 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
cmdWithConfigFlags(cmd, false, []string{"config.yml"})
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
func cmdValidateConfigRunE(cmd *cobra.Command, _ []string) (err error) {
|
|
|
|
var (
|
|
|
|
configs []string
|
|
|
|
val *schema.StructValidator
|
|
|
|
)
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if configs, err = cmd.Flags().GetStringSlice("config"); err != nil {
|
|
|
|
return err
|
2021-08-03 16:55:21 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
config, val, err = loadConfig(configs, true, true)
|
2021-08-03 16:55:21 +07:00
|
|
|
if err != nil {
|
2022-02-28 10:15:01 +07:00
|
|
|
return fmt.Errorf("error occurred loading configuration: %v", err)
|
2021-08-03 16:55:21 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
switch {
|
|
|
|
case val.HasErrors():
|
|
|
|
fmt.Println("Configuration parsed and loaded with errors:")
|
|
|
|
fmt.Println("")
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
for _, err = range val.Errors() {
|
|
|
|
fmt.Printf("\t - %v\n", err)
|
|
|
|
}
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
fmt.Println("")
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if !val.HasWarnings() {
|
|
|
|
break
|
2020-04-23 08:47:27 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
fallthrough
|
|
|
|
case val.HasWarnings():
|
|
|
|
fmt.Println("Configuration parsed and loaded with warnings:")
|
|
|
|
fmt.Println("")
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
for _, err = range val.Warnings() {
|
|
|
|
fmt.Printf("\t - %v\n", err)
|
2020-04-23 08:47:27 +07:00
|
|
|
}
|
2021-08-03 16:55:21 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
fmt.Println("")
|
|
|
|
default:
|
|
|
|
fmt.Println("Configuration parsed and loaded successfully without errors.")
|
|
|
|
fmt.Println("")
|
2021-08-03 16:55:21 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
return nil
|
2020-04-23 08:47:27 +07:00
|
|
|
}
|