ConfigurationCommon.cpp (4316B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "ConfigurationCommon.h" 5 #include "ExecutionArgs.h" 6 #include <AppInstallerRuntime.h> 7 #include "Command.h" 8 #include <winrt/Microsoft.Management.Configuration.SetProcessorFactory.h> 9 #include <filesystem> 10 11 namespace AppInstaller::CLI 12 { 13 using namespace winrt::Microsoft::Management::Configuration; 14 15 namespace 16 { 17 constexpr std::string_view s_ModulePath_Default = "default"; 18 constexpr std::string_view s_ModulePath_CurrentUser = "currentuser"; 19 constexpr std::string_view s_ModulePath_AllUsers = "allusers"; 20 21 struct ModulePathInfo 22 { 23 SetProcessorFactory::PwshConfigurationProcessorLocation location; 24 std::optional<std::string> customLocation; 25 }; 26 27 ModulePathInfo GetModulePathInfo(Execution::Args& execArgs) 28 { 29 if (execArgs.Contains(Execution::Args::Type::ConfigurationModulePath)) 30 { 31 auto modulePath = execArgs.GetArg(Execution::Args::Type::ConfigurationModulePath); 32 33 if (Utility::CaseInsensitiveEquals(modulePath, s_ModulePath_Default)) 34 { 35 return { SetProcessorFactory::PwshConfigurationProcessorLocation::Default, {} }; 36 } 37 else if (Utility::CaseInsensitiveEquals(modulePath, s_ModulePath_CurrentUser)) 38 { 39 return { SetProcessorFactory::PwshConfigurationProcessorLocation::CurrentUser, {} }; 40 } 41 else if (Utility::CaseInsensitiveEquals(modulePath, s_ModulePath_AllUsers)) 42 { 43 return { SetProcessorFactory::PwshConfigurationProcessorLocation::AllUsers, {} }; 44 } 45 else 46 { 47 return { SetProcessorFactory::PwshConfigurationProcessorLocation::Custom, std::string(execArgs.GetArg(Execution::Args::Type::ConfigurationModulePath)) }; 48 } 49 } 50 51 std::filesystem::path defaultModuleRoot = Settings::User().Get<Settings::Setting::ConfigureDefaultModuleRoot>(); 52 53 if (!defaultModuleRoot.empty()) 54 { 55 return { SetProcessorFactory::PwshConfigurationProcessorLocation::Custom, defaultModuleRoot.u8string() }; 56 } 57 58 return { SetProcessorFactory::PwshConfigurationProcessorLocation::WinGetModulePath, {} }; 59 } 60 } 61 62 namespace Configuration 63 { 64 void ValidateCommonArguments(Execution::Args& execArgs, bool requireConfigurationSetChoice) 65 { 66 auto modulePath = GetModulePathInfo(execArgs); 67 68 if (modulePath.location == SetProcessorFactory::PwshConfigurationProcessorLocation::AllUsers && !Runtime::IsRunningAsAdmin()) 69 { 70 throw CommandException(Resource::String::ConfigurationAllUsersElevated); 71 } 72 73 if (modulePath.location == SetProcessorFactory::PwshConfigurationProcessorLocation::Custom) 74 { 75 auto path = std::filesystem::path{ modulePath.customLocation.value() }; 76 if (!path.is_absolute()) 77 { 78 throw CommandException(Resource::String::ConfigurationModulePathArgError); 79 } 80 } 81 82 if (requireConfigurationSetChoice && 83 !WI_IsFlagSet(Argument::GetCategoriesPresent(execArgs), ArgTypeCategory::ConfigurationSetChoice)) 84 { 85 throw CommandException(Resource::String::RequiredArgError("file"_liv)); 86 } 87 } 88 89 void SetModulePath(Execution::Context& context, IConfigurationSetProcessorFactory const& factory) 90 { 91 auto pwshFactory = factory.as<SetProcessorFactory::IPwshConfigurationSetProcessorFactoryProperties>(); 92 auto modulePath = GetModulePathInfo(context.Args); 93 94 if (modulePath.location == SetProcessorFactory::PwshConfigurationProcessorLocation::Custom) 95 { 96 pwshFactory.CustomLocation(winrt::to_hstring(modulePath.customLocation.value())); 97 } 98 99 pwshFactory.Location(modulePath.location); 100 } 101 } 102 }