CompleteCommand.cpp (3254B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "CompleteCommand.h" 5 #include "RootCommand.h" 6 #include "Resources.h" 7 8 namespace AppInstaller::CLI 9 { 10 using namespace std::string_view_literals; 11 using namespace Execution; 12 13 std::vector<Argument> CompleteCommand::GetArguments() const 14 { 15 return { 16 Argument{ Args::Type::Word, Resource::String::WordArgumentDescription, ArgumentType::Standard, Argument::Visibility::Example, true }, 17 Argument{ Args::Type::CommandLine, Resource::String::CommandLineArgumentDescription, ArgumentType::Standard, Argument::Visibility::Example, true }, 18 Argument{ Args::Type::Position, Resource::String::PositionArgumentDescription, ArgumentType::Standard, Argument::Visibility::Example, true }, 19 }; 20 } 21 22 Resource::LocString CompleteCommand::ShortDescription() const 23 { 24 return { Resource::String::CompleteCommandShortDescription }; 25 } 26 27 Resource::LocString CompleteCommand::LongDescription() const 28 { 29 return { Resource::String::CompleteCommandLongDescription }; 30 } 31 32 Utility::LocIndView CompleteCommand::HelpLink() const 33 { 34 return "https://aka.ms/winget-command-complete"_liv; 35 } 36 37 void CompleteCommand::ExecuteInternal(Execution::Context& context) const 38 { 39 try 40 { 41 CompletionData data{ 42 context.Args.GetArg(Args::Type::Word), 43 context.Args.GetArg(Args::Type::CommandLine), 44 context.Args.GetArg(Args::Type::Position) }; 45 46 std::unique_ptr<Command> command = std::make_unique<RootCommand>(); 47 48 std::unique_ptr<Command> subCommand = command->FindSubCommand(data.BeforeWord()); 49 while (subCommand) 50 { 51 command = std::move(subCommand); 52 subCommand = command->FindSubCommand(data.BeforeWord()); 53 } 54 55 // Create a new Context to execute the Complete from 56 auto subContextPtr = context.CreateSubContext(); 57 Context& subContext = *subContextPtr; 58 auto previousThreadGlobals = subContext.SetForCurrentThread(); 59 60 subContext.Reporter.SetChannel(Execution::Reporter::Channel::Completion); 61 subContext.Add<Data::CompletionData>(std::move(data)); 62 63 // Disable all telemetry while doing a completion 64 Logging::DisableTelemetryScope disable; 65 66 AICLI_LOG(CLI, Info, << "Complete handing off to command " << command->FullName()); 67 command->Complete(subContext); 68 } 69 catch (const CommandException& ce) 70 { 71 AICLI_LOG(CLI, Info, << "Error encountered during completion, ignoring: " << ce.Message()); 72 } 73 catch (const Settings::GroupPolicyException& e) 74 { 75 AICLI_LOG(CLI, Info, << "Error encountered during completion, ignoring: Blocked by Group Policy " << Settings::TogglePolicy::GetPolicy(e.Policy()).RegValueName()); 76 } 77 catch (...) 78 { 79 AICLI_LOG(CLI, Info, << "Error encountered during completion, ignoring..."); 80 } 81 } 82 }