static void InitializeImplementation(string raw, ConfigurationFormat format, string subkey) { lock (initializationLock) { if (alreadyConfigured) { throw new AlreadyConfiguredException(); } var config = format is ConfigurationFormat.Json is var j ? loadJsonConfig() : format is ConfigurationFormat.Xml is var x ? loadXmlConfig() : throw new ArgumentException(configFormatError); InitializeComponents(config); alreadyConfigured = true; } IConfig loadXmlConfig() => new XmlConfig(subkey.IsNullOrWhiteSpace() ? XElement.Parse(raw) : XElement.Parse(raw).Element(subkey)); IConfig loadJsonConfig() => new JsonConfig((JObject)(subkey.IsNullOrWhiteSpace() ? JToken.Parse(raw) : JToken.Parse(raw).SelectToken(subkey))); }
/// <summary> /// Configures how components are initialized by specifying the locations of required resources files. /// </summary> /// <param name="stream">A stream containing a JSON or XML document holding the desired configuration information.</param> /// <param name="format">Specifies the format of the document containing the configuration information.</param> /// <param name="subkey"> /// Specifies the format of the stream containing the configuration information. Specifies the name of the child object or nested element containing the resource configuration information. For /// example, if the configuration source is a "classical" App.config file, such as that used by the LASI.App WPF application, the value of this argument would be "AppSettings". /// </param> public static void Initialize(Stream stream, ConfigurationFormat format, string subkey) { using (var reader = new StreamReader(stream)) { InitializeImplementation(reader.ReadToEnd(), format, subkey); } }
public IConfigurationGenerator FromConfigurationFormat(ConfigurationFormat configurationFormat) { switch (configurationFormat) { case ConfigurationFormat.Config: return(_appConfigGenerator); case ConfigurationFormat.Json: return(_jsonConfigGenerator); default: throw new ArgumentOutOfRangeException(nameof(configurationFormat)); } }
public async Task HashedControlPasswordGetsCleared() { // Arrange var format = new ConfigurationFormat(); var configurer = new LineByLineConfigurer(Target, format); var settings = Environment.BuildSettings(); settings.TorSettings.HashedControlPassword = "******"; await configurer.ApplySettings(Tool, settings); var configurationBefore = File.ReadAllText(Tool.ConfigurationPath); // Act settings.TorSettings.HashedControlPassword = null; await configurer.ApplySettings(Tool, settings); // Assert var configurationAfter = File.ReadAllText(Tool.ConfigurationPath); Assert.Contains("HashedControlPassword NOT_A_REAL_HASH", configurationBefore); Assert.DoesNotContain("HashedControlPassword", configurationAfter); }
private static void LoadConfiguration( IConfigurationBuilder configurationBuilder, string domain, string environment, ConfigurationFormat format) { string[] configFiles = { $"appsettings.{domain}.{Enum.GetName(typeof(ConfigurationFormat), format)?.ToLower()}", $"appsettings.{domain}.{environment}.{Enum.GetName(typeof(ConfigurationFormat), format)?.ToLower()}" }; foreach (var configFile in configFiles.Where(ConfigurationExists)) { switch (format) { case ConfigurationFormat.Json: ConfigurationWithDomainsBuilder?.Sources .Add(GetSource <JsonConfigurationSource>(configFile)); configurationBuilder.Sources .Add(GetSource <JsonConfigurationSource>(configFile)); break; case ConfigurationFormat.Yaml: case ConfigurationFormat.Yml: ConfigurationWithDomainsBuilder?.Sources .Add(GetSource <YamlConfigurationSource>(configFile)); configurationBuilder.Sources .Add(GetSource <YamlConfigurationSource>(configFile)); break; default: throw new ArgumentOutOfRangeException(nameof(format), format, null); } } }
/// <summary> /// Configures how components are initialized by specifying the locations of required resource files. /// </summary> /// <param name="configUrl">The location of an XML or JSON document containing configuration information.</param> /// <param name="format">Specifies the format of the document containing the configuration information.</param> /// <param name="subkey"> /// Specifies the name of the child object or nested element containing the resource configuration information. For example, if the configure source is a "classical" App.config file, such as /// that used by the LASI.App WPF application, the value of this argument would be "AppSettings". /// </param> public static void Initialize(string configUrl, ConfigurationFormat format, string subkey) { var isUrl = Uri.IsWellFormedUriString(configUrl, UriKind.Absolute); var isFsPath = File.Exists(Path.GetFullPath(configUrl)); if (!isUrl && !isFsPath) { throw new ArgumentException("The specified url is neither an accessible file system location nor a valid Uri", nameof(configUrl)); } Initialize( stream: isUrl?WebRequest.CreateHttp(configUrl).GetRequestStream() : isFsPath ? new FileStream( options: FileOptions.Asynchronous, path: Path.GetFullPath(configUrl), mode: FileMode.Open, access: FileAccess.Read, share: FileShare.Read, bufferSize: 1024 ) : null, format: format, subkey: subkey ); }
public void SetConfigurationFormat(ProjectBuilder project, ConfigurationFormat configurationFormat) { project.ConfigurationFormat = configurationFormat; }
public void SetConfigurationFormat(ConfigurationFormat configurationFormat) => SetConfigurationFormat(_solutionDriver.DefaultProject, configurationFormat);
/// <summary> /// Configures how components are initialized by specifying the locations of required resources files. /// </summary> /// <param name="stream">A stream containing a JSON or XML document holding the desired configuration information.</param> /// <param name="format">Specifies the format of the document containing the configuration information.</param> public static void Initialize(Stream stream, ConfigurationFormat format) => Initialize(stream, format, null);
/// <summary> /// Configures how components are initialized by specifying the locations of required resource files. /// </summary> /// <param name="configUrl">The location of an XML or JSON document containing configuration information.</param> /// <param name="format">Specifies the format of the document containing the configuration information.</param> public static void Initialize(string configUrl, ConfigurationFormat format) => Initialize(configUrl, format, null);