|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +namespace Microsoft.WingetCreateCLI.Commands; |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using CommandLine; |
| 12 | +using Microsoft.WingetCreateCLI.Commands.DscCommands; |
| 13 | +using Microsoft.WingetCreateCLI.Logging; |
| 14 | +using Microsoft.WingetCreateCLI.Properties; |
| 15 | +using Newtonsoft.Json.Linq; |
| 16 | + |
| 17 | +/// <summary> |
| 18 | +/// Command for managing the application using dsc v3. |
| 19 | +/// </summary> |
| 20 | +[Verb("dsc", HelpText = "DscCommand_HelpText", ResourceType = typeof(Resources))] |
| 21 | +public class DscCommand : BaseCommand |
| 22 | +{ |
| 23 | + /// <inheritdoc/> |
| 24 | + public override bool AcceptsGitHubToken => false; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Gets or sets the name of the resource to be managed by the dsc command. |
| 28 | + /// </summary> |
| 29 | + [Value(0, MetaName = "ResourceName", Required = true, HelpText = "DscResourceName_HelpText", ResourceType = typeof(Resources))] |
| 30 | + public string ResourceName { get; set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Gets or sets the input for the dsc command. |
| 34 | + /// </summary> |
| 35 | + [Value(1, MetaName = "Input", Required = false, HelpText = "DscInput_HelpText", ResourceType = typeof(Resources))] |
| 36 | + public string Input { get; set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets or sets a value indicating whether to execute the dsc Get operation. |
| 40 | + /// </summary> |
| 41 | + [Option('g', "get", SetName = "GetMethod", HelpText = "DscGet_HelpText", ResourceType = typeof(Resources))] |
| 42 | + public bool Get { get; set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets or sets a value indicating whether to execute the dsc Set operation. |
| 46 | + /// </summary> |
| 47 | + [Option('s', "set", SetName = "SetMethod", HelpText = "DscSet_HelpText", ResourceType = typeof(Resources))] |
| 48 | + public bool Set { get; set; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets or sets a value indicating whether to execute the dsc Test operation. |
| 52 | + /// </summary> |
| 53 | + [Option('t', "test", SetName = "TestMethod", HelpText = "DscTest_HelpText", ResourceType = typeof(Resources))] |
| 54 | + public bool Test { get; set; } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets or sets a value indicating whether to execute the dsc Export operation. |
| 58 | + /// </summary> |
| 59 | + [Option('e', "export", SetName = "ExportMethod", HelpText = "DscExport_HelpText", ResourceType = typeof(Resources))] |
| 60 | + public bool Export { get; set; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets or sets a value indicating whether to execute the schema command. |
| 64 | + /// </summary> |
| 65 | + [Option("schema", SetName = "SchemaMethod", HelpText = "DscSchema_HelpText", ResourceType = typeof(Resources))] |
| 66 | + public bool Schema { get; set; } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Executes the dsc command flow. |
| 70 | + /// </summary> |
| 71 | + /// <returns>Boolean representing success or fail of the command.</returns> |
| 72 | + public override async Task<bool> Execute() |
| 73 | + { |
| 74 | + await Task.CompletedTask; |
| 75 | + |
| 76 | + if (!BaseDscCommand.TryCreateInstance(this.ResourceName, out var dscCommand)) |
| 77 | + { |
| 78 | + var availableResources = string.Join(", ", BaseDscCommand.GetAvailableCommands()); |
| 79 | + Logger.ErrorLocalized(nameof(Resources.DscResourceNameNotFound_Message), this.ResourceName, availableResources); |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + var input = string.IsNullOrWhiteSpace(this.Input) ? null : JToken.Parse(this.Input); |
| 86 | + var operations = new (bool, Func<bool>)[] |
| 87 | + { |
| 88 | + (this.Get, () => dscCommand.Get(input)), |
| 89 | + (this.Set, () => dscCommand.Set(input)), |
| 90 | + (this.Test, () => dscCommand.Test(input)), |
| 91 | + (this.Export, () => dscCommand.Export(input)), |
| 92 | + (this.Schema, () => dscCommand.Schema()), |
| 93 | + }; |
| 94 | + |
| 95 | + foreach (var (methodFlag, methodAction) in operations) |
| 96 | + { |
| 97 | + if (methodFlag) |
| 98 | + { |
| 99 | + if (!methodAction()) |
| 100 | + { |
| 101 | + Logger.ErrorLocalized(nameof(Resources.DscResourceOperationFailed_Message)); |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + Logger.ErrorLocalized(nameof(Resources.DscResourceOperationNotSpecified_Message)); |
| 110 | + return false; |
| 111 | + } |
| 112 | + catch (Exception ex) |
| 113 | + { |
| 114 | + Logger.Error(ex.Message); |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments