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