public void EnableFeature(ChocolateyGuiConfiguration configuration)
        {
            var featureProperty = GetProperty(configuration.FeatureCommand.Name, true);
            var featureValue    = GetPropertyValue <bool>(_appConfiguration, featureProperty);

            if (!featureValue)
            {
                featureProperty.SetValue(_appConfiguration, true);
                UpdateSettings(_appConfiguration);
                Logger.Warning(Resources.FeatureCommand_EnabledWarning.format_with(configuration.FeatureCommand.Name));
            }
            else
            {
                Logger.Warning(Resources.FeatureCommand_NoChangeMessage);
            }
        }
Ejemplo n.º 2
0
        public virtual void HandleValidation(ChocolateyGuiConfiguration configuration)
        {
            if (configuration.ConfigCommand.Command != ConfigCommandType.List &&
                string.IsNullOrWhiteSpace(configuration.ConfigCommand.Name))
            {
                Logger.Error(Resources.ConfigCommand_MissingNameOptionError.format_with(configuration.ConfigCommand.Command.to_string(), "--name"));
                Environment.Exit(-1);
            }

            if (configuration.ConfigCommand.Command == ConfigCommandType.Set &&
                string.IsNullOrWhiteSpace(configuration.ConfigCommand.ConfigValue))
            {
                Logger.Error(Resources.ConfigCommand_MissingValueOptionError.format_with(configuration.ConfigCommand.Command.to_string(), "--value"));
                Environment.Exit(-1);
            }
        }
Ejemplo n.º 3
0
 public virtual void ConfigureArgumentParser(OptionSet optionSet, ChocolateyGuiConfiguration configuration)
 {
     optionSet
     .Add(
         "name=",
         Resources.ConfigCommand_NameOption,
         option => configuration.ConfigCommand.Name = option.remove_surrounding_quotes())
     .Add(
         "value=",
         Resources.ConfigCommand_ValueOption,
         option => configuration.ConfigCommand.ConfigValue = option.remove_surrounding_quotes())
     .Add(
         "g|global",
         Resources.GlobalOption,
         option => configuration.Global = option != null);
 }
        public void ListSettings(ChocolateyGuiConfiguration configuration)
        {
            Logger.Warning(Resources.Command_SettingsTitle);
            Logger.Information(string.Empty);

            foreach (var setting in GetSettings())
            {
                Logger.Information("{0} = {1} | {2}".format_with(setting.Key, setting.Value, setting.Description));
            }

            Logger.Information(string.Empty);
            Logger.Warning(Resources.Command_FeaturesTitle);
            Logger.Information(string.Empty);
            ListFeatures(configuration);
            Logger.Information(string.Empty);
            Logger.Information(Resources.Command_UseFeatureCommandNote.format_with("chocolateyguicli feature"));
        }
Ejemplo n.º 5
0
        public virtual void Run(ChocolateyGuiConfiguration config)
        {
            switch (config.FeatureCommand.Command)
            {
            case FeatureCommandType.List:
                _configService.ListFeatures(config);
                break;

            case FeatureCommandType.Disable:
                _configService.ToggleFeature(config, false);
                break;

            case FeatureCommandType.Enable:
                _configService.ToggleFeature(config, true);
                break;
            }
        }
        private static void SetUpGlobalOptions(IList <string> args, ChocolateyGuiConfiguration configuration, IContainer container)
        {
            ParseArgumentsAndUpdateConfiguration(
                args,
                configuration,
                (option_set) =>
            {
                option_set
                .Add(
                    "r|limitoutput|limit-output",
                    ChocolateyGui.Common.Properties.Resources.Command_LimitOutputOption,
                    option => configuration.RegularOutput = option == null);
            },
                (unparsedArgs) =>
            {
                if (!string.IsNullOrWhiteSpace(configuration.CommandName))
                {
                    // save help for next menu
                    configuration.HelpRequested       = false;
                    configuration.UnsuccessfulParsing = false;
                }
            },
                () => { },
                () =>
            {
                var commandsLog = new StringBuilder();
                var commands    = container.Resolve <IEnumerable <ICommand> >();
                foreach (var command in commands.or_empty_list_if_null())
                {
                    var attributes = command.GetType().GetCustomAttributes(typeof(LocalizedCommandForAttribute), false).Cast <LocalizedCommandForAttribute>();
                    foreach (var attribute in attributes.or_empty_list_if_null())
                    {
                        commandsLog.AppendFormat(" * {0} - {1}\n", attribute.CommandName, attribute.Description);
                    }
                }

                Bootstrapper.Logger.Information(ChocolateyGui.Common.Properties.Resources.Command_CommandsListText.format_with("chocolateyguicli"));
                Bootstrapper.Logger.Information(string.Empty);
                Bootstrapper.Logger.Warning(ChocolateyGui.Common.Properties.Resources.Command_CommandsTitle);
                Bootstrapper.Logger.Information(string.Empty);
                Bootstrapper.Logger.Information("{0}".format_with(commandsLog.ToString()));
                Bootstrapper.Logger.Information(ChocolateyGui.Common.Properties.Resources.Command_CommandsText.format_with("chocolateyguicli"));
                Bootstrapper.Logger.Information(string.Empty);
                Bootstrapper.Logger.Warning(ChocolateyGui.Common.Properties.Resources.Command_DefaultOptionsAndSwitches);
            });
        }
Ejemplo n.º 7
0
        public virtual void HelpMessage(ChocolateyGuiConfiguration configuration)
        {
            Logger.Warning(L(nameof(Resources.PurgeCommand_Title)));
            Logger.Information(string.Empty);
            Logger.Information(L(nameof(Resources.PurgeCommand_Help)));
            Logger.Information(string.Empty);
            Logger.Warning(L(nameof(Resources.Command_Usage)));
            Logger.Information(@"
    chocolateyguicli purge icons|outdated [<options/switches>]
");
            Logger.Warning(L(nameof(Resources.Command_Examples)));
            Logger.Information(@"
    chocolateyguicli purge icons
    chocolateyguicli purge outdated
");

            PrintExitCodeInformation();
        }
        public async Task UpdateChocolateyGuiFeature(ChocolateyGuiFeature feature)
        {
            var configuration = new ChocolateyGuiConfiguration();

            configuration.CommandName         = "feature";
            configuration.FeatureCommand.Name = feature.Title;

            if (feature.Enabled)
            {
                configuration.FeatureCommand.Command = FeatureCommandType.Enable;
                await Task.Run(() => _configService.EnableFeature(configuration));
            }
            else
            {
                configuration.FeatureCommand.Command = FeatureCommandType.Disable;
                await Task.Run(() => _configService.DisableFeature(configuration));
            }

            _eventAggregator.PublishOnUIThread(new FeatureModifiedMessage());
        }
Ejemplo n.º 9
0
        public virtual void HelpMessage(ChocolateyGuiConfiguration configuration)
        {
            Logger.Warning(L(nameof(Resources.FeatureCommand_Title)));
            Logger.Information(string.Empty);
            Logger.Information(L(nameof(Resources.FeatureCommand_Help)));
            Logger.Information(string.Empty);
            Logger.Warning(L(nameof(Resources.Command_Usage)));
            Logger.Information(@"
    chocolateyguicli feature [list]|disable|enable [<options/switches>]
");
            Logger.Warning(L(nameof(Resources.Command_Examples)));
            Logger.Information(@"
    chocolateyguicli feature
    chocolateyguicli feature list
    chocolateyguicli feature disable -n=ShowConsoleOutput
    chocolateyguicli feature enable -n=ShowConsoleOutput
");

            PrintExitCodeInformation();
        }
Ejemplo n.º 10
0
        public void UnsetConfigValue(ChocolateyGuiConfiguration configuration)
        {
            if (configuration.Global && !Hacks.IsElevated)
            {
                // This is not allowed!
                Logger.Error(L(nameof(Resources.ConfigCommand_Unset_ElevatedPermissionsError)));
                return;
            }

            var chosenAppConfiguration = GetChosenAppConfiguration(configuration.Global);
            var configProperty         = GetProperty(configuration.ConfigCommand.Name, false);

            configProperty.SetValue(chosenAppConfiguration, string.Empty);
            UpdateSettings(chosenAppConfiguration, configuration.Global);

            // since the update happened successfully, update the effective configuration
            configProperty.SetValue(EffectiveAppConfiguration, string.Empty);

            Logger.Warning(L(nameof(Resources.ConfigCommand_Unset), configuration.ConfigCommand.Name));
        }
Ejemplo n.º 11
0
        public virtual void Run(ChocolateyGuiConfiguration config)
        {
            switch (config.ConfigCommand.Command)
            {
            case ConfigCommandType.List:
                _configService.ListSettings(config);
                break;

            case ConfigCommandType.Get:
                _configService.GetConfigValue(config);
                break;

            case ConfigCommandType.Set:
                _configService.SetConfigValue(config);
                break;

            case ConfigCommandType.Unset:
                _configService.UnsetConfigValue(config);
                break;
            }
        }
Ejemplo n.º 12
0
        public async Task UpdateChocolateyGuiFeature(ChocolateyGuiFeature feature)
        {
            // When the flow direction gets changed, this results in the feature
            // being null some times immediately. As such, if the feature is null
            // then just return so we don't encounter an exception.
            if (feature == null)
            {
                return;
            }

            var configuration = new ChocolateyGuiConfiguration();

            configuration.CommandName         = "feature";
            configuration.FeatureCommand.Name = feature.Title;

            if (feature.Enabled)
            {
                configuration.FeatureCommand.Command = FeatureCommandType.Enable;
                await Task.Run(() => _configService.ToggleFeature(configuration, true));
            }
            else
            {
                configuration.FeatureCommand.Command = FeatureCommandType.Disable;
                await Task.Run(() => _configService.ToggleFeature(configuration, false));
            }

            _eventAggregator.PublishOnUIThread(new FeatureModifiedMessage());

            if (feature.Title == "ShowAggregatedSourceView")
            {
                await _eventAggregator.PublishOnUIThreadAsync(new SourcesUpdatedMessage());
            }

            if (feature.Title == "DefaultToDarkMode")
            {
                ThemeAssist.BundledTheme.IsLightTheme = !feature.Enabled;
                ThemeAssist.BundledTheme.ToggleTheme.Execute(null);
            }
        }
Ejemplo n.º 13
0
        public virtual void HelpMessage(ChocolateyGuiConfiguration configuration)
        {
            Logger.Warning(Resources.ConfigCommand_Title);
            Logger.Information(string.Empty);
            Logger.Information(Resources.ConfigCommand_Help);
            Logger.Information(string.Empty);
            Logger.Warning(Resources.Command_Usage);
            Logger.Information(@"
    chocolateyguicli config [list]|get|set|unset [<options/switches>]
");

            Logger.Warning(Resources.Command_Examples);
            Logger.Information(@"
    chocolateyguicli config
    chocolateyguicli config list
    chocolateyguicli config get outdatedPackagesCacheDurationInMinutes
    chocolateyguicli config get --name outdatedPackagesCacheDurationInMinutes
    chocolateyguicli config set outdatedPackagesCacheDurationInMinutes 60
    chocolateyguicli config set --name outdatedPackagesCacheDurationInMinutes --value 60
    chocolateyguicli config unset outdatedPackagesCacheDurationInMinutes
    chocolateyguicli config unset --name outdatedPackagesCacheDurationInMinutes
");
            PrintExitCodeInformation();
        }
Ejemplo n.º 14
0
        public virtual void HandleAdditionalArgumentParsing(IList <string> unparsedArguments, ChocolateyGuiConfiguration configuration)
        {
            configuration.Input = string.Join(" ", unparsedArguments);

            if (unparsedArguments.Count > 1)
            {
                Logger.Error(L(nameof(Resources.FeatureCommand_SingleFeatureError)));
                Environment.Exit(-1);
            }

            var command         = FeatureCommandType.Unknown;
            var unparsedCommand = unparsedArguments.DefaultIfEmpty(string.Empty).FirstOrDefault();

            Enum.TryParse(unparsedCommand, true, out command);
            if (command == FeatureCommandType.Unknown)
            {
                if (!string.IsNullOrWhiteSpace(unparsedCommand))
                {
                    Logger.Warning(L(nameof(Resources.FeatureCommand_UnknownCommandError), unparsedCommand, "list"));
                }

                command = FeatureCommandType.List;
            }

            configuration.FeatureCommand.Command = command;
        }
Ejemplo n.º 15
0
        public virtual void HandleAdditionalArgumentParsing(IList <string> unparsedArguments, ChocolateyGuiConfiguration configuration)
        {
            configuration.Input = string.Join(" ", unparsedArguments);

            var command         = ConfigCommandType.Unknown;
            var unparsedCommand = unparsedArguments.DefaultIfEmpty(string.Empty).FirstOrDefault().to_string().Replace("-", string.Empty);

            Enum.TryParse(unparsedCommand, true, out command);
            if (command == ConfigCommandType.Unknown)
            {
                if (!string.IsNullOrWhiteSpace(unparsedCommand))
                {
                    Logger.Warning(Resources.ConfigCommand_UnknownCommandError.format_with(unparsedCommand, "list"));
                }

                command = ConfigCommandType.List;
            }

            configuration.ConfigCommand.Command = command;

            if ((configuration.ConfigCommand.Command == ConfigCommandType.List || !string.IsNullOrWhiteSpace(configuration.ConfigCommand.Name)) && unparsedArguments.Count > 1)
            {
                Logger.Error(Resources.ConfigCommand_SingleConfigError);
                Environment.Exit(-1);
            }

            if (string.IsNullOrWhiteSpace(configuration.ConfigCommand.Name) && unparsedArguments.Count >= 2)
            {
                configuration.ConfigCommand.Name = unparsedArguments[1];
            }

            if (string.IsNullOrWhiteSpace(configuration.ConfigCommand.ConfigValue) && unparsedArguments.Count >= 3)
            {
                configuration.ConfigCommand.ConfigValue = unparsedArguments[2];
            }
        }
Ejemplo n.º 16
0
 public virtual void ConfigureArgumentParser(OptionSet optionSet, ChocolateyGuiConfiguration configuration)
 {
     // There are no additional options for this command currently
 }
Ejemplo n.º 17
0
        public virtual void HandleAdditionalArgumentParsing(IList <string> unparsedArguments, ChocolateyGuiConfiguration configuration)
        {
            configuration.Input = string.Join(" ", unparsedArguments);

            if (unparsedArguments.Count > 1)
            {
                Environment.Exit(-1);
            }

            var command         = PurgeCommandType.Unknown;
            var unparsedCommand = unparsedArguments.DefaultIfEmpty(string.Empty).FirstOrDefault();

            Enum.TryParse(unparsedCommand, true, out command);
            configuration.PurgeCommand.Command = command;
        }
        public static void Main(string[] args)
        {
            AddAssemblyResolver();

            Bootstrapper.Configure();

            var            commandName = string.Empty;
            IList <string> commandArgs = new List <string>();

            // shift the first arg off
            int count = 0;

            foreach (var arg in args)
            {
                if (count == 0)
                {
                    count += 1;
                    continue;
                }

                commandArgs.Add(arg);
            }

            var configuration = new ChocolateyGuiConfiguration();

            SetUpGlobalOptions(args, configuration, Bootstrapper.Container);
            SetEnvironmentOptions(configuration);

            if (configuration.RegularOutput)
            {
#if DEBUG
                Bootstrapper.Logger.Information("{0} v{1} (DEBUG BUILD)".format_with("Chocolatey GUI", configuration.Information.ChocolateyGuiProductVersion));
#else
                Bootstrapper.Logger.Information("{0} v{1}".format_with("Chocolatey GUI", configuration.Information.ChocolateyGuiProductVersion));
#endif

                if (args.Length == 0)
                {
                    Bootstrapper.Logger.Information(ChocolateyGui.Common.Properties.Resources.Command_CommandsText.format_with("chocolateyguicli"));
                }
            }

            var runner = new GenericRunner();
            runner.Run(configuration, Bootstrapper.Container, command =>
            {
                ParseArgumentsAndUpdateConfiguration(
                    commandArgs,
                    configuration,
                    (optionSet) => command.ConfigureArgumentParser(optionSet, configuration),
                    (unparsedArgs) =>
                {
                    command.HandleAdditionalArgumentParsing(unparsedArgs, configuration);
                },
                    () =>
                {
                    Bootstrapper.Logger.Debug("Performing validation checks...");
                    command.HandleValidation(configuration);
                },
                    () => command.HelpMessage(configuration));
            });
        }
        private static void ParseArgumentsAndUpdateConfiguration(
            ICollection <string> args,
            ChocolateyGuiConfiguration configuration,
            Action <OptionSet> setOptions,
            Action <IList <string> > afterParse,
            Action validateConfiguration,
            Action helpMessage)
        {
            IList <string> unparsedArguments = new List <string>();

            // add help only once
            if (_optionSet.Count == 0)
            {
                _optionSet
                .Add(
                    "?|help|h",
                    ChocolateyGui.Common.Properties.Resources.Command_HelpOption,
                    option => configuration.HelpRequested = option != null);
            }

            if (setOptions != null)
            {
                setOptions(_optionSet);
            }

            try
            {
                unparsedArguments = _optionSet.Parse(args);
            }
            catch (OptionException)
            {
                ShowHelp(_optionSet, helpMessage);
                configuration.UnsuccessfulParsing = true;
            }

            // the command argument
            if (string.IsNullOrWhiteSpace(configuration.CommandName) &&
                unparsedArguments.Contains(args.FirstOrDefault()))
            {
                var commandName = args.FirstOrDefault();
                if (!Regex.IsMatch(commandName, @"^[-\/+]"))
                {
                    configuration.CommandName = commandName;
                }
                else if (commandName.is_equal_to("-v") || commandName.is_equal_to("--version"))
                {
                    // skip help menu
                }
                else
                {
                    configuration.HelpRequested       = true;
                    configuration.UnsuccessfulParsing = true;
                }
            }

            if (afterParse != null)
            {
                afterParse(unparsedArguments);
            }

            if (configuration.HelpRequested)
            {
                ShowHelp(_optionSet, helpMessage);
            }
            else
            {
                if (validateConfiguration != null)
                {
                    validateConfiguration();
                }
            }
        }
 private static void SetEnvironmentOptions(ChocolateyGuiConfiguration config)
 {
     config.Information.ChocolateyGuiVersion        = VersionInformation.get_current_assembly_version(Assembly.GetCallingAssembly());
     config.Information.ChocolateyGuiProductVersion = VersionInformation.get_current_informational_version(Assembly.GetCallingAssembly());
     config.Information.FullName = Assembly.GetExecutingAssembly().FullName;
 }
Ejemplo n.º 21
0
        public static void Main(string[] args)
        {
            try
            {
                AddAssemblyResolver();

                Bootstrapper.Configure();

                var            commandName = string.Empty;
                IList <string> commandArgs = new List <string>();

                // shift the first arg off
                int count = 0;
                foreach (var arg in args)
                {
                    if (count == 0)
                    {
                        count += 1;
                        continue;
                    }

                    commandArgs.Add(arg);
                }

                var configuration = new ChocolateyGuiConfiguration();
                SetUpGlobalOptions(args, configuration, Bootstrapper.Container);
                SetEnvironmentOptions(configuration);

                if (configuration.RegularOutput)
                {
    #if DEBUG
                    Bootstrapper.Logger.Warning(" (DEBUG BUILD)".format_with("Chocolatey GUI", configuration.Information.DisplayVersion));
    #else
                    Bootstrapper.Logger.Warning("{0}".format_with(configuration.Information.DisplayVersion));
    #endif

                    if (args.Length == 0)
                    {
                        Bootstrapper.Logger.Information(ChocolateyGui.Common.Properties.Resources.Command_CommandsText.format_with("chocolateyguicli"));
                    }
                }

                var runner = new GenericRunner();
                runner.Run(configuration, Bootstrapper.Container, command =>
                {
                    ParseArgumentsAndUpdateConfiguration(
                        commandArgs,
                        configuration,
                        (optionSet) => command.ConfigureArgumentParser(optionSet, configuration),
                        (unparsedArgs) =>
                    {
                        command.HandleAdditionalArgumentParsing(unparsedArgs, configuration);
                    },
                        () =>
                    {
                        Bootstrapper.Logger.Debug("Performing validation checks...");
                        command.HandleValidation(configuration);
                    },
                        () => command.HelpMessage(configuration));
                });
            }
            catch (Exception ex)
            {
                Bootstrapper.Logger.Error(ex.Message);
            }
            finally
            {
                Log.CloseAndFlush();

                if (Bootstrapper.Container != null)
                {
                    if (Bootstrapper.Container.IsRegisteredWithName <LiteDatabase>(Bootstrapper.GlobalConfigurationDatabaseName))
                    {
                        var globalDatabase = Bootstrapper.Container.ResolveNamed <LiteDatabase>(Bootstrapper.GlobalConfigurationDatabaseName);
                        globalDatabase.Dispose();
                    }

                    if (Bootstrapper.Container.IsRegisteredWithName <LiteDatabase>(Bootstrapper.UserConfigurationDatabaseName))
                    {
                        var userDatabase = Bootstrapper.Container.ResolveNamed <LiteDatabase>(Bootstrapper.UserConfigurationDatabaseName);
                        userDatabase.Dispose();
                    }

                    Bootstrapper.Container.Dispose();
                }
            }
        }