Skip to content

Commit

Permalink
Merge pull request chocolatey#834 from steviecoaster/gh770
Browse files Browse the repository at this point in the history
(chocolatey#770) Provide ability to "see" saved installation parameters
  • Loading branch information
gep13 authored Jan 21, 2022
2 parents 418bb96 + e7329bf commit e704b2d
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@
<Compile Include="Services\DialogService.cs" />
<Compile Include="Services\IDialogService.cs" />
<Compile Include="Utilities\ChocolateyMessageBox.cs" />
<Compile Include="Services\IPackageArgumentsService.cs" />
<Compile Include="Services\PackageArgumentsService.cs" />
<Compile Include="Utilities\ToolTipBehavior.cs" />
<Compile Include="Bootstrapper.cs" />
<Compile Include="Commands\CommandExecutionManager.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="IPackageArgumentsService.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Collections.Generic;

namespace ChocolateyGui.Common.Windows.Services
{
public interface IPackageArgumentsService
{
IEnumerable<string> DecryptPackageArgumentsFile(string id, string version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="PackageArgumentsService.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using chocolatey;
using chocolatey.infrastructure.adapters;
using chocolatey.infrastructure.app.utility;
using ChocolateyGui.Common.Properties;
using ChocolateyGui.Common.Providers;
using IFileSystem = chocolatey.infrastructure.filesystem.IFileSystem;
using ILogger = Serilog.ILogger;

namespace ChocolateyGui.Common.Windows.Services
{
public class PackageArgumentsService : IPackageArgumentsService
{
private static readonly ILogger Logger = Serilog.Log.ForContext<PackageArgumentsService>();
private readonly IEncryptionUtility _encryptionUtility;
private readonly IFileSystem _fileSystem;
private readonly IChocolateyConfigurationProvider _chocolateyConfigurationProvider;
private readonly IDialogService _dialogService;

public PackageArgumentsService(
IEncryptionUtility encryptionUtility,
IFileSystem fileSystem,
IChocolateyConfigurationProvider chocolateyConfigurationProvider,
IDialogService dialogService)
{
_encryptionUtility = encryptionUtility;
_fileSystem = fileSystem;
_chocolateyConfigurationProvider = chocolateyConfigurationProvider;
_dialogService = dialogService;
}

public IEnumerable<string> DecryptPackageArgumentsFile(string id, string version)
{
var argumentsPath = _fileSystem.combine_paths(_chocolateyConfigurationProvider.ChocolateyInstall, ".chocolatey", "{0}.{1}".format_with(id, version));
var argumentsFile = _fileSystem.combine_paths(argumentsPath, ".arguments");

string arguments = string.Empty;

// Get the arguments decrypted in here and return them
try
{
if (_fileSystem.file_exists(argumentsFile))
{
arguments = _fileSystem.read_file(argumentsFile);
}
}
catch (Exception ex)
{
var message = Resources.Application_PackageArgumentsError.format_with(version, id);
Logger.Error(ex, message);
}

if (string.IsNullOrEmpty(arguments))
{
_dialogService.ShowMessageAsync(
string.Empty,
Resources.PackageView_UnableToFindArgumentsFile.format_with(version, id));
yield break;
}

// The following code is borrowed from the Chocolatey codebase, should
// be extracted to a separate location in choco executable so we can re-use it.
var packageArgumentsUnencrypted = arguments.contains(" --") && arguments.to_string().Length > 4
? arguments
: _encryptionUtility.decrypt_string(arguments);

var sensitiveArgs = ArgumentsUtility.arguments_contain_sensitive_information(packageArgumentsUnencrypted);

var packageArgumentsSplit =
packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries);

foreach (var packageArgument in packageArgumentsSplit.or_empty_list_if_null())
{
var packageArgumentSplit =
packageArgument.Split(new[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries);
var optionName = packageArgumentSplit[0].to_string();
var optionValue = string.Empty;

if (packageArgumentSplit.Length == 2)
{
optionValue = packageArgumentSplit[1].to_string().remove_surrounding_quotes();
if (optionValue.StartsWith("'"))
{
optionValue.remove_surrounding_quotes();
}
}

yield return "--{0}{1}".format_with(
optionName,
string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
using AutoMapper;
using Caliburn.Micro;
using chocolatey;
using chocolatey.infrastructure.adapters;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.nuget;
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.cryptography;
using chocolatey.infrastructure.filesystem;
using chocolatey.infrastructure.services;
using ChocolateyGui.Common.Models;
Expand Down Expand Up @@ -48,6 +50,8 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<ChocolateyConfigurationProvider>().As<IChocolateyConfigurationProvider>().SingleInstance();
builder.RegisterType<ChocolateyService>().As<IChocolateyService>().SingleInstance();
builder.RegisterType<DotNetFileSystem>().As<chocolatey.infrastructure.filesystem.IFileSystem>().SingleInstance();
builder.RegisterType<PackageArgumentsService>().As<IPackageArgumentsService>().SingleInstance();
builder.RegisterType<DefaultEncryptionUtility>().As<IEncryptionUtility>().SingleInstance();

// Register ViewModels
builder.RegisterAssemblyTypes(viewModelAssembly)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="PackageViewModel.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
Expand All @@ -7,9 +7,11 @@

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Caliburn.Micro;
using chocolatey;
using ChocolateyGui.Common.Base;
using ChocolateyGui.Common.Models;
using ChocolateyGui.Common.Models.Messages;
Expand Down Expand Up @@ -46,6 +48,7 @@ public class PackageViewModel :
private readonly IChocolateyGuiCacheService _chocolateyGuiCacheService;
private readonly IConfigService _configService;
private readonly IAllowedCommandsService _allowedCommandsService;
private readonly IPackageArgumentsService _packageArgumentsService;

private string[] _authors;

Expand Down Expand Up @@ -123,7 +126,8 @@ public PackageViewModel(
IProgressService progressService,
IChocolateyGuiCacheService chocolateyGuiCacheService,
IConfigService configService,
IAllowedCommandsService allowedCommandsService)
IAllowedCommandsService allowedCommandsService,
IPackageArgumentsService packageArgumentsService)
{
_chocolateyService = chocolateyService;
_eventAggregator = eventAggregator;
Expand All @@ -134,6 +138,7 @@ public PackageViewModel(
_chocolateyGuiCacheService = chocolateyGuiCacheService;
_configService = configService;
_allowedCommandsService = allowedCommandsService;
_packageArgumentsService = packageArgumentsService;
}

public DateTime Created
Expand Down Expand Up @@ -422,6 +427,15 @@ public bool IsPackageSizeAvailable
get { return PackageSize != -1; }
}

public async Task ShowArguments()
{
var decryptedArguments = _packageArgumentsService.DecryptPackageArgumentsFile(Id, Version.ToString()).ToList();

await _dialogService.ShowMessageAsync(
Resources.PackageViewModel_ArgumentsForPackageFormat.format_with(Title),
string.Join(Environment.NewLine, decryptedArguments));
}

public async Task Install()
{
await InstallPackage(Version.ToString());
Expand Down
9 changes: 8 additions & 1 deletion Source/ChocolateyGui.Common.Windows/Views/PackageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
VerticalAlignment="Stretch"
Background="{DynamicResource MahApps.Brushes.Accent4}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,20,10">
<Button Padding="10" Margin="5 0"
Command="{commands:DataContextCommandAdapter ShowArguments}">
<StackPanel Orientation="Horizontal">
<iconPacks:PackIconEntypo Kind="Info" Margin="0 0 5 0 " VerticalAlignment="Center" />
<TextBlock Text="{x:Static properties:Resources.PackageView_ButtonPackageArguments}" FontSize="16"/>
</StackPanel>
</Button>
<StackPanel Orientation="Horizontal"
Visibility="{Binding IsInstalled, Converter={StaticResource BooleanToVisibility}, ConverterParameter=True}">
<Button Padding="10" Margin="5 0"
Expand Down Expand Up @@ -183,7 +190,7 @@
Target="{Binding ElementName=Version}" />
<TextBlock x:Name="Version" Text="{Binding Version}"
Style="{StaticResource PackageResourceValue}" />

<Label Style="{StaticResource PackageResourceLabel}" Content="{x:Static properties:Resources.PackageView_Downloads}"
Target="{Binding ElementName=VersionDownloadCount}"
Visibility="{Binding IsDownloadCountAvailable, Converter={StaticResource BoolToVis}}" />
Expand Down
Loading

0 comments on commit e704b2d

Please sign in to comment.