CliCommand.cs (4535B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="CliCommand.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Client.Engine.Commands 8 { 9 using System.Management.Automation; 10 using Microsoft.WinGet.Client.Engine.Commands.Common; 11 using Microsoft.WinGet.Client.Engine.Common; 12 using Microsoft.WinGet.Client.Engine.Helpers; 13 using Microsoft.WinGet.Common.Command; 14 15 /// <summary> 16 /// Commands that just calls winget.exe underneath. 17 /// </summary> 18 public sealed class CliCommand : BaseCommand 19 { 20 /// <summary> 21 /// Initializes a new instance of the <see cref="CliCommand"/> class. 22 /// </summary> 23 /// <param name="psCmdlet">PSCmdlet.</param> 24 public CliCommand(PSCmdlet psCmdlet) 25 : base(psCmdlet) 26 { 27 } 28 29 /// <summary> 30 /// Enables admin setting. 31 /// </summary> 32 /// <param name="name">Setting name.</param> 33 public void EnableSetting(string name) 34 { 35 Utilities.VerifyAdmin(); 36 _ = this.Run("settings", $"--enable \"{name}\""); 37 } 38 39 /// <summary> 40 /// Disables admin setting. 41 /// </summary> 42 /// <param name="name">Setting name.</param> 43 public void DisableSetting(string name) 44 { 45 Utilities.VerifyAdmin(); 46 _ = this.Run("settings", $"--disable \"{name}\""); 47 } 48 49 /// <summary> 50 /// Gets winget settings. 51 /// </summary> 52 /// <param name="asPlainText">Return as string.</param> 53 public void GetSettings(bool asPlainText) 54 { 55 var result = this.Run("settings", "export"); 56 57 if (asPlainText) 58 { 59 this.Write(StreamType.Object, result.StdOut); 60 } 61 else 62 { 63 this.Write(StreamType.Object, Utilities.ConvertToHashtable(result.StdOut)); 64 } 65 } 66 67 /// <summary> 68 /// Adds source. 69 /// </summary> 70 /// <param name="name">Name of source.</param> 71 /// <param name="arg">Arg of source.</param> 72 /// <param name="type">Type of source.</param> 73 /// <param name="trustLevel">Trust level of source.</param> 74 /// <param name="isExplicit">Make source explicit.</param> 75 public void AddSource(string name, string arg, string type, string trustLevel, bool isExplicit) 76 { 77 Utilities.VerifyAdmin(); 78 string parameters = $"add --name \"{name}\" --arg \"{arg}\""; 79 80 if (!string.IsNullOrEmpty(type)) 81 { 82 parameters += $" --type \"{type}\""; 83 } 84 85 if (!string.IsNullOrEmpty(trustLevel)) 86 { 87 parameters += $" --trust-level \"{trustLevel}\""; 88 } 89 90 if (isExplicit) 91 { 92 parameters += " --explicit"; 93 } 94 95 _ = this.Run("source", parameters, 300000); 96 } 97 98 /// <summary> 99 /// Removes source. 100 /// </summary> 101 /// <param name="name">Name of source.</param> 102 public void RemoveSource(string name) 103 { 104 Utilities.VerifyAdmin(); 105 _ = this.Run("source", $"remove --name \"{name}\""); 106 } 107 108 /// <summary> 109 /// Resets a source. 110 /// </summary> 111 /// <param name="name">Name of source.</param> 112 public void ResetSourceByName(string name) 113 { 114 Utilities.VerifyAdmin(); 115 _ = this.Run("source", $"reset --name \"{name}\" --force"); 116 } 117 118 /// <summary> 119 /// Resets all sources and adds the defaults. 120 /// </summary> 121 public void ResetAllSources() 122 { 123 Utilities.VerifyAdmin(); 124 _ = this.Run("source", $"reset --force"); 125 } 126 127 private WinGetCLICommandResult Run(string command, string parameters, int timeOut = 60000) 128 { 129 var wingetCliWrapper = new WingetCLIWrapper(); 130 var result = wingetCliWrapper.RunCommand(this, command, parameters, timeOut); 131 result.VerifyExitCode(); 132 133 return result; 134 } 135 } 136 }