Skip to content

Commit cf597ec

Browse files
authored
Make Ignore Warnings suppress the output level entirely (#4221)
<!-- To check a checkbox place an "x" between the brackets. e.g: [x] --> - [x] I have signed the [Contributor License Agreement](https://cla.opensource.microsoft.com/microsoft/winget-pkgs). - [x] This pull request is related to an issue. * Resolves #4213 * Closes #4214 Instead of handling each case individually, this PR makes the `--ignore-warnings` a common argument and suppresses all output from `context.Reporter.Warn()`. Support is added for enabling or disabling each reporter level individually. cc @yao-msft ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/microsoft/winget-cli/pull/4221)
1 parent 7cea3d9 commit cf597ec

File tree

9 files changed

+79
-15
lines changed

9 files changed

+79
-15
lines changed

src/AppInstallerCLICore/Argument.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ namespace AppInstaller::CLI
406406
args.push_back(ForType(Args::Type::RainbowStyle));
407407
args.push_back(ForType(Args::Type::RetroStyle));
408408
args.push_back(ForType(Args::Type::VerboseLogs));
409+
args.push_back(ForType(Args::Type::IgnoreWarnings));
409410
args.emplace_back(Args::Type::DisableInteractivity, Resource::String::DisableInteractivityArgumentDescription, ArgumentType::Flag, false);
410411
args.push_back(ForType(Args::Type::Proxy));
411412
args.push_back(ForType(Args::Type::NoProxy));

src/AppInstallerCLICore/ChannelStreams.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ namespace AppInstaller::CLI::Execution
9494
OutputStream& operator<<(const VirtualTerminal::ConstructedSequence& sequence);
9595
OutputStream& operator<<(const std::filesystem::path& path);
9696

97+
inline bool IsEnabled() {
98+
return m_enabled;
99+
}
100+
97101
private:
98102
// Applies the format for the stream.
99103
void ApplyFormat();

src/AppInstallerCLICore/Commands/ValidateCommand.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace AppInstaller::CLI
1616
{
1717
return {
1818
Argument::ForType(Execution::Args::Type::ValidateManifest),
19-
Argument::ForType(Execution::Args::Type::IgnoreWarnings),
2019
};
2120
}
2221

src/AppInstallerCLICore/ExecutionContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ namespace AppInstaller::CLI::Execution
356356
Logging::Log().SetLevel(Logging::Level::Verbose);
357357
}
358358

359+
// Disable warnings if requested
360+
if (Args.Contains(Args::Type::IgnoreWarnings))
361+
{
362+
Reporter.SetLevelMask(Reporter::Level::Warning, false);
363+
}
364+
359365
// Set proxy
360366
if (Args.Contains(Args::Type::Proxy))
361367
{

src/AppInstallerCLICore/ExecutionReporter.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ namespace AppInstaller::CLI::Execution
5454

5555
OutputStream Reporter::GetOutputStream(Level level)
5656
{
57+
// If the level is not enabled, return a default stream which is disabled
58+
if (WI_AreAllFlagsClear(m_enabledLevels, level))
59+
{
60+
return OutputStream(*m_out, false, false);
61+
}
62+
5763
OutputStream result = GetBasicOutputStream();
5864

5965
switch (level)
@@ -111,15 +117,21 @@ namespace AppInstaller::CLI::Execution
111117
}
112118
}
113119

114-
bool Reporter::PromptForBoolResponse(Resource::LocString message, Level level)
120+
bool Reporter::PromptForBoolResponse(Resource::LocString message, Level level, bool resultIfDisabled)
115121
{
122+
auto out = GetOutputStream(level);
123+
124+
if (!out.IsEnabled())
125+
{
126+
return resultIfDisabled;
127+
}
128+
116129
const std::vector<BoolPromptOption> options
117130
{
118131
BoolPromptOption{ Resource::String::PromptOptionYes, 'Y', true },
119132
BoolPromptOption{ Resource::String::PromptOptionNo, 'N', false },
120133
};
121134

122-
auto out = GetOutputStream(level);
123135
out << message << std::endl;
124136

125137
// Try prompting until we get a recognized option
@@ -164,14 +176,24 @@ namespace AppInstaller::CLI::Execution
164176
void Reporter::PromptForEnter(Level level)
165177
{
166178
auto out = GetOutputStream(level);
179+
if (!out.IsEnabled())
180+
{
181+
return;
182+
}
183+
167184
out << std::endl << Resource::String::PressEnterToContinue << std::endl;
168185
m_in.get();
169186
}
170187

171-
std::filesystem::path Reporter::PromptForPath(Resource::LocString message, Level level)
188+
std::filesystem::path Reporter::PromptForPath(Resource::LocString message, Level level, std::filesystem::path resultIfDisabled)
172189
{
173190
auto out = GetOutputStream(level);
174191

192+
if (!out.IsEnabled())
193+
{
194+
return resultIfDisabled;
195+
}
196+
175197
// Try prompting until we get a valid answer
176198
for (;;)
177199
{
@@ -319,4 +341,16 @@ namespace AppInstaller::CLI::Execution
319341
}
320342
m_out->RestoreDefault();
321343
}
344+
345+
void Reporter::SetLevelMask(Level reporterLevel, bool setEnabled) {
346+
347+
if (setEnabled)
348+
{
349+
WI_SetAllFlags(m_enabledLevels, reporterLevel);
350+
}
351+
else
352+
{
353+
WI_ClearAllFlags(m_enabledLevels, reporterLevel);
354+
}
355+
}
322356
}

src/AppInstallerCLICore/ExecutionReporter.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ namespace AppInstaller::CLI::Execution
4949
};
5050

5151
// The level for the Output channel.
52-
enum class Level
52+
enum class Level : uint32_t
5353
{
54-
Verbose,
55-
Info,
56-
Warning,
57-
Error,
54+
None = 0x0,
55+
Verbose = 0x1,
56+
Info = 0x2,
57+
Warning = 0x4,
58+
Error = 0x8,
59+
All = Verbose | Info | Warning | Error,
5860
};
5961

6062
Reporter(std::ostream& outStream, std::istream& inStream);
@@ -98,13 +100,13 @@ namespace AppInstaller::CLI::Execution
98100
void SetStyle(AppInstaller::Settings::VisualStyle style);
99101

100102
// Prompts the user, return true if they consented.
101-
bool PromptForBoolResponse(Resource::LocString message, Level level = Level::Info);
103+
bool PromptForBoolResponse(Resource::LocString message, Level level = Level::Info, bool resultIfDisabled = false);
102104

103105
// Prompts the user, continues when Enter is pressed
104106
void PromptForEnter(Level level = Level::Info);
105107

106108
// Prompts the user for a path.
107-
std::filesystem::path PromptForPath(Resource::LocString message, Level level = Level::Info);
109+
std::filesystem::path PromptForPath(Resource::LocString message, Level level = Level::Info, std::filesystem::path resultIfDisabled = std::filesystem::path::path());
108110

109111
// Used to show indefinite progress. Currently an indefinite spinner is the form of
110112
// showing indefinite progress.
@@ -165,9 +167,15 @@ namespace AppInstaller::CLI::Execution
165167
m_progressSink = sink;
166168
}
167169

170+
bool IsLevelEnabled(Level reporterLevel)
171+
{
172+
return WI_AreAllFlagsSet(m_enabledLevels, reporterLevel);
173+
}
174+
175+
void SetLevelMask(Level reporterLevel, bool setEnabled = true);
176+
168177
private:
169178
Reporter(std::shared_ptr<BaseStream> outStream, std::istream& inStream);
170-
171179
// Gets a stream for output for internal use.
172180
OutputStream GetBasicOutputStream();
173181

@@ -180,8 +188,13 @@ namespace AppInstaller::CLI::Execution
180188
wil::srwlock m_progressCallbackLock;
181189
std::atomic<ProgressCallback*> m_progressCallback;
182190
std::atomic<IProgressSink*> m_progressSink;
191+
192+
// Enable all levels by default
193+
Level m_enabledLevels = Level::All;
183194
};
184195

196+
DEFINE_ENUM_FLAG_OPERATORS(Reporter::Level);
197+
185198
// Indirection to enable change without tracking down every place
186199
extern const VirtualTerminal::Sequence& HelpCommandEmphasis;
187200
extern const VirtualTerminal::Sequence& HelpArgumentEmphasis;

src/AppInstallerCLICore/Workflows/ConfigurationFlow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ namespace AppInstaller::CLI::Workflow
10641064
}
10651065

10661066
auto promptString = m_isApply ? Resource::String::ConfigurationWarningPromptApply : Resource::String::ConfigurationWarningPromptTest;
1067-
if (!context.Reporter.PromptForBoolResponse(promptString, Reporter::Level::Warning))
1067+
if (!context.Reporter.PromptForBoolResponse(promptString, Reporter::Level::Warning, false))
10681068
{
10691069
AICLI_TERMINATE_CONTEXT(WINGET_CONFIG_ERROR_WARNING_NOT_ACCEPTED);
10701070
}

src/AppInstallerCLICore/Workflows/PromptFlow.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "ShowFlow.h"
66
#include <winget/UserSettings.h>
77

8+
using namespace AppInstaller::CLI::Execution;
89
using namespace AppInstaller::Settings;
910
using namespace AppInstaller::Utility::literals;
1011

@@ -295,6 +296,12 @@ namespace AppInstaller::CLI::Workflow
295296

296297
AICLI_LOG(CLI, Info, << "Prompting for install root.");
297298
m_installLocation = context.Reporter.PromptForPath(Resource::String::PromptForInstallRoot);
299+
if (m_installLocation.empty())
300+
{
301+
AICLI_LOG(CLI, Error, << "Install location is required but the provided path was empty.");
302+
context.Reporter.Error() << Resource::String::InstallLocationNotProvided << std::endl;
303+
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_INSTALL_LOCATION_REQUIRED);
304+
}
298305
AICLI_LOG(CLI, Info, << "Proceeding with installation using install root: " << m_installLocation);
299306
}
300307

@@ -354,7 +361,7 @@ namespace AppInstaller::CLI::Workflow
354361
return;
355362
}
356363

357-
bool accepted = context.Reporter.PromptForBoolResponse(Resource::String::PromptToProceed, Execution::Reporter::Level::Warning);
364+
bool accepted = context.Reporter.PromptForBoolResponse(Resource::String::PromptToProceed, Reporter::Level::Warning, true);
358365
if (accepted)
359366
{
360367
AICLI_LOG(CLI, Info, << "Proceeding with installation");

src/AppInstallerCLIPackage/Shared/Strings/en-us/winget.resw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ They can be configured through the settings file 'winget settings'.</value>
262262
<value>Filter results by id</value>
263263
</data>
264264
<data name="IgnoreWarningsArgumentDescription" xml:space="preserve">
265-
<value>Suppresses warning outputs.</value>
265+
<value>Suppresses warning outputs</value>
266266
</data>
267267
<data name="InstallationDisclaimer1" xml:space="preserve">
268268
<value>This application is licensed to you by its owner.</value>

0 commit comments

Comments
 (0)