Ejemplo n.º 1
0
        static bool CanBeResolvedFromEnvironmentSettings(EnvironmentSettings environmentSettings, Parameter parameter)
        {
            var name = parameter.Name;

            if (parameter.AllowAppSetting && environmentSettings.HasAppSetting(name))
            {
                return(true);
            }

            if (parameter.AllowConnectionString && environmentSettings.HasConnectionString(name))
            {
                return(true);
            }

            if (parameter.AllowEnvironmentVariable && environmentSettings.HasEnvironmentVariable(name))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        static void ResolveParametersFromEnvironmentSettings(EnvironmentSettings environmentSettings,
                                                             ICommand commandInstance, HashSet <Parameter> setParameters, IEnumerable <Parameter> parameters)
        {
            foreach (var parameter in parameters.Where(p => p.AllowAppSetting && !setParameters.Contains(p)))
            {
                if (!environmentSettings.HasAppSetting(parameter.Name))
                {
                    continue;
                }

                var appSettingValue = environmentSettings.GetAppSetting(parameter.Name);

                SetParameter(commandInstance, setParameters, parameter, appSettingValue);
            }

            foreach (var parameter in parameters.Where(p => p.AllowConnectionString && !setParameters.Contains(p)))
            {
                if (!environmentSettings.HasConnectionString(parameter.Name))
                {
                    continue;
                }

                var appSettingValue = environmentSettings.GetConnectionString(parameter.Name);

                SetParameter(commandInstance, setParameters, parameter, appSettingValue);
            }

            foreach (var parameter in parameters.Where(p => p.AllowEnvironmentVariable && !setParameters.Contains(p)))
            {
                if (!environmentSettings.HasEnvironmentVariable(parameter.Name))
                {
                    continue;
                }

                var appSettingValue = environmentSettings.GetEnvironmentVariable(parameter.Name);

                SetParameter(commandInstance, setParameters, parameter, appSettingValue);
            }
        }
Ejemplo n.º 3
0
        void InnerInvoke(IEnumerable <Switch> switches, EnvironmentSettings environmentSettings)
        {
            var commandInstance = _commandInstance;

            var requiredParametersMissing = Parameters
                                            .Where(p => !p.Optional &&
                                                   !p.HasDefaultValue &&
                                                   !CanBeResolvedFromSwitches(switches, p) &&
                                                   !CanBeResolvedFromEnvironmentSettings(environmentSettings, p))
                                            .ToList();

            var optionalParamtersNotSpecified = Parameters
                                                .Where(p => p.Optional &&
                                                       !CanBeResolvedFromSwitches(switches, p) &&
                                                       !CanBeResolvedFromEnvironmentSettings(environmentSettings, p))
                                                .ToList();

            if (requiredParametersMissing.Any())
            {
                var requiredParametersMissingString = string.Join(Environment.NewLine,
                                                                  requiredParametersMissing.Select(p => $"    {_settings.SwitchPrefix}{p.Name} - {p.DescriptionText}"));

                var text = $@"The following required parameters are missing:

{requiredParametersMissingString}";

                if (optionalParamtersNotSpecified.Any())
                {
                    var optionalParamtersNotSpecifiedString = string.Join(Environment.NewLine,
                                                                          optionalParamtersNotSpecified.Select(p => $"    {_settings.SwitchPrefix}{p.Name} - {p.DescriptionText}"));

                    var moreText = $@"The following optional parameters are also available:

{optionalParamtersNotSpecifiedString}";

                    throw new GoCommandoException(string.Concat(
                                                      text,
                                                      Environment.NewLine,
                                                      Environment.NewLine,
                                                      moreText
                                                      ));
                }

                throw new GoCommandoException(text);
            }

            var switchesWithoutMathingParameter = switches
                                                  .Where(s => !Parameters.Any(p => p.MatchesKey(s.Key)))
                                                  .ToList();

            if (switchesWithoutMathingParameter.Any())
            {
                var switchesWithoutMathingParameterString = string.Join(Environment.NewLine,
                                                                        switchesWithoutMathingParameter.Select(p => p.Value != null
                        ? $"    {_settings.SwitchPrefix}{p.Key} = {p.Value}"
                        : $"    {_settings.SwitchPrefix}{p.Key}"));

                throw new GoCommandoException(
                          $@"The following switches do not have a corresponding parameter:

{switchesWithoutMathingParameterString}");
            }

            var setParameters = new HashSet <Parameter>();

            ResolveParametersFromSwitches(switches, commandInstance, setParameters);

            ResolveParametersFromEnvironmentSettings(environmentSettings, commandInstance, setParameters, Parameters);

            ResolveParametersWithDefaultValues(setParameters, commandInstance);

            commandInstance.Run();
        }