Skip to content

Instantly share code, notes, and snippets.

@grexican
Created September 9, 2016 16:49
Show Gist options
  • Save grexican/ff32c82e8efdff166d724813218a1142 to your computer and use it in GitHub Desktop.
Save grexican/ff32c82e8efdff166d724813218a1142 to your computer and use it in GitHub Desktop.
OptionalTextFileSettings.cs - For ServiceStack. If the file exists, it loads it and sets its settings. (e.g. use it for loading a local ".env" file, similar to dotenv for Ruby)
using System.Collections.Generic;
using System.IO;
namespace ServiceStack.Configuration
{
public class OptionalTextFileSettings : ISettings, IAppSettings
{
private readonly DictionarySettings _settings;
public OptionalTextFileSettings(string filePath, string delimiter = " ")
{
if(File.Exists(filePath))
_settings = new DictionarySettings(File.ReadAllText(filePath).ParseKeyValueText(delimiter));
else
_settings = new DictionarySettings();
}
public string Get(string key)
{
return _settings.Get(key);
}
public Dictionary<string, string> GetAll()
{
return _settings.GetAll();
}
public List<string> GetAllKeys()
{
return _settings.GetAllKeys();
}
public bool Exists(string key)
{
return _settings.Exists(key);
}
public void Set<T>(string key, T value)
{
_settings.Set<T>(key, value);
}
public string GetString(string name)
{
return _settings.GetString(name);
}
public IList<string> GetList(string key)
{
return _settings.GetList(key);
}
public IDictionary<string, string> GetDictionary(string key)
{
return _settings.GetDictionary(key);
}
public T Get<T>(string name)
{
return _settings.Get<T>(name);
}
public T Get<T>(string name, T defaultValue)
{
return _settings.Get<T>(name, defaultValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment