Esempio n. 1
0
        public bool TryLoadAndValidateConfig(out Config?config)
        {
            config = null;

            if (!_configfile.IsExisting)
            {
                _log.Info("No bookgen.json config found.");
                return(false);
            }

            config = _configfile.DeserializeJson <Config>(_log);

            if (config == null)
            {
                _log.Critical("bookgen.json deserialize error. Invalid config file");
                return(false);
            }

            if (config.Version < Program.CurrentState.ConfigVersion)
            {
                _configfile.CreateBackup(_log);
                config.UpgradeTo(Program.CurrentState.ConfigVersion);
                _configfile.SerializeJson(config, _log, true);
                _log.Info("Configuration file migrated to new version.");
                _log.Info("Review configuration then run program again");
                return(false);
            }

            ConfigValidator validator = new ConfigValidator(config, _workdir);

            validator.Validate();

            if (!validator.IsValid)
            {
                _log.Warning("Errors found in configuration: ");
                foreach (var error in validator.Errors)
                {
                    _log.Warning(error);
                }
                return(false);
            }

            return(true);
        }