forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cpp
More file actions
196 lines (164 loc) · 6.68 KB
/
Copy pathCore.cpp
File metadata and controls
196 lines (164 loc) · 6.68 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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>
#include "Commands/InstallCommand.h"
#include "COMContext.h"
#include <AppInstallerFileLogger.h>
#ifndef AICLI_DISABLE_TEST_HOOKS
#include <winget/Debugging.h>
#endif
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;
};
void __CRTDECL abort_signal_handler(int)
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (Settings::User().Get<Settings::Setting::EnableSelfInitiatedMinidump>())
{
Debugging::WriteMinidump();
}
#endif
std::_Exit(APPINSTALLER_CLI_ERROR_INTERNAL_ERROR);
}
}
int CoreMain(int argc, wchar_t const** argv) try
{
std::signal(SIGABRT, abort_signal_handler);
init_apartment();
#ifndef AICLI_DISABLE_TEST_HOOKS
if (Settings::User().Get<Settings::Setting::EnableSelfInitiatedMinidump>())
{
Debugging::EnableSelfInitiatedMinidump();
}
#endif
Logging::UseGlobalTelemetryLoggerActivityIdOnly();
Execution::Context context{ std::cout, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
Logging::Log().EnableChannel(Settings::User().Get<Settings::Setting::LoggingChannelPreference>());
Logging::Log().SetLevel(Settings::User().Get<Settings::Setting::LoggingLevelPreference>());
Logging::FileLogger::Add();
Logging::EnableWilFailureTelemetry();
// Set output to UTF8
ConsoleOutputCPRestore utf8CP(CP_UTF8);
Logging::Telemetry().SetCaller("winget-cli");
Logging::Telemetry().LogStartup();
#ifndef AICLI_DISABLE_TEST_HOOKS
if (!Settings::User().Get<Settings::Setting::KeepAllLogFiles>())
#endif
{
// Initiate the background cleanup of the log file location.
Logging::FileLogger::BeginCleanup();
}
context.EnableSignalTerminationHandler();
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
{
// Block CLI execution if WinGetCommandLineInterfaces is disabled by Policy
if (!Settings::GroupPolicies().IsEnabled(Settings::TogglePolicy::Policy::WinGetCommandLineInterfaces))
{
AICLI_LOG(CLI, Error, << "WinGet is disabled by group policy");
throw Settings::GroupPolicyException(Settings::TogglePolicy::Policy::WinGetCommandLineInterfaces);
}
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);
context.UpdateForArgs();
context.SetExecutingCommand(command.get());
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;
}
catch (const Settings::GroupPolicyException& e)
{
// Report any action blocked by Group Policy.
auto policy = Settings::TogglePolicy::GetPolicy(e.Policy());
AICLI_LOG(CLI, Error, << "Operation blocked by Group Policy: " << policy.RegValueName());
context.Reporter.Error() << Resource::String::DisabledByGroupPolicy(policy.PolicyName()) << std::endl;
return APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY;
}
return Execute(context, command);
}
// 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;
}
void ServerInitialize()
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (Settings::User().Get<Settings::Setting::EnableSelfInitiatedMinidump>())
{
Debugging::EnableSelfInitiatedMinidump();
}
#endif
AppInstaller::CLI::Execution::COMContext::SetLoggers();
}
void InProcInitialize()
{
#ifndef AICLI_DISABLE_TEST_HOOKS
if (Settings::User().Get<Settings::Setting::EnableSelfInitiatedMinidump>())
{
Debugging::EnableSelfInitiatedMinidump();
}
#endif
// Explicitly set default channel and level before user settings from PackageManagerSettings
AppInstaller::CLI::Execution::COMContext::SetLoggers(AppInstaller::Logging::Channel::Defaults, AppInstaller::Logging::Level::Info);
}
}