|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Management.Automation; |
| 8 | +using System.Management.Automation.Internal; |
| 9 | +using OutGridView.Models; |
| 10 | + |
| 11 | +namespace OutGridView.Cmdlet |
| 12 | +{ |
| 13 | + [Cmdlet("Show", "ObjectTree")] |
| 14 | + [Alias("sot")] |
| 15 | + public class ShowObjectTreeCmdletCommand : PSCmdlet, IDisposable |
| 16 | + { |
| 17 | + #region Properties |
| 18 | + |
| 19 | + private const string DataNotQualifiedForShowObjectTree = nameof(DataNotQualifiedForShowObjectTree); |
| 20 | + private const string EnvironmentNotSupportedForShowObjectTree = nameof(EnvironmentNotSupportedForShowObjectTree); |
| 21 | + |
| 22 | + private List<PSObject> _psObjects = new List<PSObject>(); |
| 23 | + |
| 24 | + #endregion Properties |
| 25 | + |
| 26 | + #region Input Parameters |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// This parameter specifies the current pipeline object. |
| 30 | + /// </summary> |
| 31 | + [Parameter(ValueFromPipeline = true, HelpMessage = "Specifies the input pipeline object")] |
| 32 | + public PSObject InputObject { get; set; } = AutomationNull.Value; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets/sets the title of the Out-GridView window. |
| 36 | + /// </summary> |
| 37 | + [Parameter(HelpMessage = "Specifies the text that appears in the title bar of the Out-ConsoleGridView window. y default, the title bar displays the command that invokes Out-ConsoleGridView.")] |
| 38 | + [ValidateNotNullOrEmpty] |
| 39 | + public string Title { get; set; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// gets or sets the initial value for the filter in the GUI |
| 43 | + /// </summary> |
| 44 | + [Parameter(HelpMessage = "Pre-populates the Filter edit box, allowing filtering to be specified on the command line. The filter uses regular expressions." )] |
| 45 | + public string Filter { set; get; } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// gets or sets the whether "minimum UI" mode will be enabled |
| 49 | + /// </summary> |
| 50 | + [Parameter(HelpMessage = "If specified no window frame, filter box, or status bar will be displayed in the GUI.")] |
| 51 | + public SwitchParameter MinUI { set; get; } |
| 52 | + /// <summary> |
| 53 | + /// gets or sets the whether the Terminal.Gui System.Net.Console-based ConsoleDriver will be used instead of the |
| 54 | + /// default platform-specific (Windows or Curses) ConsoleDriver. |
| 55 | + /// </summary> |
| 56 | + [Parameter(HelpMessage = "If specified the Terminal.Gui System.Net.Console-based ConsoleDriver (NetDriver) will be used.")] |
| 57 | + public SwitchParameter UseNetDriver { set; get; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// For the -Debug switch |
| 61 | + /// </summary> |
| 62 | + public bool Debug => MyInvocation.BoundParameters.TryGetValue("Debug", out var o); |
| 63 | + |
| 64 | + #endregion Input Parameters |
| 65 | + |
| 66 | + // This method gets called once for each cmdlet in the pipeline when the pipeline starts executing |
| 67 | + protected override void BeginProcessing() |
| 68 | + { |
| 69 | + if (Console.IsInputRedirected) |
| 70 | + { |
| 71 | + ErrorRecord error = new ErrorRecord( |
| 72 | + new PSNotSupportedException("Not supported in this environment (when input is redirected)."), |
| 73 | + EnvironmentNotSupportedForShowObjectTree, |
| 74 | + ErrorCategory.NotImplemented, |
| 75 | + null); |
| 76 | + |
| 77 | + ThrowTerminatingError(error); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called |
| 82 | + protected override void ProcessRecord() |
| 83 | + { |
| 84 | + if (InputObject == null || InputObject == AutomationNull.Value) |
| 85 | + { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (InputObject.BaseObject is IDictionary dictionary) |
| 90 | + { |
| 91 | + // Dictionaries should be enumerated through because the pipeline does not enumerate through them. |
| 92 | + foreach (DictionaryEntry entry in dictionary) |
| 93 | + { |
| 94 | + ProcessObject(PSObject.AsPSObject(entry)); |
| 95 | + } |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + ProcessObject(InputObject); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void ProcessObject(PSObject input) |
| 104 | + { |
| 105 | + |
| 106 | + object baseObject = input.BaseObject; |
| 107 | + |
| 108 | + // Throw a terminating error for types that are not supported. |
| 109 | + if (baseObject is ScriptBlock || |
| 110 | + baseObject is SwitchParameter || |
| 111 | + baseObject is PSReference || |
| 112 | + baseObject is PSObject) |
| 113 | + { |
| 114 | + ErrorRecord error = new ErrorRecord( |
| 115 | + new FormatException("Invalid data type for Show-ObjectTree"), |
| 116 | + DataNotQualifiedForShowObjectTree, |
| 117 | + ErrorCategory.InvalidType, |
| 118 | + null); |
| 119 | + |
| 120 | + ThrowTerminatingError(error); |
| 121 | + } |
| 122 | + |
| 123 | + _psObjects.Add(input); |
| 124 | + } |
| 125 | + |
| 126 | + // This method will be called once at the end of pipeline execution; if no input is received, this method is not called |
| 127 | + protected override void EndProcessing() |
| 128 | + { |
| 129 | + base.EndProcessing(); |
| 130 | + |
| 131 | + //Return if no objects |
| 132 | + if (_psObjects.Count == 0) |
| 133 | + { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + var applicationData = new ApplicationData |
| 138 | + { |
| 139 | + Title = Title ?? "Show-ObjectTree", |
| 140 | + Filter = Filter, |
| 141 | + MinUI = MinUI, |
| 142 | + UseNetDriver = UseNetDriver, |
| 143 | + Debug = Debug, |
| 144 | + ModuleVersion = MyInvocation.MyCommand.Version.ToString() |
| 145 | + }; |
| 146 | + |
| 147 | + ShowObjectView.Run(_psObjects, applicationData); |
| 148 | + } |
| 149 | + |
| 150 | + public void Dispose() |
| 151 | + { |
| 152 | + |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments