public static string Validate(EtlOptions.EtlOptions options) { var report = ""; #region Sending Validation var moving = options.MoveOptions; if (!CreateDirectoryIfNotExist(moving.SourceDirectory)) { moving.SourceDirectory = "/Users/alex/Desktop/folder/source"; CreateDirectoryIfNotExist(moving.SourceDirectory); report += "Cannot open source directory, using default. "; } if (!CreateDirectoryIfNotExist(moving.TargetDirectory)) { moving.TargetDirectory = "/Users/alex/Desktop/folder/target"; report += "Cannot open target directory, using default. "; CreateDirectoryIfNotExist(moving.TargetDirectory); } if (!CreateDirectoryIfNotExist(moving.ArchiveDirectory)) { moving.ArchiveDirectory = "/Users/alex/Desktop/folder/target/archive"; report += "Cannot open target archive, using default. "; CreateDirectoryIfNotExist(moving.ArchiveDirectory); } if (!CreateDirectoryIfNotExist(moving.TargetSourceDirectory)) { moving.TargetSourceDirectory = "/Users/alex/Desktop/folder/target/source"; report += "Cannot open target source, using default. "; } #endregion #region Logging Validation var logging = options.LoggingOptions; if (!CreateFileIfNotExist(logging.LogPath)) { logging.LogPath = "/Users/alex/Desktop/folder/target/log.txt"; report += "Cannot open source log file, using default. "; CreateFileIfNotExist(logging.LogPath); } #endregion #region Encryption Validation var encryption = options.EncryptionOptions; if (!encryption.RandomKey && encryption.Key.Length != 16) { report += "Encryption key's length must be 16, using random key. "; } #endregion #region Archive Validation var archive = options.ArchiveOptions; // ReSharper disable once InvertIf if ((int)archive.CompressionLevel < 0 || (int)archive.CompressionLevel > 2) { archive.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal; report += "Compression level value can't be below zero and above 2, using default value. "; } #endregion return(report); }
public OptionManager(string path) { _defaultOptions = new EtlOptions.EtlOptions(); string options; try { using (var xmlReader = new StreamReader($"{path}/config.xml")) { options = xmlReader.ReadToEnd(); } _xml = new EtlXmlOptions(options); _xmlConfigured = true; Report = _xml.Report; Report += "Xml options loaded successfully. "; // logger.Log(Report); } catch { _xmlConfigured = false; } try { using (var jsonReader = new StreamReader($"{path}/appsettings.json")) { options = jsonReader.ReadToEnd(); } _json = new EtlJsonOptions(options); _jsonConfigured = true; Report = _json.Report; Report += "Json options loaded successfully. "; // logger.Log(Report); } catch { _jsonConfigured = false; } if (!_jsonConfigured && !_xmlConfigured) { Report = "Failed to load both of json and xml. Using default options and creating appsettings.json"; // logger.Log(Report); if (!File.Exists($"{path}/appsettings.json")) { var json = Converter.SerializeJson(_defaultOptions); Validator.CreateDirectoryIfNotExist(path); using var appsettings = new StreamWriter($"{path}/appsettings.json"); appsettings.Write(json); } if (!File.Exists($"{path}/config.xml")) { var xml = Converter.SerializeXml(_defaultOptions); Validator.CreateDirectoryIfNotExist(path); using var configXml = new StreamWriter($"{path}/config.xml"); configXml.Write(xml); } } }
private static Models.Options SeekForOption <T>(EtlOptions.EtlOptions options) { if (typeof(T) == typeof(EtlOptions.EtlOptions)) { return(options); } var name = typeof(T).Name; try { return(options.GetType().GetProperty(name)?.GetValue(options, null) as Models.Options); } catch { throw new NotImplementedException(); } }