Пример #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();
 }
Пример #2
0
 /// <summary>
 /// Migrates independent configs to the new config framework.
 /// </summary>
 /// <param name="profilePath">Path of session profile where old config files are stored.</param>
 internal void MigrateConfigs(string profilePath)
 {
     lock (_fsLock)
     {
         // Migrate data collection config
         string       dataCollectionConfigPath = Path.Combine(profilePath, AzurePSDataCollectionProfile.DefaultFileName);
         const string legacyConfigKey          = "enableAzureDataCollection";
         // Migrate only when:
         // 1. Old config file exists
         // 2. New config file does not exist
         if (DataStore.FileExists(dataCollectionConfigPath) && _configPathCandidates.All(path => !DataStore.FileExists(path)))
         {
             try
             {
                 string  json = DataStore.ReadFileAsText(dataCollectionConfigPath);
                 JObject root = JObject.Parse(json);
                 if (root.TryGetValue(legacyConfigKey, out JToken jToken))
                 {
                     bool enabled = ((bool)jToken);
                     new JsonConfigHelper(ConfigPath, DataStore).Update(ConfigPathHelper.GetPathOfConfig(ConfigKeys.EnableDataCollection), enabled);
                 }
             }
             catch (Exception)
             {
                 // do not throw for file IO exceptions
             }
         }
     }
 }
Пример #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));
                }
            }
        }
Пример #4
0
        /// <inheritdoc/>
        public ConfigData UpdateConfig(UpdateConfigOptions options)
        {
            if (options == null)
            {
                throw new AzPSArgumentNullException($"{nameof(options)} cannot be null when updating config.", nameof(options));
            }

            if (!_configDefinitionMap.TryGetValue(options.Key, out ConfigDefinition definition) || definition == null)
            {
                throw new AzPSArgumentException($"Config with key [{options.Key}] was not registered.", nameof(options.Key));
            }

            try
            {
                definition.Validate(options.Value);
            }
            catch (Exception e)
            {
                throw new AzPSArgumentException(e.Message, e);
            }

            if (AppliesToHelper.TryParseAppliesTo(options.AppliesTo, out var appliesTo) && !definition.CanApplyTo.Contains(appliesTo))
            {
                throw new AzPSArgumentException($"[{options.AppliesTo}] is not a valid value for AppliesTo - it doesn't match any of ({AppliesToHelper.FormatOptions(definition.CanApplyTo)}).", nameof(options.AppliesTo));
            }

            definition.Apply(options.Value);

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

            switch (options.Scope)
            {
            case ConfigScope.Process:
                SetProcessLevelConfig(path, options.Value);
                break;

            case ConfigScope.CurrentUser:
                SetUserLevelConfig(path, options.Value);
                break;
            }

            WriteDebug($"[ConfigManager] Updated [{options.Key}] to [{options.Value}]. Scope = [{options.Scope}], AppliesTo = [{options.AppliesTo}]");

            return(new ConfigData(definition, options.Value, options.Scope, options.AppliesTo));
        }
Пример #5
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);
        }
Пример #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}]");
        }
Пример #7
0
 /// <inheritdoc/>
 public void RegisterConfig(ConfigDefinition config)
 {
     // check if key already taken
     if (_configDefinitionMap.ContainsKey(config.Key))
     {
         if (_configDefinitionMap[config.Key] == config)
         {
             Debug.WriteLine($"Config with key [{config.Key}] was registered twice");
         }
         else
         {
             throw new AzPSArgumentException($"Duplicated config key. [{config.Key}] was already taken.", nameof(config.Key));
         }
         return;
     }
     // configure environment variable providers
     if (!string.IsNullOrEmpty(config.EnvironmentVariableName))
     {
         EnvironmentVariableToKeyMap[config.EnvironmentVariableName] = ConfigPathHelper.GetPathOfConfig(config.Key);
     }
     _configDefinitionMap[config.Key] = config;
 }
Пример #8
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);
            }
        }
Пример #9
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();
 }