Skip to content

Commit

Permalink
Pass through header changes (microsoft#1364)
Browse files Browse the repository at this point in the history
Arbitrary Header Changes
  • Loading branch information
ashpatil-msft authored Aug 27, 2021
1 parent 58402d4 commit 63ea8b0
Show file tree
Hide file tree
Showing 28 changed files with 964 additions and 626 deletions.
2 changes: 2 additions & 0 deletions src/AppInstallerCLICore/Argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace AppInstaller::CLI
return Argument{ "verbose-logs", NoAlias, Args::Type::VerboseLogs, Resource::String::VerboseLogsArgumentDescription, ArgumentType::Flag };
case Args::Type::ExperimentalArg:
return Argument{ "arg", NoAlias, Args::Type::ExperimentalArg, Resource::String::ExperimentalArgumentDescription, ArgumentType::Flag, ExperimentalFeature::Feature::ExperimentalArg };
case Args::Type::CustomHeader:
return Argument{ "header", NoAlias, Args::Type::CustomHeader, Resource::String::HeaderArgumentDescription, ArgumentType::Standard, Argument::Visibility::Help };
default:
THROW_HR(E_UNEXPECTED);
}
Expand Down
6 changes: 6 additions & 0 deletions src/AppInstallerCLICore/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ namespace AppInstaller::CLI
throw CommandException(Resource::String::TooManyBehaviorsError, s_Command_ArgName_SilentAndInteractive);
}

if (execArgs.Contains(Execution::Args::Type::CustomHeader) && !execArgs.Contains(Execution::Args::Type::Source) &&
!execArgs.Contains(Execution::Args::Type::SourceName))
{
throw CommandException(Resource::String::HeaderArgumentNotApplicableWithoutSource, Argument::ForType(Execution::Args::Type::CustomHeader).Name(), {});
}

ValidateArgumentsInternal(execArgs);
}

Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/InstallCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace AppInstaller::CLI
Argument::ForType(Args::Type::InstallLocation),
Argument::ForType(Args::Type::HashOverride),
Argument::ForType(Args::Type::AcceptPackageAgreements),
Argument::ForType(Args::Type::CustomHeader),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/ListCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace AppInstaller::CLI
Argument::ForType(Execution::Args::Type::Command),
Argument::ForType(Execution::Args::Type::Count),
Argument::ForType(Execution::Args::Type::Exact),
Argument::ForType(Execution::Args::Type::CustomHeader),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/SearchCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace AppInstaller::CLI
Argument::ForType(Execution::Args::Type::Source),
Argument::ForType(Execution::Args::Type::Count),
Argument::ForType(Execution::Args::Type::Exact),
Argument::ForType(Execution::Args::Type::CustomHeader),
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerCLICore/Commands/ShowCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Workflows/WorkflowBase.h"
#include "Resources.h"

using namespace AppInstaller::CLI::Execution;

namespace AppInstaller::CLI
{
std::vector<Argument> ShowCommand::GetArguments() const
Expand All @@ -23,6 +25,7 @@ namespace AppInstaller::CLI
Argument::ForType(Execution::Args::Type::Source),
Argument::ForType(Execution::Args::Type::Exact),
Argument::ForType(Execution::Args::Type::ListVersions),
Argument::ForType(Execution::Args::Type::CustomHeader),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/SourceCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace AppInstaller::CLI
Argument::ForType(Args::Type::SourceName).SetRequired(true),
Argument::ForType(Args::Type::SourceArg),
Argument::ForType(Args::Type::SourceType),
Argument::ForType(Args::Type::CustomHeader),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/UninstallCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace AppInstaller::CLI
Argument::ForType(Args::Type::Interactive),
Argument::ForType(Args::Type::Silent),
Argument::ForType(Args::Type::Log),
Argument::ForType(Args::Type::CustomHeader),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/Commands/UpgradeCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace AppInstaller::CLI
Argument::ForType(Args::Type::HashOverride),
Argument::ForType(Args::Type::AcceptPackageAgreements),
Argument{ "all", Argument::NoAlias, Args::Type::All, Resource::String::UpdateAllArgumentDescription, ArgumentType::Flag },
Argument::ForType(Execution::Args::Type::CustomHeader),
};
}

Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerCLICore/ExecutionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace AppInstaller::CLI::Execution
Help, // Show command usage
Info, // Show general info about WinGet
VerboseLogs, // Increases winget logging level to verbose
CustomHeader, // Optional Rest source header

// Used for demonstration purposes
ExperimentalArg,
Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerCLICore/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ namespace AppInstaller::CLI::Resource
WINGET_DEFINE_RESOURCE_STRINGID(GetManifestResultVersionNotFound);
WINGET_DEFINE_RESOURCE_STRINGID(HashCommandLongDescription);
WINGET_DEFINE_RESOURCE_STRINGID(HashCommandShortDescription);
WINGET_DEFINE_RESOURCE_STRINGID(HeaderArgumentDescription);
WINGET_DEFINE_RESOURCE_STRINGID(HeaderArgumentNotApplicableForNonRestSourceWarning);
WINGET_DEFINE_RESOURCE_STRINGID(HeaderArgumentNotApplicableWithoutSource);
WINGET_DEFINE_RESOURCE_STRINGID(HelpArgumentDescription);
WINGET_DEFINE_RESOURCE_STRINGID(HelpForDetails);
WINGET_DEFINE_RESOURCE_STRINGID(HelpLinkPreamble);
Expand Down
15 changes: 9 additions & 6 deletions src/AppInstallerCLICore/Workflows/SourceFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ namespace AppInstaller::CLI::Workflow

void AddSource(Execution::Context& context)
{
std::string name(context.Args.GetArg(Args::Type::SourceName));
std::string arg(context.Args.GetArg(Args::Type::SourceArg));
std::string type;
Repository::SourceDetails sourceDetails;
sourceDetails.Name = context.Args.GetArg(Args::Type::SourceName);
sourceDetails.Arg = context.Args.GetArg(Args::Type::SourceArg);

if (context.Args.Contains(Args::Type::SourceType))
{
type = context.Args.GetArg(Args::Type::SourceType);
sourceDetails.Type = context.Args.GetArg(Args::Type::SourceType);
}

sourceDetails.CustomHeader = GetCustomHeaderFromArg(context, sourceDetails);

context.Reporter.Info() <<
Resource::String::SourceAddBegin << std::endl <<
" "_liv << name << " -> "_liv << arg << std::endl;
" "_liv << sourceDetails.Name << " -> "_liv << sourceDetails.Arg << std::endl;

if (context.Reporter.ExecuteWithProgress(std::bind(Repository::AddSource, std::move(name), std::move(type), std::move(arg), std::placeholders::_1)))
if (context.Reporter.ExecuteWithProgress(std::bind(Repository::AddSource, sourceDetails, std::placeholders::_1)))
{
context.Reporter.Info() << Resource::String::Done;
}
Expand Down
38 changes: 35 additions & 3 deletions src/AppInstallerCLICore/Workflows/WorkflowBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,23 @@ namespace AppInstaller::CLI::Workflow
std::shared_ptr<Repository::ISource> source;
try
{
auto result = context.Reporter.ExecuteWithProgress(std::bind(Repository::OpenSource, sourceName, std::placeholders::_1), true);
source = result.Source;
OpenSourceResult result;
if (!sourceName.empty())
{
auto sourceDetails = Repository::GetSource(sourceName);
if (sourceDetails)
{
sourceDetails.value().CustomHeader = GetCustomHeaderFromArg(context, sourceDetails.value());

result = context.Reporter.ExecuteWithProgress(std::bind(Repository::OpenSourceFromDetails, sourceDetails.value(), std::placeholders::_1), true);
}
}
else
{
result = context.Reporter.ExecuteWithProgress(std::bind(Repository::OpenSource, sourceName, std::placeholders::_1), true);
}

source = result.Source;
// We'll only report the source update failure as warning and continue
for (const auto& s : result.SourcesWithUpdateFailure)
{
Expand Down Expand Up @@ -353,7 +367,7 @@ namespace AppInstaller::CLI::Workflow
latestVersion->GetProperty(PackageVersionProperty::Version),
GetMatchCriteriaDescriptor(searchResult.Matches[i]),
sourceIsComposite ? static_cast<std::string>(latestVersion->GetProperty(PackageVersionProperty::SourceName)) : ""s
});
});
}

table.Complete();
Expand Down Expand Up @@ -698,6 +712,24 @@ namespace AppInstaller::CLI::Workflow
}
}

std::optional<std::string> GetCustomHeaderFromArg(Execution::Context& context, const SourceDetails& sourceDetails)
{
std::optional<std::string> customHeader;
if (context.Args.Contains(Execution::Args::Type::CustomHeader))
{
if (!SupportsCustomHeader(sourceDetails))
{
context.Reporter.Warn() << Resource::String::HeaderArgumentNotApplicableForNonRestSourceWarning << std::endl;
}
else
{
customHeader = context.Args.GetArg(Execution::Args::Type::CustomHeader);
}
}

return customHeader;
}

void EnsureFeatureEnabled::operator()(Execution::Context& context) const
{
if (!Settings::ExperimentalFeature::IsEnabled(m_feature))
Expand Down
4 changes: 4 additions & 0 deletions src/AppInstallerCLICore/Workflows/WorkflowBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ namespace AppInstaller::CLI::Workflow
// Outputs: None
void EnsureRunningAsAdmin(Execution::Context& context);

// Gets the custom header from Arguments.
// Returns: Custom header if provided and applicable.
std::optional<std::string> GetCustomHeaderFromArg(Execution::Context& context, const AppInstaller::Repository::SourceDetails& sourceDetails);

// Ensures that the feature is enabled.
// Required Args: the desired feature
// Inputs: None
Expand Down
9 changes: 9 additions & 0 deletions src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw
Original file line number Diff line number Diff line change
Expand Up @@ -1022,4 +1022,13 @@ Do you agree to the terms?</value>
<data name="InstallFlowRegistrationDeferred" xml:space="preserve">
<value>Successfully installed. Restart the application to complete the upgrade.</value>
</data>
<data name="HeaderArgumentDescription" xml:space="preserve">
<value>Optional Windows-Package-Manager REST source HTTP header</value>
</data>
<data name="HeaderArgumentNotApplicableForNonRestSourceWarning" xml:space="preserve">
<value>Ignoring the optional header as it is not applicable for this source.</value>
</data>
<data name="HeaderArgumentNotApplicableWithoutSource" xml:space="preserve">
<value>The optional header is not applicable without specifying a Rest source</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/AppInstallerCLITests/AppInstallerCLITests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<ClCompile Include="Command.cpp" />
<ClCompile Include="Completion.cpp" />
<ClCompile Include="CompositeSource.cpp" />
<ClCompile Include="CustomHeader.cpp" />
<ClCompile Include="Downloader.cpp" />
<ClCompile Include="ExperimentalFeature.cpp" />
<ClCompile Include="GroupPolicy.cpp" />
Expand Down
Loading

0 comments on commit 63ea8b0

Please sign in to comment.