forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationCommon.cpp
More file actions
95 lines (82 loc) · 3.89 KB
/
Copy pathConfigurationCommon.cpp
File metadata and controls
95 lines (82 loc) · 3.89 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "ConfigurationCommon.h"
#include "ExecutionArgs.h"
#include <AppInstallerRuntime.h>
#include "Command.h"
#include <winrt/Microsoft.Management.Configuration.SetProcessorFactory.h>
#include <filesystem>
namespace AppInstaller::CLI
{
using namespace winrt::Microsoft::Management::Configuration;
namespace
{
constexpr std::string_view s_ModulePath_Default = "default";
constexpr std::string_view s_ModulePath_CurrentUser = "currentuser";
constexpr std::string_view s_ModulePath_AllUsers = "allusers";
struct ModulePathInfo
{
SetProcessorFactory::PwshConfigurationProcessorLocation location;
std::optional<std::string_view> customLocation;
};
ModulePathInfo GetModulePathInfo(Execution::Args& execArgs)
{
if (execArgs.Contains(Execution::Args::Type::ConfigurationModulePath))
{
auto modulePath = execArgs.GetArg(Execution::Args::Type::ConfigurationModulePath);
if (Utility::CaseInsensitiveEquals(modulePath, s_ModulePath_Default))
{
return { SetProcessorFactory::PwshConfigurationProcessorLocation::Default, {} };
}
else if (Utility::CaseInsensitiveEquals(modulePath, s_ModulePath_CurrentUser))
{
return { SetProcessorFactory::PwshConfigurationProcessorLocation::CurrentUser, {} };
}
else if (Utility::CaseInsensitiveEquals(modulePath, s_ModulePath_AllUsers))
{
return { SetProcessorFactory::PwshConfigurationProcessorLocation::AllUsers, {} };
}
else
{
return { SetProcessorFactory::PwshConfigurationProcessorLocation::Custom, execArgs.GetArg(Execution::Args::Type::ConfigurationModulePath) };
}
}
return { SetProcessorFactory::PwshConfigurationProcessorLocation::WinGetModulePath, {} };
}
}
namespace Configuration
{
void ValidateCommonArguments(Execution::Args& execArgs, bool requireConfigurationSetChoice)
{
auto modulePath = GetModulePathInfo(execArgs);
if (modulePath.location == SetProcessorFactory::PwshConfigurationProcessorLocation::AllUsers && !Runtime::IsRunningAsAdmin())
{
throw CommandException(Resource::String::ConfigurationAllUsersElevated);
}
if (modulePath.location == SetProcessorFactory::PwshConfigurationProcessorLocation::Custom)
{
auto path = std::filesystem::path{ modulePath.customLocation.value() };
if (!path.is_absolute())
{
throw CommandException(Resource::String::ConfigurationModulePathArgError);
}
}
if (requireConfigurationSetChoice &&
!WI_IsFlagSet(Argument::GetCategoriesPresent(execArgs), ArgTypeCategory::ConfigurationSetChoice))
{
throw CommandException(Resource::String::RequiredArgError("file"_liv));
}
}
void SetModulePath(Execution::Context& context, IConfigurationSetProcessorFactory const& factory)
{
auto pwshFactory = factory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>();
auto modulePath = GetModulePathInfo(context.Args);
if (modulePath.location == SetProcessorFactory::PwshConfigurationProcessorLocation::Custom)
{
pwshFactory.CustomLocation(winrt::to_hstring(modulePath.customLocation.value()));
}
pwshFactory.Location(modulePath.location);
}
}
}