Example #1
0
        private static Monitor.Configuration LoadConfiguration(bool production, string?configurationFilePath = null)
        {
            Monitor.Configuration?configuration = null;

            if (string.IsNullOrEmpty(configurationFilePath))
            {
                configuration = new Monitor.Configuration();

                var cloudBuildProdApplicationKey = Environment.GetEnvironmentVariable("CACHE_MONITOR_PROD_APPLICATION_KEY");
                if (string.IsNullOrEmpty(cloudBuildProdApplicationKey))
                {
                    throw new ArgumentException($"Please specify a configuration file or set the `CACHE_MONITOR_PROD_APPLICATION_KEY` environment variable to your application key");
                }

                var cloudBuildTestApplicationKey = Environment.GetEnvironmentVariable("CACHE_MONITOR_TEST_APPLICATION_KEY");
                if (string.IsNullOrEmpty(cloudBuildTestApplicationKey))
                {
                    throw new ArgumentException($"Please specify a configuration file or set the `CACHE_MONITOR_TEST_APPLICATION_KEY` environment variable to your application key");
                }

                App.Constants.MicrosoftTenantCredentials.AppKey = cloudBuildProdApplicationKey;
                App.Constants.PMETenantCredentials.AppKey       = cloudBuildTestApplicationKey;

                configuration.ReadOnly = !production;
                return(configuration);
            }

            if (!File.Exists(configurationFilePath))
            {
                throw new ArgumentException($"Configuration file does not exist at `{configurationFilePath}`",
                                            nameof(configurationFilePath));
            }

            using var stream = File.OpenText(configurationFilePath);
            var serializer = CreateSerializer();

            try
            {
                configuration = (Monitor.Configuration?)serializer.Deserialize(stream, typeof(Monitor.Configuration));
            }
            catch (JsonException exception)
            {
                throw new ArgumentException($"Invalid configuration file at `{configurationFilePath}`",
                                            nameof(configurationFilePath),
                                            exception);
            }

            if (configuration is null)
            {
                throw new ArgumentException($"Configuration file is empty at `{configurationFilePath}`",
                                            nameof(configurationFilePath));
            }

            if (configuration.ReadOnly == production)
            {
                throw new ArgumentException($"Command line arguments and configuration diverge on whether to run in production or test mode. CLI=[{production}], Configuration=[{configuration.ReadOnly}]");
            }

            return(configuration);
        }
Example #2
0
        private static Monitor.Configuration LoadConfiguration(bool production, string?configurationFilePath = null)
        {
            Monitor.Configuration?configuration = null;

            if (string.IsNullOrEmpty(configurationFilePath))
            {
                configuration = new Monitor.Configuration();

                var applicationKey = Environment.GetEnvironmentVariable("CACHE_MONITOR_APPLICATION_KEY");
                if (string.IsNullOrEmpty(applicationKey))
                {
                    throw new ArgumentException($"Please specify a configuration file or set the `CACHE_MONITOR_APPLICATION_KEY` environment variable to your application key");
                }
                configuration.AzureAppKey = applicationKey;

                configuration.TestMode = !production;
                return(configuration);
            }

            if (!File.Exists(configurationFilePath))
            {
                throw new ArgumentException($"Configuration file does not exist at `{configurationFilePath}`",
                                            nameof(configurationFilePath));
            }

            using var stream = File.OpenText(configurationFilePath);
            var serializer = CreateSerializer();

            try
            {
                configuration = (Monitor.Configuration?)serializer.Deserialize(stream, typeof(Monitor.Configuration));
            }
            catch (JsonException exception)
            {
                throw new ArgumentException($"Invalid configuration file at `{configurationFilePath}`",
                                            nameof(configurationFilePath),
                                            exception);
            }

            if (configuration is null)
            {
                throw new ArgumentException($"Configuration file is empty at `{configurationFilePath}`",
                                            nameof(configurationFilePath));
            }

            if (configuration.TestMode == production)
            {
                throw new ArgumentException($"TestMode is set to {configuration.TestMode}, but should not be equal to {production}");
            }

            return(configuration);
        }
Example #3
0
        private static void PersistConfiguration(Monitor.Configuration configuration, string configurationFilePath, bool force = false)
        {
            Contract.RequiresNotNullOrEmpty(configurationFilePath);

            if (File.Exists(configurationFilePath) && !force)
            {
                throw new ArgumentException($"File `{configurationFilePath}` already exists. Not overwriting it.",
                                            nameof(configurationFilePath));
            }

            using var stream = File.CreateText(configurationFilePath);
            var serializer = CreateSerializer();

            serializer.Serialize(stream, configuration);
        }