Example #1
0
 protected virtual void ValidateParameters()
 {
     if (!AppliesToHelper.TryParseAppliesTo(AppliesTo, out _))
     {
         throw new AzPSArgumentException($"{nameof(AppliesTo)} must be a valid module name, a cmdlet name, or \"{ConfigFilter.GlobalAppliesTo}\"", nameof(AppliesTo));
     }
 }
Example #2
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));
        }