private bool TryReadConfigurationFile()

in src/Analyzer.Cli/CommandLineParser.cs [362:418]


        private bool TryReadConfigurationFile(FileInfo configurationFile, out ConfigurationDefinition config)
        {
            config = null;

            string configFilePath;
            if (configurationFile != null)
            {
                if (!configurationFile.Exists)
                {
                    // If a config file was specified in the command but doesn't exist, it's an error.
                    this.logger.LogError("Configuration file does not exist.");
                    return false;
                }
                configFilePath = configurationFile.FullName;
            }
            else
            {
                // Look for a config at the default location.
                // It's not required to exist, so if it doesn't, just return early.
                configFilePath = Path.Combine(AppContext.BaseDirectory, DefaultConfigFileName);
                if (!File.Exists(configFilePath))
                    return true;
            }

            // At this point, an existing config file was found.
            // If there are any problems reading it, it's an error.

            this.logger.LogInformation($"Configuration File: {configFilePath}");

            string configContents;
            try
            {
                configContents = File.ReadAllText(configFilePath);
            }
            catch (Exception e)
            {
                this.logger.LogError(e, "Unable to read configuration file.");
                return false;
            }

            if (string.IsNullOrWhiteSpace(configContents))
            {
                this.logger.LogError("Configuration is empty.");
                return false;
            }

            try
            {
                config = JsonConvert.DeserializeObject<ConfigurationDefinition>(configContents);
                return true;
            }
            catch (Exception e)
            {
                this.logger.LogError(e, "Failed to parse configuration file.");
                return false;
            }
        }