winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

UninstallCommand.cpp (5313B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "UninstallCommand.h"
      5 #include "Workflows/UninstallFlow.h"
      6 #include "Workflows/CompletionFlow.h"
      7 #include "Workflows/WorkflowBase.h"
      8 #include "Workflows/MultiQueryFlow.h"
      9 #include "Resources.h"
     10 
     11 namespace AppInstaller::CLI
     12 {
     13     using AppInstaller::CLI::Execution::Args;
     14     using namespace AppInstaller::CLI::Workflow;
     15 
     16     std::vector<Argument> UninstallCommand::GetArguments() const
     17     {
     18         return
     19         {
     20             Argument::ForType(Args::Type::MultiQuery),
     21             Argument::ForType(Args::Type::Manifest),
     22             Argument::ForType(Args::Type::Id),
     23             Argument::ForType(Args::Type::Name),
     24             Argument::ForType(Args::Type::Moniker),
     25             Argument::ForType(Args::Type::ProductCode),
     26             Argument::ForType(Args::Type::TargetVersion),
     27             Argument::ForType(Args::Type::AllVersions),
     28             Argument::ForType(Args::Type::Channel),
     29             Argument::ForType(Args::Type::Source),
     30             Argument::ForType(Args::Type::Exact),
     31             Argument{ Args::Type::InstallScope, Resource::String::InstalledScopeArgumentDescription, ArgumentType::Standard, Argument::Visibility::Help },
     32             Argument::ForType(Args::Type::Interactive),
     33             Argument::ForType(Args::Type::Silent),
     34             Argument::ForType(Args::Type::Force),
     35             Argument::ForType(Args::Type::Purge),
     36             Argument::ForType(Args::Type::Preserve),
     37             Argument::ForType(Args::Type::Log),
     38             Argument::ForType(Args::Type::CustomHeader),
     39             Argument::ForType(Args::Type::AuthenticationMode),
     40             Argument::ForType(Args::Type::AuthenticationAccount),
     41             Argument::ForType(Args::Type::AcceptSourceAgreements),
     42         };
     43     }
     44 
     45     Resource::LocString UninstallCommand::ShortDescription() const
     46     {
     47         return { Resource::String::UninstallCommandShortDescription };
     48     }
     49 
     50     Resource::LocString UninstallCommand::LongDescription() const
     51     {
     52         return { Resource::String::UninstallCommandLongDescription };
     53     }
     54 
     55     void UninstallCommand::Complete(Execution::Context& context, Execution::Args::Type valueType) const
     56     {
     57         if (valueType == Execution::Args::Type::Manifest ||
     58             valueType == Execution::Args::Type::Log)
     59         {
     60             // Intentionally output nothing to allow pass through to filesystem.
     61             return;
     62         }
     63 
     64         context <<
     65             Workflow::OpenSource() <<
     66             Workflow::OpenCompositeSource(Repository::PredefinedSource::Installed);
     67 
     68         switch (valueType)
     69         {
     70         case Execution::Args::Type::MultiQuery:
     71             context <<
     72                 Workflow::RequireCompletionWordNonEmpty <<
     73                 Workflow::SearchSourceForManyCompletion <<
     74                 Workflow::CompleteWithMatchedField;
     75             break;
     76         case Execution::Args::Type::Id:
     77         case Execution::Args::Type::Name:
     78         case Execution::Args::Type::Moniker:
     79         case Execution::Args::Type::TargetVersion:
     80         case Execution::Args::Type::Channel:
     81         case Execution::Args::Type::Source:
     82         case Execution::Args::Type::ProductCode:
     83             context <<
     84                 Workflow::CompleteWithSingleSemanticsForValueUsingExistingSource(valueType);
     85             break;
     86         }
     87     }
     88 
     89     Utility::LocIndView UninstallCommand::HelpLink() const
     90     {
     91         return "https://aka.ms/winget-command-uninstall"_liv;
     92     }
     93 
     94     void UninstallCommand::ValidateArgumentsInternal(Execution::Args& execArgs) const
     95     {
     96         Argument::ValidateCommonArguments(execArgs);
     97     }
     98 
     99     void UninstallCommand::ExecuteInternal(Execution::Context& context) const
    100     {
    101         context.SetFlags(Execution::ContextFlag::TreatSourceFailuresAsWarning);
    102 
    103         // open the sources where to search for the package
    104         context <<
    105             Workflow::ReportExecutionStage(ExecutionStage::Discovery) <<
    106             Workflow::OpenSource() <<
    107             Workflow::OpenCompositeSource(Workflow::DetermineInstalledSource(context));
    108 
    109         // find the uninstaller
    110         if (context.Args.Contains(Execution::Args::Type::Manifest))
    111         {
    112             // --manifest case where new manifest is provided
    113             context <<
    114                 Workflow::GetManifestFromArg <<
    115                 Workflow::SearchSourceUsingManifest <<
    116                 Workflow::EnsureOneMatchFromSearchResult(OperationType::Uninstall) <<
    117                 Workflow::UninstallSinglePackage;
    118         }
    119         else
    120         {
    121             // search for specific packages to uninstall
    122             if (!context.Args.Contains(Execution::Args::Type::MultiQuery))
    123             {
    124                 context <<
    125                     Workflow::SearchSourceForSingle <<
    126                     Workflow::HandleSearchResultFailures <<
    127                     Workflow::EnsureOneMatchFromSearchResult(OperationType::Uninstall) <<
    128                     Workflow::UninstallSinglePackage;
    129             }
    130             else
    131             {
    132                 context <<
    133                     Workflow::GetMultiSearchRequests <<
    134                     Workflow::SearchSubContextsForSingle(OperationType::Uninstall) <<
    135                     Workflow::UninstallMultiplePackages;
    136             }
    137         }
    138     }
    139 }