diff options
Diffstat (limited to 'internal/commands/validate.go')
| -rw-r--r-- | internal/commands/validate.go | 76 | 
1 files changed, 51 insertions, 25 deletions
diff --git a/internal/commands/validate.go b/internal/commands/validate.go index 73a9c65b9..54f119a92 100644 --- a/internal/commands/validate.go +++ b/internal/commands/validate.go @@ -1,40 +1,66 @@  package commands  import ( -	"fmt"  	"log"  	"os"  	"github.com/spf13/cobra"  	"github.com/authelia/authelia/internal/configuration" +	"github.com/authelia/authelia/internal/configuration/schema" +	"github.com/authelia/authelia/internal/configuration/validator" +	"github.com/authelia/authelia/internal/logging"  ) -// ValidateConfigCmd uses the internal configuration reader to validate the configuration. -var ValidateConfigCmd = &cobra.Command{ -	Use:   "validate-config [yaml]", -	Short: "Check a configuration against the internal configuration validation mechanisms.", -	Run: func(cobraCmd *cobra.Command, args []string) { -		configPath := args[0] -		if _, err := os.Stat(configPath); err != nil { -			log.Fatalf("Error Loading Configuration: %s\n", err) +func newValidateConfigCmd() (cmd *cobra.Command) { +	cmd = &cobra.Command{ +		Use:   "validate-config [yaml]", +		Short: "Check a configuration against the internal configuration validation mechanisms", +		Args:  cobra.MinimumNArgs(1), +		Run:   cmdValidateConfigRun, +	} + +	return cmd +} + +func cmdValidateConfigRun(_ *cobra.Command, args []string) { +	logger := logging.Logger() + +	configPath := args[0] +	if _, err := os.Stat(configPath); err != nil { +		logger.Fatalf("Error Loading Configuration: %v\n", err) +	} + +	val := schema.NewStructValidator() + +	keys, conf, err := configuration.Load(val, configuration.NewYAMLFileSource(configPath)) +	if err != nil { +		logger.Fatalf("Error occurred loading configuration: %v", err) +	} + +	validator.ValidateKeys(keys, configuration.DefaultEnvPrefix, val) +	validator.ValidateConfiguration(conf, val) + +	warnings := val.Warnings() +	errors := val.Errors() + +	if len(warnings) != 0 { +		logger.Warn("Warnings occurred while loading the configuration:") + +		for _, warn := range warnings { +			logger.Warnf("  %+v", warn)  		} +	} -		// TODO: Actually use the configuration to validate some providers like Notifier -		_, errs := configuration.Read(configPath) -		if len(errs) != 0 { -			str := "Errors" -			if len(errs) == 1 { -				str = "Error" -			} -			errors := "" -			for _, err := range errs { -				errors += fmt.Sprintf("\t%s\n", err.Error()) -			} -			log.Fatalf("%s occurred parsing configuration:\n%s", str, errors) -		} else { -			log.Println("Configuration parsed successfully without errors.") +	if len(errors) != 0 { +		logger.Error("Errors occurred while loading the configuration:") + +		for _, err := range errors { +			logger.Errorf("  %+v", err)  		} -	}, -	Args: cobra.MinimumNArgs(1), + +		logger.Fatal("Can't continue due to errors") +	} + +	log.Println("Configuration parsed successfully without errors.")  }  | 
