using System;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
///
/// Provides helper overloads to a .
///
public static class ConfigurationExtensions
{
///
/// Get a configuration value for the given key parts.
///
/// For example in order to get the value for this in a .git\config file:
///
///
/// [core]
/// bare = true
///
///
/// You would call:
///
///
/// bool isBare = repo.Config.Get<bool>(new []{ "core", "bare" }).Value;
///
///
///
/// The configuration value type
/// The configuration being worked with.
/// The key parts
/// The , or null if not set
public static ConfigurationEntry Get(this Configuration config, string[] keyParts)
{
Ensure.ArgumentNotNull(keyParts, "keyParts");
return config.Get(string.Join(".", keyParts));
}
///
/// Get a configuration value for the given key parts.
///
/// For example in order to get the value for this in a .git\config file:
///
///
/// [difftool "kdiff3"]
/// path = c:/Program Files/KDiff3/kdiff3.exe
///
///
/// You would call:
///
///
/// string where = repo.Config.Get<string>("difftool", "kdiff3", "path").Value;
///
///
///
/// The configuration value type
/// The configuration being worked with.
/// The first key part
/// The second key part
/// The third key part
/// The , or null if not set
public static ConfigurationEntry Get(this Configuration config, string firstKeyPart, string secondKeyPart, string thirdKeyPart)
{
Ensure.ArgumentNotNullOrEmptyString(firstKeyPart, "firstKeyPart");
Ensure.ArgumentNotNullOrEmptyString(secondKeyPart, "secondKeyPart");
Ensure.ArgumentNotNullOrEmptyString(thirdKeyPart, "thirdKeyPart");
return config.Get(new[] { firstKeyPart, secondKeyPart, thirdKeyPart });
}
///
/// Get a configuration value for the given key,
/// or if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The key
/// The default value if the key is not set.
/// The configuration value, or the default.
public static T GetValueOrDefault(this Configuration config, string key, T defaultValue = default(T))
{
return ValueOrDefault(config.Get(key), defaultValue);
}
///
/// Get a configuration value for the given key,
/// or if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The key.
/// The configuration file into which the key should be searched for.
/// The selector used to generate a default value if the key is not set.
/// The configuration value, or the default.
public static T GetValueOrDefault(this Configuration config, string key, ConfigurationLevel level, T defaultValue = default(T))
{
return ValueOrDefault(config.Get(key, level), defaultValue);
}
///
/// Get a configuration value for the given key parts,
/// or if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The key parts.
/// The default value if the key is not set.
/// The configuration value, or the default.
public static T GetValueOrDefault(this Configuration config, string[] keyParts, T defaultValue = default(T))
{
return ValueOrDefault(config.Get(keyParts), defaultValue);
}
///
/// Get a configuration value for the given key parts,
/// or if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The first key part.
/// The second key part.
/// The third key part.
/// The default value if the key is not set.
/// The configuration value, or the default.
public static T GetValueOrDefault(this Configuration config, string firstKeyPart, string secondKeyPart, string thirdKeyPart, T defaultValue = default(T))
{
return ValueOrDefault(config.Get(firstKeyPart, secondKeyPart, thirdKeyPart), defaultValue);
}
///
/// Get a configuration value for the given key,
/// or a value generated by
/// if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The key
/// The selector used to generate a default value if the key is not set.
/// The configuration value, or a generated default.
public static T GetValueOrDefault(this Configuration config, string key, Func defaultValueSelector)
{
return ValueOrDefault(config.Get(key), defaultValueSelector);
}
///
/// Get a configuration value for the given key,
/// or a value generated by
/// if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The key.
/// The configuration file into which the key should be searched for.
/// The selector used to generate a default value if the key is not set.
/// The configuration value, or a generated default.
public static T GetValueOrDefault(this Configuration config, string key, ConfigurationLevel level, Func defaultValueSelector)
{
return ValueOrDefault(config.Get(key, level), defaultValueSelector);
}
///
/// Get a configuration value for the given key parts,
/// or a value generated by
/// if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The key parts.
/// The selector used to generate a default value if the key is not set.
/// The configuration value, or a generated default.
public static T GetValueOrDefault(this Configuration config, string[] keyParts, Func defaultValueSelector)
{
return ValueOrDefault(config.Get(keyParts), defaultValueSelector);
}
///
/// Get a configuration value for the given key parts,
/// or a value generated by
/// if the key is not set.
///
/// The configuration value type.
/// The configuration being worked with.
/// The first key part.
/// The second key part.
/// The third key part.
/// The selector used to generate a default value if the key is not set.
/// The configuration value, or a generated default.
public static T GetValueOrDefault(this Configuration config, string firstKeyPart, string secondKeyPart, string thirdKeyPart, Func defaultValueSelector)
{
return ValueOrDefault(config.Get(firstKeyPart, secondKeyPart, thirdKeyPart), defaultValueSelector);
}
private static T ValueOrDefault(ConfigurationEntry value, T defaultValue)
{
return value == null ? defaultValue : value.Value;
}
private static T ValueOrDefault(ConfigurationEntry value, Func defaultValueSelector)
{
Ensure.ArgumentNotNull(defaultValueSelector, "defaultValueSelector");
return value == null
? defaultValueSelector()
: value.Value;
}
}
}