Skip to content

Commit 2cd3afb

Browse files
authored
Add WriteInformation Method in AzurePSCmdlet (#408)
* add writeinformation method * polish * fix * fix * add WriteHighlightedInformation function * remove hardcoded ansicode color theme * polish * suppress warning as error for vuluneribility * Revert style change for intercept survey and add predefine ansi color * Rename WriteStringInformation * polish code * fix comments * Update src/Common/AzurePSCmdlet.cs * Update src/Common/AzurePSCmdlet.cs
1 parent 037a53a commit 2cd3afb

File tree

6 files changed

+113
-41
lines changed

6 files changed

+113
-41
lines changed

src/Common/AzurePSCmdlet.cs

+32-39
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.Azure.ServiceManagement.Common.Models;
2121
using Microsoft.WindowsAzure.Commands.Common;
2222
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
23+
using Microsoft.WindowsAzure.Commands.Common.Properties;
2324
using Microsoft.WindowsAzure.Commands.Common.Utilities;
2425
using System;
2526
using System.Collections.Concurrent;
@@ -57,12 +58,6 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
5758
protected IList<Regex> _matchers { get; private set; } = new List<Regex>();
5859
private static readonly Regex _defaultMatcher = new Regex("(\\s*\"access_token\"\\s*:\\s*)\"[^\"]+\"");
5960

60-
// Using Ansi Code to control font color(97(Bold White)) and background color(0;120;212(RGB))
61-
private static readonly string ansiCodePrefix = "\u001b[97;48;2;0;120;212m";
62-
63-
// using '[k' for erase in line. '[0m' to ending ansi code
64-
private static readonly string ansiCodeSuffix = "\u001b[K\u001b[0m";
65-
6661
protected AzurePSDataCollectionProfile _dataCollectionProfile
6762
{
6863
get
@@ -463,40 +458,16 @@ protected bool IsVerbose()
463458

464459
protected void WriteSurvey()
465460
{
466-
HostInformationMessage newLine = new HostInformationMessage()
467-
{
468-
Message = ansiCodePrefix + "\n" + ansiCodeSuffix
469-
};
470-
HostInformationMessage howWas = new HostInformationMessage()
471-
{
472-
Message = ansiCodePrefix + "[Survey] Help us improve Azure PowerShell by sharing your experience. This survey should take about 5 minutes. Run "+ ansiCodeSuffix,
473-
NoNewLine = true
474-
};
475-
HostInformationMessage link = new HostInformationMessage()
476-
{
477-
Message = ansiCodePrefix + "'Open-AzSurveyLink'"+ ansiCodeSuffix,
478-
NoNewLine = true,
479-
};
480-
HostInformationMessage action = new HostInformationMessage()
481-
{
482-
Message = ansiCodePrefix + " to open in browser. Learn more at "+ ansiCodeSuffix,
483-
NoNewLine = true,
484-
485-
};
486-
HostInformationMessage website = new HostInformationMessage()
487-
{
488-
Message = ansiCodePrefix + "https://go.microsoft.com/fwlink/?linkid=2202892"+ ansiCodeSuffix,
489-
NoNewLine = true,
490-
};
491-
WriteInformation(newLine, new string[] { "PSHOST" });
492-
WriteInformation(howWas, new string[] { "PSHOST" });
493-
WriteInformation(link, new string[] { "PSHOST" });
494-
WriteInformation(action, new string[] { "PSHOST" });
495-
WriteInformation(website, new string[] { "PSHOST" });
496-
WriteInformation(newLine, new string[] { "PSHOST" });
497-
498-
461+
// Using color same with Azure brand event.
462+
// Using Ansi Code to control font color(97(Bold White)) and background color(0;120;212(RGB))
463+
string ansiCodePrefix = "\u001b[97;48;2;0;120;212m";
464+
// using '[k' for erase in line. '[0m' to ending ansi code
465+
string ansiCodeSuffix = "\u001b[K\u001b[0m";
466+
var website = "https://go.microsoft.com/fwlink/?linkid=2202892";
467+
WriteInformation(Environment.NewLine);
468+
WriteInformation(ansiCodePrefix + string.Format(Resources.SurveyPreface, website) + ansiCodeSuffix, false);
499469
}
470+
500471
protected new void WriteError(ErrorRecord errorRecord)
501472
{
502473
FlushDebugMessages();
@@ -551,6 +522,18 @@ protected void WriteSurvey()
551522
base.WriteWarning(text);
552523
}
553524

525+
protected new void WriteInformation(object messageData, string[] tags)
526+
{
527+
FlushDebugMessages();
528+
base.WriteInformation(messageData, tags);
529+
}
530+
531+
protected void WriteInformation(string text, bool? noNewLine = null)
532+
{
533+
HostInformationMessage message = new HostInformationMessage { Message = text, NoNewLine = noNewLine };
534+
WriteInformation(message, new string[1] { "PSHOST" });
535+
}
536+
554537
protected new void WriteCommandDetail(string text)
555538
{
556539
FlushDebugMessages();
@@ -593,6 +576,16 @@ protected void WriteWarningWithTimestamp(string message)
593576
}
594577
}
595578

579+
protected void WriteInformationWithTimestamp(string message)
580+
{
581+
if (CommandRuntime != null)
582+
{
583+
WriteInformation(
584+
new HostInformationMessage { Message = string.Format("{0:T} - {1}", DateTime.Now, message), NoNewLine = false },
585+
new string[1] { "PSHOST" });
586+
}
587+
}
588+
596589
protected void WriteDebugWithTimestamp(string message, params object[] args)
597590
{
598591
if (CommandRuntime != null)

src/Common/ColorAndFormat/PSStyle.cs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.WindowsAzure.Commands.Common
16+
{
17+
/// <summary>
18+
/// Contains configuration for how PowerShell renders text.
19+
/// A subset of https://github.com/PowerShell/PowerShell/blob/f0076b9d883aa0ed07fb2a5c2be5f38f5d945e1e/src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs#L173
20+
/// </summary>
21+
public sealed class PSStyle
22+
{
23+
/// <summary>
24+
/// Contains background colors.
25+
/// </summary>
26+
public sealed class BackgroundColor
27+
{
28+
/// <summary>
29+
/// Gets the color blue.
30+
/// </summary>
31+
public static string Blue { get; } = "\x1b[44m";
32+
}
33+
34+
public static string Reset { get; } = "\x1b[0m";
35+
36+
/// <summary>
37+
/// Gets value to turn off underlined.
38+
/// </summary>
39+
public static string UnderlineOff { get; } = "\x1b[24m";
40+
41+
/// <summary>
42+
/// Gets value to turn on underlined.
43+
/// </summary>
44+
public static string Underline { get; } = "\x1b[4m";
45+
46+
/// <summary>
47+
/// Gets value to turn off bold.
48+
/// </summary>
49+
public static string BoldOff { get; } = "\x1b[22m";
50+
51+
/// <summary>
52+
/// Gets value to turn on bold.
53+
/// </summary>
54+
public static string Bold { get; } = "\x1b[1m";
55+
}
56+
}

src/Common/Properties/Resources.Designer.cs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Common/Properties/Resources.resx

+3
Original file line numberDiff line numberDiff line change
@@ -1757,4 +1757,7 @@ Note : Go to {0} for steps to suppress this breaking change warning, and other i
17571757
<value>
17581758
- The change is expected to take effect in {0} version : '{1}'</value>
17591759
</data>
1760+
<data name="SurveyPreface" xml:space="preserve">
1761+
<value>[Survey] Help us improve Azure PowerShell by sharing your experience. This survey should take about 5 minutes. Run 'Open-AzSurveyLink' to open in browser. Learn more at {0}</value>
1762+
</data>
17601763
</root>

src/Dependencies.targets

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<PropertyGroup>
44
<IncludeSource>True</IncludeSource>
55
<IncludeSymbols>True</IncludeSymbols>
6-
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
6+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
7+
<!-- Suppress known NuGet package vulnerabilities to unblock build as we track this kind of security issues internally. -->
8+
<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>
79
</PropertyGroup>
810
<ItemGroup Condition="'$(OmitJsonPackage)' != 'true'">
911
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />

src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public abstract class AzureRMCmdlet : AzurePSCmdlet, IDynamicParameters
5353
public const string WriteDebugKey = "WriteDebug";
5454
public const string WriteVerboseKey = "WriteVerbose";
5555
public const string WriteWarningKey = "WriteWarning";
56+
public const string WriteInformationKey = "WriteInformation";
5657
public const string EnqueueDebugKey = "EnqueueDebug";
5758
private const string SubscriptionIdParameter = "SubscriptionId";
5859

@@ -240,7 +241,6 @@ private List<IAzureSubscription> CheckAccessToSubscriptions(IEnumerable<string>
240241
return subscriptionObjects;
241242
}
242243

243-
244244
private IAccessToken GetTokenForTenant(string tenantId)
245245
{
246246
return Utilities.SubscriptionAndTenantHelper.AcquireAccessToken(DefaultContext.Account,
@@ -597,6 +597,7 @@ public bool ShouldGetByName(string resourceGroupName, string name)
597597
private event EventHandler<StreamEventArgs> _writeDebugEvent;
598598
private event EventHandler<StreamEventArgs> _writeVerboseEvent;
599599
private event EventHandler<StreamEventArgs> _writeWarningEvent;
600+
private event EventHandler<StreamEventArgs> _writeInformationEvent;
600601
private event EventHandler<StreamEventArgs> _enqueueDebugEvent;
601602

602603
private void InitializeEventHandlers()
@@ -607,11 +608,14 @@ private void InitializeEventHandlers()
607608
_writeVerboseEvent += WriteVerboseSender;
608609
_writeWarningEvent -= WriteWarningSender;
609610
_writeWarningEvent += WriteWarningSender;
611+
_writeInformationEvent -= WriteInformationSender;
612+
_writeInformationEvent += WriteInformationSender;
610613
_enqueueDebugEvent -= EnqueueDebugSender;
611614
_enqueueDebugEvent += EnqueueDebugSender;
612615
AzureSession.Instance.RegisterComponent(WriteDebugKey, () => _writeDebugEvent, true);
613616
AzureSession.Instance.RegisterComponent(WriteVerboseKey, () => _writeVerboseEvent, true);
614617
AzureSession.Instance.RegisterComponent(WriteWarningKey, () => _writeWarningEvent, true);
618+
AzureSession.Instance.RegisterComponent(WriteInformationKey, () => _writeInformationEvent, true);
615619
AzureSession.Instance.RegisterComponent(EnqueueDebugKey, () => _enqueueDebugEvent, true);
616620
}
617621

@@ -630,6 +634,11 @@ private void WriteWarningSender(object sender, StreamEventArgs args)
630634
WriteWarningWithTimestamp(args.Message);
631635
}
632636

637+
private void WriteInformationSender(object sender, StreamEventArgs args)
638+
{
639+
WriteInformationWithTimestamp(args.Message);
640+
}
641+
633642
private void EnqueueDebugSender(object sender, StreamEventArgs args)
634643
{
635644
DebugMessages.Enqueue(args.Message);

0 commit comments

Comments
 (0)