Skip to content

Commit

Permalink
Hacky way to get a sort of "comment" into the settings file and keep …
Browse files Browse the repository at this point in the history
…them there. :)
  • Loading branch information
Jaben Cargman committed Jul 12, 2018
1 parent 3c3a494 commit 920463d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/Papercut.Core/Domain/Settings/BaseSettingsStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public string this[[NotNull] string key]
{
if (key == null) throw new ArgumentNullException(nameof(key));

string value;
return this.CurrentSettings.TryGetValue(key, out value) ? value : null;
return this.CurrentSettings.TryGetValue(key, out var value) ? value : null;
}
set
{
Expand Down
7 changes: 3 additions & 4 deletions src/Papercut.Core/Domain/Settings/JsonSettingStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ namespace Papercut.Core.Domain.Settings
using System;
using System.Collections.Generic;
using System.IO;

using Newtonsoft.Json;
using System.Linq;

using Papercut.Core.Domain.Application;
using Papercut.Core.Infrastructure.Json;
Expand All @@ -32,7 +31,7 @@ public JsonSettingStore(IAppMeta appMeta)
{
this.SettingsFilePath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
appMeta.AppName + ".json");
$"{appMeta.AppName}.json");
}

protected string SettingsFilePath { get; set; }
Expand All @@ -48,7 +47,7 @@ public override void Save()
{
if (this.SettingsFilePath == null) return;

JsonHelpers.SaveJson(this.GetSettingSnapshot(), this.SettingsFilePath);
JsonHelpers.SaveJson(new SortedDictionary<string, string>(this.GetSettingSnapshot()), this.SettingsFilePath);
}
}
}
39 changes: 36 additions & 3 deletions src/Papercut.Core/Domain/Settings/ReadWriteValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Papercut.Core.Domain.Settings
using System;

using Papercut.Common.Extensions;
using Papercut.Common.Helper;
using Papercut.Core.Annotations;

public static class ReadWriteValueExtensions
Expand All @@ -34,6 +35,39 @@ public static void Set(
writeValue.Set(key, value.ToType<string>());
}

public static T GetOrSet<T>(this ISettingStore settings, string key, T defaultValue, string description)
{
return settings.GetOrSet(key, () => defaultValue, description);
}

public static T GetOrSet<T>(this ISettingStore settings, string key, Func<T> getDefaultValue, string description)
{
T returnValue;

string keyValue = settings.Get(key);

if (keyValue.IsNullOrWhiteSpace())
{
returnValue = getDefaultValue();

// set default
settings.Set(key, returnValue);
}
else
{
returnValue = keyValue.ToType<T>();
}

var descriptionKey = $"{key}_Description";

if (!description.IsNullOrWhiteSpace() && settings.Get(descriptionKey).IsNullOrWhiteSpace())
{
settings.Set(descriptionKey, $@"## {description}");
}

return returnValue;
}

public static T Get<T>(
[NotNull] this IReadValue<string> readValue,
[NotNull] string key,
Expand All @@ -44,7 +78,7 @@ public static T Get<T>(
if (getDefaultValue == null) throw new ArgumentNullException(nameof(getDefaultValue));

string value = readValue.Get(key);
return value.IsDefault() ? getDefaultValue() : value.ToType<T>();
return value.IsNullOrWhiteSpace() ? getDefaultValue() : value.ToType<T>();
}

public static T Get<T>(
Expand All @@ -55,8 +89,7 @@ public static T Get<T>(
if (readValue == null) throw new ArgumentNullException(nameof(readValue));
if (key == null) throw new ArgumentNullException(nameof(key));

string value = readValue.Get(key);
return value.IsDefault() ? defaultValue : value.ToType<T>();
return readValue.Get(key, () => defaultValue);
}
}
}
3 changes: 3 additions & 0 deletions src/Papercut.Core/Infrastructure/Json/JsonHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ namespace Papercut.Core.Infrastructure.Json
using System.Text;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static class JsonHelpers
{
private static readonly JsonSerializerSettings _serializationSettings =
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Include,
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
};

Expand Down
2 changes: 1 addition & 1 deletion src/Papercut.Module.WebUI/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public WebServer(ILifetimeScope scope, ISettingStore settingStore, ILogger logge
{
this.scope = scope;
this.logger = logger;
httpPort = settingStore.Get("HttpPort", DefaultHttpPort);
httpPort = settingStore.GetOrSet("HttpPort", DefaultHttpPort, $"The Http Web UI Server listening port (Defaults to {DefaultHttpPort}).");
}

public void Handle(PapercutServiceReadyEvent @event)
Expand Down
6 changes: 3 additions & 3 deletions src/Papercut.Service/Helpers/PapercutServiceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public class PapercutServiceSettings : ISettingsTyped

public string IP
{
get { return Settings.Get("IP", "Any"); }
get => Settings.GetOrSet("IP", "Any", "SMTP Server listening IP. 'Any' is the default and it means '0.0.0.0'.");
set { if (IP != value) Settings.Set("IP", value); }
}

public int Port
{
get { return Settings.Get("Port", 25); }
get => Settings.GetOrSet("Port", 25, "SMTP Server listening Port. Default is 25.");
set { if (Port != value) Settings.Set("Port", value); }
}

public string MessagePath
{
get { return Settings.Get<string>("MessagePath", @"%BaseDirectory%\Incoming"); }
get => Settings.GetOrSet<string>("MessagePath", @"%BaseDirectory%\Incoming", "Base path where incoming emails are written.");
set { if (MessagePath != value) Settings.Set("MessagePath", value); }
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/Papercut.Service/Papercut.Service.json
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@

{
"HttpPort_Description": "## The Http Web UI Server listening port (Defaults to 37408).",
"HttpPort": "37408",

"IP_Description": "## SMTP Server listening IP. 'Any' is the default and it means '0.0.0.0'.",
"IP": "Any",

"Port_Description": "## SMTP Server listening Port. Default is 25.",
"Port": "25"
}

0 comments on commit 920463d

Please sign in to comment.