forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.cpp
177 lines (150 loc) · 6.23 KB
/
Core.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/AppInstallerCLICore.h"
#include "Commands/RootCommand.h"
#include "ExecutionContext.h"
#include "Workflows/WorkflowBase.h"
#include <winget/UserSettings.h>
using namespace winrt;
using namespace winrt::Windows::Foundation;
using namespace AppInstaller::CLI;
using namespace AppInstaller::Utility::literals;
namespace AppInstaller::CLI
{
namespace
{
// RAII class to restore the console output codepage.
struct ConsoleOutputCPRestore
{
ConsoleOutputCPRestore(UINT cpToChangeTo)
{
m_previousCP = GetConsoleOutputCP();
LOG_LAST_ERROR_IF(!SetConsoleOutputCP(cpToChangeTo));
}
~ConsoleOutputCPRestore()
{
SetConsoleOutputCP(m_previousCP);
}
ConsoleOutputCPRestore(const ConsoleOutputCPRestore&) = delete;
ConsoleOutputCPRestore& operator=(const ConsoleOutputCPRestore&) = delete;
ConsoleOutputCPRestore(ConsoleOutputCPRestore&&) = delete;
ConsoleOutputCPRestore& operator=(ConsoleOutputCPRestore&&) = delete;
private:
UINT m_previousCP = 0;
};
}
int CoreMain(int argc, wchar_t const** argv) try
{
init_apartment();
// Enable all logging for this phase; we will update once we have the arguments
Logging::Log().EnableChannel(Logging::Channel::All);
Logging::Log().SetLevel(Logging::Level::Verbose);
Logging::AddFileLogger();
Logging::EnableWilFailureTelemetry();
// Set output to UTF8
ConsoleOutputCPRestore utf8CP(CP_UTF8);
Logging::Telemetry().LogStartup();
// Initiate the background cleanup of the log file location.
Logging::BeginLogFileCleanup();
Execution::Context context{ std::cout, std::cin };
context.EnableCtrlHandler();
context << Workflow::ReportExecutionStage(Workflow::ExecutionStage::ParseArgs);
// Convert incoming wide char args to UTF8
std::vector<std::string> utf8Args;
for (int i = 1; i < argc; ++i)
{
utf8Args.emplace_back(Utility::ConvertToUTF8(argv[i]));
}
AICLI_LOG(CLI, Info, << "WinGet invoked with arguments:" << [&]() {
std::stringstream strstr;
for (const auto& arg : utf8Args)
{
strstr << " '" << arg << '\'';
}
return strstr.str();
}());
Invocation invocation{ std::move(utf8Args) };
// The root command is our fallback in the event of very bad or very little input
std::unique_ptr<Command> command = std::make_unique<RootCommand>();
try
{
std::unique_ptr<Command> subCommand = command->FindSubCommand(invocation);
while (subCommand)
{
command = std::move(subCommand);
subCommand = command->FindSubCommand(invocation);
}
Logging::Telemetry().LogCommand(command->FullName());
command->ParseArguments(invocation, context.Args);
// Change logging level to Info if Verbose not requested
if (!context.Args.Contains(Execution::Args::Type::VerboseLogs))
{
Logging::Log().SetLevel(Logging::Level::Info);
}
context.UpdateForArgs();
command->ValidateArguments(context.Args);
}
// Exceptions specific to parsing the arguments of a command
catch (const CommandException& ce)
{
command->OutputHelp(context.Reporter, &ce);
AICLI_LOG(CLI, Error, << "Error encountered parsing command line: " << ce.Message());
return APPINSTALLER_CLI_ERROR_INVALID_CL_ARGUMENTS;
}
try
{
if (!Settings::User().GetWarnings().empty())
{
context.Reporter.Warn() << Resource::String::SettingsWarnings << std::endl;
}
command->Execute(context);
}
// Exceptions that may occur in the process of executing an arbitrary command
catch (const wil::ResultException& re)
{
// Even though they are logged at their source, log again here for completeness.
Logging::Telemetry().LogException(command->FullName(), "wil::ResultException", re.what());
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
GetUserPresentableMessage(re) << std::endl;
return re.GetErrorCode();
}
catch (const winrt::hresult_error& hre)
{
std::string message = GetUserPresentableMessage(hre);
Logging::Telemetry().LogException(command->FullName(), "winrt::hresult_error", message);
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
message << std::endl;
return hre.code();
}
catch (const std::exception& e)
{
Logging::Telemetry().LogException(command->FullName(), "std::exception", e.what());
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << ' ' << std::endl <<
GetUserPresentableMessage(e) << std::endl;
return APPINSTALLER_CLI_ERROR_COMMAND_FAILED;
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
Logging::Telemetry().LogException(command->FullName(), "unknown", {});
context.Reporter.Error() <<
Resource::String::UnexpectedErrorExecutingCommand << " ???"_liv << std::endl;
return APPINSTALLER_CLI_ERROR_COMMAND_FAILED;
}
if (SUCCEEDED(context.GetTerminationHR()))
{
Logging::Telemetry().LogCommandSuccess(command->FullName());
}
return context.GetTerminationHR();
}
// End of the line exceptions that are not ever expected.
// Telemetry cannot be reliable beyond this point, so don't let these happen.
catch (...)
{
return APPINSTALLER_CLI_ERROR_INTERNAL_ERROR;
}
}