Beispiel #1
0
 /// <summary>
 /// Gets a list of paths to check when getting a config value by key and invocation info.
 /// </summary>
 /// <param name="key">The key in the config definition.</param>
 /// <param name="invocation">Command invocation info, containing command name and module name.</param>
 public static IEnumerable <string> EnumerateConfigPaths(string key, InternalInvocationInfo invocation = null)
 {
     if (!string.IsNullOrEmpty(invocation?.CmdletName))
     {
         yield return(GetPathOfConfig(key, invocation.CmdletName));
     }
     if (!string.IsNullOrEmpty(invocation?.ModuleName))
     {
         yield return(GetPathOfConfig(key, invocation.ModuleName));
     }
     yield return(GetPathOfConfig(key));
 }
Beispiel #2
0
        internal object GetConfigValueInternal(string key, InternalInvocationInfo invocation)
        {
            _ = key ?? throw new AzPSArgumentNullException($"{nameof(key)} cannot be null.", nameof(key));
            if (!_configDefinitionMap.TryGetValue(key, out ConfigDefinition definition) || definition == null)
            {
                throw new AzPSArgumentException($"Config with key [{key}] was not registered.", nameof(key));
            }

            foreach (var path in ConfigPathHelper.EnumerateConfigPaths(key, invocation))
            {
                IConfigurationSection section = _root.GetSection(path);
                if (section.Exists())
                {
                    (object value, _) = GetConfigValueOrDefault(section, definition);
                    WriteDebug($"[ConfigManager] Got [{value}] from [{key}], Module = [{invocation?.ModuleName}], Cmdlet = [{invocation?.CmdletName}].");
                    return(value);
                }
            }

            WriteDebug($"[ConfigManager] Got nothing from [{key}], Module = [{invocation?.ModuleName}], Cmdlet = [{invocation?.CmdletName}]. Returning default value [{definition.DefaultValue}].");
            return(definition.DefaultValue);
        }
Beispiel #3
0
 internal T GetConfigValueInternal <T>(string key, InternalInvocationInfo invocation) => (T)GetConfigValueInternal(key, invocation);