EasyConfigurationExtensions.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using EasyDevCore.Common;
  2. using Microsoft.Extensions.Configuration;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace EasyDevCore.Configuration.Configuration
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public static class EasyConfigurationExtensions
  14. {
  15. /// <summary>
  16. /// Resolves the macro value (like pattern {Property}) from config file.
  17. /// </summary>
  18. /// <param name="config">The configuration.</param>
  19. /// <param name="configSecrets">The configuration secrets.</param>
  20. /// <param name="sectionName">Name of the section.</param>
  21. /// <param name="resolveChildren">if set to <c>true</c> [resolve children].</param>
  22. /// <returns></returns>
  23. public static IConfiguration ResolveConfigSecrets(this IConfiguration config, EasyConfiguration configSecrets, string sectionName = "", bool resolveChildren = true)
  24. {
  25. var section = config;
  26. if (!string.IsNullOrWhiteSpace(sectionName))
  27. {
  28. section = config.GetSection(sectionName);
  29. }
  30. var list = resolveChildren ? section.GetAllChildren() : section.GetChildren();
  31. foreach(var item in list)
  32. {
  33. var value = item.Value;
  34. var found = BindSecretItem(configSecrets, ref value);
  35. if (found)
  36. {
  37. item.Value = value;
  38. }
  39. }
  40. return config;
  41. }
  42. /// <summary>
  43. /// Binds the secret item.
  44. /// </summary>
  45. /// <param name="configSecrets">The configuration secrets.</param>
  46. /// <param name="configString">The configuration string.</param>
  47. /// <returns></returns>
  48. private static bool BindSecretItem(EasyConfiguration configSecrets, ref string configString)
  49. {
  50. var decodedValue = configString;
  51. var found = false;
  52. if (configString == null) return false;
  53. configString.RegexGroupMatch("{(?<m>[^{}]+)}", (m) =>
  54. {
  55. if (m.Value == "m" && configSecrets.Exists(m.Value))
  56. {
  57. found = true;
  58. decodedValue = decodedValue.Replace("{{" + m.Value + "}}", (configSecrets[m.Value] ?? "?Null?").ToString(), StringComparison.InvariantCultureIgnoreCase);
  59. }
  60. });
  61. if (found)
  62. {
  63. configString = decodedValue;
  64. }
  65. return found;
  66. }
  67. /// <summary>
  68. /// Resolves the secret item.
  69. /// </summary>
  70. /// <param name="configSecrets">The configuration secrets.</param>
  71. /// <param name="configString">The configuration string.</param>
  72. /// <returns></returns>
  73. public static string ResolveSecretItem(this EasyConfiguration configSecrets, string configString)
  74. {
  75. BindSecretItem(configSecrets, ref configString);
  76. return configString;
  77. }
  78. }
  79. }