protected void CheckPreviousBackup() { string backupFilePath = GetSystemSettingsBackupPath(); if (File.Exists(backupFilePath)) { // backup file exists DateTime date = File.GetLastWriteTime(backupFilePath); string message = string.Format(Resources.CommandBase_Message_SystemSettingsAreNotRestored, date); bool? answer = Prompt(message, threeState: true); // process depending on answer // true (Yes): restore the backup // false (No): do not restore the backup, but delete it // null (Cancel): do nothing if (answer.HasValue) { if (answer.Value) { // restore the backup JsonObjectData data = JsonObjectData.Load(backupFilePath, createIfNotExist: false); if (data != null) { SystemSettingsSwitcher switcher = this.ComponentFactory.CreateSystemSettingsSwitcher(this, null); switcher.Restore(data, systemSessionEnding: false); } } // delete backup file File.Delete(backupFilePath); } } return; }
protected void UpdateSettingsFile(Action <CommandSettings> updateSettings, string settingsFilePath = null) { // argument checks if (updateSettings == null) { throw new ArgumentNullException(nameof(updateSettings)); } int backupHistory = GetBackupHistoryFor(settingsFilePath); if (settingsFilePath == null) { settingsFilePath = EnsureSettingsFilePathSet(); } // load settings JsonObjectData settingsData = LoadSettingsFromFile(true, settingsFilePath); CommandSettings settings = this.ComponentFactory.CreateCommandSettings(settingsData); // update settings updateSettings(settings); settings.SaveToObjectData(settingsData, true); // save settings to the file Util.BackupAndSave(settingsFilePath, settingsData.Save, backupHistory); }
protected virtual bool HandleOption(string name, string value, CommandSettings settings) { // argument checks Debug.Assert(name != null); // value may null Debug.Assert(settings != null); // handle option bool handled = true; if (AreSameOptionNames(name, OptionNames.Help) || AreSameOptionNames(name, "?")) { this.Kind = ExecutionKind.ShowUsage; } else if (AreSameOptionNames(name, OptionNames.SettingsFile) || AreSameOptionNames(name, OptionNames.NoSettings)) { // ignore, it was already handled in CreateSettings() } else if (AreSameOptionNames(name, OptionNames.LogLevel)) { settings.LogLevel = (TraceLevel)Enum.Parse(typeof(TraceLevel), value); } else if (AreSameOptionNames(name, OptionNames.Culture)) { settings.Culture = new CultureInfo(value); } else if (AreSameOptionNames(name, OptionNames.Credential)) { settings.AddCredential(new CredentialSettings(new JsonObjectData(value))); } else if (AreSameOptionNames(name, OptionNames.MainListener)) { settings.Proxy.MainListener = new ListenerSettings(new JsonObjectData(value)); } else if (AreSameOptionNames(name, OptionNames.AdditionalListeners)) { settings.Proxy.AdditionalListeners = JsonObjectData.CreateArray(value).Select(v => new ListenerSettings(v.ExtractObjectValue())).ToArray(); } else if (AreSameOptionNames(name, OptionNames.RetryCount)) { settings.Proxy.RetryCount = int.Parse(value); } else if (AreSameOptionNames(name, OptionNames.EnableSystemSettingsSwitch)) { settings.SystemSettingsSwitcher.EnableSystemSettingsSwitch = bool.Parse(value); } else if (AreSameOptionNames(name, OptionNames.ActualProxy)) { settings.SystemSettingsSwitcher.ActualProxy = new ActualProxySettings(new JsonObjectData(value)); } else { handled = false; // not handled } return(handled); }
protected JsonObjectData LoadSettingsFromFile(bool createIfNotExist, string settingsFilePath = null) { // argument checks if (settingsFilePath == null) { settingsFilePath = EnsureSettingsFilePathSet(); } // load settings from the file return(JsonObjectData.Load(settingsFilePath, createIfNotExist)); }
public void EmptyHostData() { // ARRANGE int port = 80; string jsonText = $"{{\"Host\":\"\", \"Port\": {port}}}"; IObjectData data = new JsonObjectData(jsonText); // ACT, ASSERT Assert.Throws <FormatException>(() => { new ActualProxySettings(data); }); }
public void EmptyData() { // ARRANGE string jsonText = $"{{}}"; IObjectData data = new JsonObjectData(jsonText); // ACT ActualProxySettings target = new ActualProxySettings(data); // ASSERT Assert.Equal(ActualProxySettings.Defaults.Host, target.Host); Assert.Equal(ActualProxySettings.Defaults.Port, target.Port); Assert.Equal(null, target.ConfigurationScript); }
public void ConfigurationScriptData() { // ARRANGE string configurationScript = "file:///c:/test.pac"; string jsonText = $"{{\"ConfigurationScript\":\"{configurationScript}\"}}"; IObjectData data = new JsonObjectData(jsonText); // ACT ActualProxySettings target = new ActualProxySettings(data); // ASSERT Assert.Equal(null, target.Host); Assert.Equal(ActualProxySettings.Defaults.Port, target.Port); Assert.Equal(configurationScript, target.ConfigurationScript); }
public void AddressData() { // ARRANGE string host = "*****@*****.**"; int port = 80; string jsonText = $"{{\"Host\":\"{host}\", \"Port\": {port}}}"; IObjectData data = new JsonObjectData(jsonText); // ACT ActualProxySettings target = new ActualProxySettings(data); // ASSERT Assert.Equal(host, target.Host); Assert.Equal(port, target.Port); Assert.Equal(null, target.ConfigurationScript); }
public void SaveSystemSettingsBackup(SystemSettings backup) { // argument checks if (backup == null) { throw new ArgumentNullException(nameof(backup)); } // save the backup settings to the file string backupFilePath = GetSystemSettingsBackupPath(); JsonObjectData data = JsonObjectData.CreateEmpty(); backup.SaveToObjectData(data); data.Save(backupFilePath); return; }
protected void SaveSettingsToFile(CommandSettings settings, string settingsFilePath = null) { // argument checks if (settings == null) { throw new ArgumentNullException(nameof(settings)); } int backupHistory = GetBackupHistoryFor(settingsFilePath); if (settingsFilePath == null) { settingsFilePath = EnsureSettingsFilePathSet(); } // get object data to be saved JsonObjectData settingsData = JsonObjectData.CreateEmpty(); settings.SaveToObjectData(settingsData, true); // save settings to the file Util.BackupAndSave(settingsFilePath, settingsData.Save, backupHistory); }
private IObjectData GetBaseSettings(IDictionary <string, string> options) { // argument checks Debug.Assert(options != null); // create base settings IObjectData settingsData = null; bool noSetting = options.ContainsKey(OptionNames.NoSettings); if (noSetting == false) { // load the settings // find the settings file path string settingsFilePath; if (options.TryGetValue(OptionNames.SettingsFile, out settingsFilePath) == false) { // default location is %LOCALAPPDATA%\MAPE settingsFilePath = Path.Combine(GetMAPEAppDataFolder(), "Settings.json"); } // load settings from the config file try { settingsData = LoadSettingsFromFile(true, settingsFilePath); this.SettingsFilePath = settingsFilePath; } catch (Exception exception) { string message = string.Format(Resources.CommandBase_Message_FailToLoadSettingsFile, settingsFilePath, exception.Message); ShowErrorMessage(message); } } if (settingsData == null) { settingsData = JsonObjectData.CreateEmpty(); } return(settingsData); }