Exemple #1
0
 private void ClearUserLevelConfigByKey(ClearConfigOptions options)
 {
     _lock.EnterWriteLock();
     try
     {
         if (string.IsNullOrEmpty(options.AppliesTo))
         {
             IList <string> keysToClear = new List <string>();
             foreach (var appliesToSection in _root.GetChildren())
             {
                 if (appliesToSection.GetSection(options.Key).Exists())
                 {
                     keysToClear.Add(ConfigPathHelper.GetPathOfConfig(options.Key, appliesToSection.Key));
                 }
             }
             foreach (var key in keysToClear)
             {
                 _jsonConfigHelper.Clear(key);
             }
         }
         else
         {
             _jsonConfigHelper.Clear(ConfigPathHelper.GetPathOfConfig(options.Key, options.AppliesTo));
         }
     }
     finally
     {
         _lock.ExitWriteLock();
     }
     BuildConfig();
 }
Exemple #2
0
        /// <inheritdoc/>
        public void ClearConfig(ClearConfigOptions options)
        {
            _ = options ?? throw new AzPSArgumentNullException($"{nameof(options)} cannot be null.", nameof(options));

            bool clearAll = string.IsNullOrEmpty(options.Key);

            if (clearAll)
            {
                ClearAllConfigs(options);
            }
            else
            {
                ClearConfigByKey(options);
            }
        }
Exemple #3
0
        private void ClearProcessLevelAllConfigs(ClearConfigOptions options)
        {
            var configProvider = GetProcessLevelConfigProvider();

            if (string.IsNullOrEmpty(options.AppliesTo))
            {
                configProvider.UnsetAll();
            }
            else
            {
                foreach (var key in _configDefinitionMap.Keys)
                {
                    configProvider.Unset(ConfigPathHelper.GetPathOfConfig(key, options.AppliesTo));
                }
            }
        }
Exemple #4
0
        private void ClearAllConfigs(ClearConfigOptions options)
        {
            switch (options.Scope)
            {
            case ConfigScope.Process:
                ClearProcessLevelAllConfigs(options);
                break;

            case ConfigScope.CurrentUser:
                ClearUserLevelAllConfigs(options);
                break;

            default:
                throw new AzPSArgumentException($"[{options.Scope}] is not a valid scope when clearing configs.", nameof(options.Scope));
            }
            WriteDebug($"[ConfigManager] Cleared all the configs. Scope = [{options.Scope}].");
        }
Exemple #5
0
        private void ClearConfigByKey(ClearConfigOptions options)
        {
            if (!_configDefinitionMap.TryGetValue(options.Key, out _))
            {
                throw new AzPSArgumentException($"Config with key [{options.Key}] was not registered.", nameof(options.Key));
            }

            switch (options.Scope)
            {
            case ConfigScope.Process:
                ClearProcessLevelConfigByKey(options);
                break;

            case ConfigScope.CurrentUser:
                ClearUserLevelConfigByKey(options);
                break;
            }

            WriteDebug($"[ConfigManager] Cleared [{options.Key}]. Scope = [{options.Scope}], AppliesTo = [{options.AppliesTo}]");
        }
Exemple #6
0
        private void ClearConfigByKey(ClearConfigOptions options)
        {
            if (!_configDefinitionMap.TryGetValue(options.Key, out ConfigDefinition definition))
            {
                throw new AzPSArgumentException($"Config with key [{options.Key}] was not registered.", nameof(options.Key));
            }

            string path = ConfigPathHelper.GetPathOfConfig(definition.Key, options.AppliesTo);

            switch (options.Scope)
            {
            case ConfigScope.Process:
                GetProcessLevelConfigProvider().Unset(path);
                break;

            case ConfigScope.CurrentUser:
                ClearUserLevelConfigByKey(path);
                break;
            }

            WriteDebug($"[ConfigManager] Cleared [{options.Key}]. Scope = [{options.Scope}], AppliesTo = [{options.AppliesTo}]");
        }
Exemple #7
0
        private void ClearProcessLevelConfigByKey(ClearConfigOptions options)
        {
            var configProvider = GetProcessLevelConfigProvider();

            if (string.IsNullOrEmpty(options.AppliesTo))
            {
                // find config by key with any possible AppliesTo value
                var match = configProvider.Where(pair => ConfigPathHelper.ArePathAndKeyMatch(pair.Key, options.Key))
                            .Select(pair => pair.Key)
                            .ToList();
                match.ForEach(key =>
                {
                    configProvider.Unset(key);
                    _processLevelConfigs.Remove(key);
                });
            }
            else
            {
                string path = ConfigPathHelper.GetPathOfConfig(options.Key, options.AppliesTo);
                configProvider.Unset(path);
                _processLevelConfigs.Remove(path);
            }
        }
Exemple #8
0
 private void ClearUserLevelAllConfigs(ClearConfigOptions options)
 {
     _lock.EnterWriteLock();
     try
     {
         if (string.IsNullOrEmpty(options.AppliesTo))
         {
             _jsonConfigHelper.ClearAll();
         }
         else
         {
             foreach (var key in _configDefinitionMap.Keys)
             {
                 _jsonConfigHelper.Clear(ConfigPathHelper.GetPathOfConfig(key, options.AppliesTo));
             }
         }
     }
     finally
     {
         _lock.ExitWriteLock();
     }
     BuildConfig();
 }