AddSourceCmdlet.cs (2868B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="AddSourceCmdlet.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Client.Cmdlets.Cmdlets 8 { 9 using System.Management.Automation; 10 using Microsoft.WinGet.Client.Cmdlets.PSObjects; 11 using Microsoft.WinGet.Client.Common; 12 using Microsoft.WinGet.Client.Engine.Commands; 13 14 /// <summary> 15 /// Adds a source. Requires admin. 16 /// </summary> 17 [Cmdlet(VerbsCommon.Add, Constants.WinGetNouns.Source)] 18 [Alias("awgs")] 19 public sealed class AddSourceCmdlet : PSCmdlet 20 { 21 /// <summary> 22 /// Gets or sets the name of the source to add. 23 /// </summary> 24 [Parameter( 25 Mandatory = true, 26 ValueFromPipeline = true, 27 ValueFromPipelineByPropertyName = true)] 28 public string Name { get; set; } 29 30 /// <summary> 31 /// Gets or sets the argument of the source to add. 32 /// </summary> 33 [Parameter( 34 Mandatory = true, 35 ValueFromPipeline = true, 36 ValueFromPipelineByPropertyName = true)] 37 public string Argument { get; set; } 38 39 /// <summary> 40 /// Gets or sets the type of the source to add. 41 /// </summary> 42 [Parameter( 43 ValueFromPipeline = true, 44 ValueFromPipelineByPropertyName = true)] 45 [ValidateSet( 46 "Microsoft.Rest", 47 "Microsoft.PreIndexed.Package")] 48 public string Type { get; set; } 49 50 /// <summary> 51 /// Gets or sets the trust level of the source to add. 52 /// </summary> 53 [Parameter(ValueFromPipelineByPropertyName = true)] 54 public PSSourceTrustLevel TrustLevel { get; set; } = PSSourceTrustLevel.Default; 55 56 /// <summary> 57 /// Gets or sets a value indicating whether the source to add is explicit. 58 /// </summary> 59 /// 60 [Parameter(ValueFromPipelineByPropertyName = true)] 61 public SwitchParameter Explicit { get; set; } 62 63 /// <summary> 64 /// Adds source. 65 /// </summary> 66 protected override void ProcessRecord() 67 { 68 var command = new CliCommand(this); 69 command.AddSource(this.Name, this.Argument, this.Type, this.ConvertPSSourceTrustLevelToString(this.TrustLevel), this.Explicit.ToBool()); 70 } 71 72 private string ConvertPSSourceTrustLevelToString(PSSourceTrustLevel trustLevel) => trustLevel switch 73 { 74 PSSourceTrustLevel.Default => string.Empty, 75 _ => trustLevel.ToString(), 76 }; 77 } 78 }