SourceCommand.cpp (10101B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "SourceCommand.h" 5 #include "Workflows/CompletionFlow.h" 6 #include "Workflows/SourceFlow.h" 7 #include "Workflows/WorkflowBase.h" 8 #include "Resources.h" 9 10 namespace AppInstaller::CLI 11 { 12 using namespace AppInstaller::CLI::Execution; 13 using namespace std::string_view_literals; 14 15 Utility::LocIndView s_SourceCommand_HelpLink = "https://aka.ms/winget-command-source"_liv; 16 17 std::vector<std::unique_ptr<Command>> SourceCommand::GetCommands() const 18 { 19 return InitializeFromMoveOnly<std::vector<std::unique_ptr<Command>>>({ 20 std::make_unique<SourceAddCommand>(FullName()), 21 std::make_unique<SourceListCommand>(FullName()), 22 std::make_unique<SourceUpdateCommand>(FullName()), 23 std::make_unique<SourceRemoveCommand>(FullName()), 24 std::make_unique<SourceResetCommand>(FullName()), 25 std::make_unique<SourceExportCommand>(FullName()), 26 }); 27 } 28 29 Resource::LocString SourceCommand::ShortDescription() const 30 { 31 return { Resource::String::SourceCommandShortDescription }; 32 } 33 34 Resource::LocString SourceCommand::LongDescription() const 35 { 36 return { Resource::String::SourceCommandLongDescription }; 37 } 38 39 Utility::LocIndView SourceCommand::HelpLink() const 40 { 41 return s_SourceCommand_HelpLink; 42 } 43 44 void SourceCommand::ExecuteInternal(Context& context) const 45 { 46 OutputHelp(context.Reporter); 47 } 48 49 std::vector<Argument> SourceAddCommand::GetArguments() const 50 { 51 return { 52 Argument::ForType(Args::Type::SourceName).SetRequired(true), 53 Argument::ForType(Args::Type::SourceArg), 54 Argument::ForType(Args::Type::SourceType), 55 Argument::ForType(Args::Type::SourceTrustLevel), 56 Argument::ForType(Args::Type::CustomHeader), 57 Argument::ForType(Args::Type::AcceptSourceAgreements), 58 Argument::ForType(Args::Type::SourceExplicit), 59 }; 60 } 61 62 Resource::LocString SourceAddCommand::ShortDescription() const 63 { 64 return { Resource::String::SourceAddCommandShortDescription }; 65 } 66 67 Resource::LocString SourceAddCommand::LongDescription() const 68 { 69 return { Resource::String::SourceAddCommandLongDescription }; 70 } 71 72 Utility::LocIndView SourceAddCommand::HelpLink() const 73 { 74 return s_SourceCommand_HelpLink; 75 } 76 77 void SourceAddCommand::ValidateArgumentsInternal(Args& execArgs) const 78 { 79 if (execArgs.Contains(Execution::Args::Type::SourceTrustLevel)) 80 { 81 try 82 { 83 std::string trustLevelArg = std::string{ execArgs.GetArg(Execution::Args::Type::SourceTrustLevel) }; 84 85 for (auto trustLevel : Utility::Split(trustLevelArg, '|', true)) 86 { 87 Repository::ConvertToSourceTrustLevelEnum(trustLevel); 88 } 89 } 90 catch (...) 91 { 92 auto validOptions = std::vector<Utility::LocIndString>{ 93 Utility::LocIndString{ Repository::SourceTrustLevelEnumToString(Repository::SourceTrustLevel::None) }, 94 Utility::LocIndString{ Repository::SourceTrustLevelEnumToString(Repository::SourceTrustLevel::Trusted) } }; 95 throw CommandException(Resource::String::InvalidArgumentValueError(ArgumentCommon::ForType(Execution::Args::Type::SourceTrustLevel).Name, Utility::Join(","_liv, validOptions))); 96 } 97 } 98 } 99 100 void SourceAddCommand::ExecuteInternal(Context& context) const 101 { 102 // Note: Group Policy for allowed sources is enforced at the RepositoryCore level 103 // as we need to validate the source data and handle sources that were already added. 104 context << 105 Workflow::EnsureRunningAsAdmin << 106 Workflow::GetSourceList << 107 Workflow::CheckSourceListAgainstAdd << 108 Workflow::CreateSourceForSourceAdd << 109 Workflow::AddSource; 110 } 111 112 std::vector<Argument> SourceListCommand::GetArguments() const 113 { 114 return { 115 Argument::ForType(Args::Type::SourceName), 116 }; 117 } 118 119 Resource::LocString SourceListCommand::ShortDescription() const 120 { 121 return { Resource::String::SourceListCommandShortDescription }; 122 } 123 124 Resource::LocString SourceListCommand::LongDescription() const 125 { 126 return { Resource::String::SourceListCommandLongDescription }; 127 } 128 129 void SourceListCommand::Complete(Context& context, Args::Type valueType) const 130 { 131 if (valueType == Args::Type::SourceName) 132 { 133 context << 134 Workflow::CompleteSourceName; 135 } 136 } 137 138 Utility::LocIndView SourceListCommand::HelpLink() const 139 { 140 return s_SourceCommand_HelpLink; 141 } 142 143 void SourceListCommand::ExecuteInternal(Context& context) const 144 { 145 context << 146 Workflow::GetSourceListWithFilter << 147 Workflow::ListSources; 148 } 149 150 std::vector<Argument> SourceUpdateCommand::GetArguments() const 151 { 152 return { 153 Argument::ForType(Args::Type::SourceName), 154 }; 155 } 156 157 Resource::LocString SourceUpdateCommand::ShortDescription() const 158 { 159 return { Resource::String::SourceUpdateCommandShortDescription }; 160 } 161 162 Resource::LocString SourceUpdateCommand::LongDescription() const 163 { 164 return { Resource::String::SourceUpdateCommandLongDescription }; 165 } 166 167 void SourceUpdateCommand::Complete(Context& context, Args::Type valueType) const 168 { 169 if (valueType == Args::Type::SourceName) 170 { 171 context << 172 Workflow::CompleteSourceName; 173 } 174 } 175 176 Utility::LocIndView SourceUpdateCommand::HelpLink() const 177 { 178 return s_SourceCommand_HelpLink; 179 } 180 181 void SourceUpdateCommand::ExecuteInternal(Context& context) const 182 { 183 context << 184 Workflow::GetSourceListWithFilter << 185 Workflow::UpdateSources; 186 } 187 188 std::vector<Argument> SourceRemoveCommand::GetArguments() const 189 { 190 return { 191 Argument::ForType(Args::Type::SourceName).SetRequired(true), 192 }; 193 } 194 195 Resource::LocString SourceRemoveCommand::ShortDescription() const 196 { 197 return { Resource::String::SourceRemoveCommandShortDescription }; 198 } 199 200 Resource::LocString SourceRemoveCommand::LongDescription() const 201 { 202 return { Resource::String::SourceRemoveCommandLongDescription }; 203 } 204 205 void SourceRemoveCommand::Complete(Context& context, Args::Type valueType) const 206 { 207 if (valueType == Args::Type::SourceName) 208 { 209 context << 210 Workflow::CompleteSourceName; 211 } 212 } 213 214 Utility::LocIndView SourceRemoveCommand::HelpLink() const 215 { 216 return s_SourceCommand_HelpLink; 217 } 218 219 void SourceRemoveCommand::ExecuteInternal(Context& context) const 220 { 221 // Note: Group Policy for unremovable sources is enforced at the RepositoryCore. 222 context << 223 Workflow::EnsureRunningAsAdmin << 224 Workflow::GetSourceListWithFilter << 225 Workflow::RemoveSources; 226 } 227 228 std::vector<Argument> SourceResetCommand::GetArguments() const 229 { 230 return { 231 Argument::ForType(Args::Type::SourceName), 232 Argument{ Args::Type::ForceSourceReset, Resource::String::SourceResetForceArgumentDescription, ArgumentType::Flag }, 233 }; 234 } 235 236 Resource::LocString SourceResetCommand::ShortDescription() const 237 { 238 return { Resource::String::SourceResetCommandShortDescription }; 239 } 240 241 Resource::LocString SourceResetCommand::LongDescription() const 242 { 243 return { Resource::String::SourceResetCommandLongDescription }; 244 } 245 246 void SourceResetCommand::Complete(Context& context, Args::Type valueType) const 247 { 248 if (valueType == Args::Type::SourceName) 249 { 250 context << 251 Workflow::CompleteSourceName; 252 } 253 } 254 255 Utility::LocIndView SourceResetCommand::HelpLink() const 256 { 257 return s_SourceCommand_HelpLink; 258 } 259 260 void SourceResetCommand::ExecuteInternal(Context& context) const 261 { 262 if (context.Args.Contains(Args::Type::SourceName)) 263 { 264 context << 265 Workflow::EnsureRunningAsAdmin << 266 Workflow::GetSourceListWithFilter << 267 Workflow::ResetSourceList; 268 } 269 else 270 { 271 context << 272 Workflow::EnsureRunningAsAdmin << 273 Workflow::QueryUserForSourceReset << 274 Workflow::ResetAllSources; 275 } 276 } 277 278 std::vector<Argument> SourceExportCommand::GetArguments() const 279 { 280 return { 281 Argument::ForType(Args::Type::SourceName), 282 }; 283 } 284 285 Resource::LocString SourceExportCommand::ShortDescription() const 286 { 287 return { Resource::String::SourceExportCommandShortDescription }; 288 } 289 290 Resource::LocString SourceExportCommand::LongDescription() const 291 { 292 return { Resource::String::SourceExportCommandLongDescription }; 293 } 294 295 void SourceExportCommand::Complete(Context& context, Args::Type valueType) const 296 { 297 if (valueType == Args::Type::SourceName) 298 { 299 context << 300 Workflow::CompleteSourceName; 301 } 302 } 303 304 Utility::LocIndView SourceExportCommand::HelpLink() const 305 { 306 return s_SourceCommand_HelpLink; 307 } 308 309 void SourceExportCommand::ExecuteInternal(Context& context) const 310 { 311 context << 312 Workflow::GetSourceListWithFilter << 313 Workflow::ExportSourceList; 314 } 315 }