winget-cli

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

DscCommandBase.h (5111B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include "Command.h"
      5 #include <json/json.h>
      6 #include <optional>
      7 
      8 #ifndef USE_PROD_CLSIDS
      9 #define WINGET_DSCV3_MODULE_NAME "Microsoft.WinGet.Dev"
     10 #define WINGET_DSCV3_MODULE_NAME_WIDE L"Microsoft.WinGet.Dev"
     11 #else
     12 #define WINGET_DSCV3_MODULE_NAME "Microsoft.WinGet"
     13 #define WINGET_DSCV3_MODULE_NAME_WIDE L"Microsoft.WinGet"
     14 #endif
     15 
     16 namespace AppInstaller::CLI
     17 {
     18     // The kind of resource that this command is implementing.
     19     enum class DscResourceKind
     20     {
     21         // Standard resource
     22         Resource,
     23         // Group resource
     24         Group,
     25         // Adapter resource
     26         Adapter,
     27         // Import resource
     28         Import,
     29     };
     30 
     31     // The functions that a DSC resource can provide.
     32     enum class DscFunctions
     33     {
     34         None        = 0x000,
     35         // Gets the current state; should always be implemented.
     36         Get         = 0x001,
     37         // Sets the state; should always be implemented.
     38         Set         = 0x002,
     39         // Produces the output of Set without modifying state.
     40         WhatIf      = 0x004,
     41         // Determines if the current state matches the given state.
     42         Test        = 0x008,
     43         // Deletes the given state.
     44         Delete      = 0x010,
     45         // Gets all instances of the resource.
     46         Export      = 0x020,
     47         // Required for a Group resource, ignored for all others.
     48         Validate    = 0x040,
     49         // Required for an Import resource.
     50         Resolve     = 0x080,
     51         // Required for an Adapter resource.
     52         Adapter     = 0x100,
     53         // Gets the schema for the resource's properties.
     54         Schema      = 0x200,
     55     };
     56 
     57     DEFINE_ENUM_FLAG_OPERATORS(DscFunctions);
     58 
     59     // Behavior changes for DSC functions.
     60     enum class DscFunctionModifiers
     61     {
     62         None                = 0x00,
     63         // The resource implements a check during Set (and WhatIf) to determine if already in the correct state.
     64         // If not provided, DSC will ensure that the state is tested beforehand.
     65         ImplementsPretest   = 0x01,
     66         // The resource will act on the `_exist` property during Set (and WhatIf).
     67         // If not provided, the resource should implement Delete.
     68         HandlesExist        = 0x02,
     69         // Functions that may return state information (set, what-if, test) return only the state.
     70         ReturnsState        = 0x04,
     71         // Functions that may return state information (set, what-if, test) return the state and property difference.
     72         ReturnsStateAndDiff = 0x08,
     73     };
     74 
     75     DEFINE_ENUM_FLAG_OPERATORS(DscFunctionModifiers);
     76 
     77     // Provides infrastructure for DSC commands to be implemented.
     78     struct DscCommandBase : public Command
     79     {
     80         DscCommandBase(std::string_view parent, std::string_view resourceName, DscResourceKind kind, DscFunctions functions, DscFunctionModifiers modifiers);
     81 
     82         std::vector<Argument> GetArguments() const override;
     83 
     84         Utility::LocIndView HelpLink() const override;
     85 
     86         static constexpr std::string_view ModuleName()
     87         {
     88             return WINGET_DSCV3_MODULE_NAME;
     89         }
     90 
     91         // Writes the manifest for the command to the file path.
     92         // If the path is empty, writes the manifest to the output stream.
     93         void WriteManifest(Execution::Context& context, const std::filesystem::path& filePath) const;
     94 
     95     protected:
     96         void ExecuteInternal(Execution::Context& context) const override;
     97 
     98         // Gets the resource specific type name.
     99         virtual std::string ResourceType() const = 0;
    100 
    101         virtual void ResourceFunctionGet(Execution::Context& context) const;
    102         virtual void ResourceFunctionSet(Execution::Context& context) const;
    103         virtual void ResourceFunctionWhatIf(Execution::Context& context) const;
    104         virtual void ResourceFunctionTest(Execution::Context& context) const;
    105         virtual void ResourceFunctionDelete(Execution::Context& context) const;
    106         virtual void ResourceFunctionExport(Execution::Context& context) const;
    107         virtual void ResourceFunctionValidate(Execution::Context& context) const;
    108         virtual void ResourceFunctionResolve(Execution::Context& context) const;
    109         virtual void ResourceFunctionAdapter(Execution::Context& context) const;
    110         virtual void ResourceFunctionSchema(Execution::Context& context) const;
    111         virtual void ResourceFunctionManifest(Execution::Context& context) const;
    112 
    113         // Parses a JSON object from stdin.
    114         std::optional<Json::Value> GetJsonFromInput(Execution::Context& context, bool terminateContextOnError = true) const;
    115 
    116         // Writes the value to the context output.
    117         void WriteJsonOutputLine(Execution::Context& context, const Json::Value& value) const;
    118 
    119     private:
    120         DscResourceKind m_kind = DscResourceKind::Resource;
    121         DscFunctions m_functions = DscFunctions::None;
    122         DscFunctionModifiers m_modifiers = DscFunctionModifiers::None;
    123     };
    124 }