Skip to content

Commit

Permalink
Add cmdlet to enable/disable az predictor (#14188)
Browse files Browse the repository at this point in the history
* Add cmdlet.

* Modify the parmaeter values.
  • Loading branch information
kceiw authored Feb 12, 2021
1 parent db56960 commit 8dbbb6a
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ PowerShellVersion = '7.1'

NestedModules = @("Microsoft.Azure.PowerShell.Tools.AzPredictor.dll")

CmdletsToExport = @("Enable-AzPredictor", "Disable-AzPredictor")

# Format files (.ps1xml) to be loaded when importing this module

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Text;
using System.Management.Automation;

namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
{
/// <summary>
/// A cmdlet that disable Az Predictor.
/// </summary>
[Cmdlet("Disable", "AzPredictor")]
public sealed class DisableAzPredictor : PSCmdlet
{
private static readonly string[] _DisableStatements = {
"Set-PSReadLineOption -PredictionSource History"
};

/// <summary>
/// Gets and sets the session that this cmdlet applies to.
/// </summary>
[Parameter(Position = 0)]
[ValidateSet(nameof(SessionParameterValue.All), nameof(SessionParameterValue.Current))]
public SessionParameterValue Session { get; set; }

/// <summary>
/// Indicates whether the user would like to receive output.
/// </summary>
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

/// <inheritdoc/>
protected override void ProcessRecord()
{
var scriptToRun = new StringBuilder();
var _ = scriptToRun.Append(DisableAzPredictor._DisableStatements[0]);

if (Session == SessionParameterValue.All)
{
_ = scriptToRun.Append(";Write-Host \"To disable Az Predictor, please edit your profile ($PROFILE) and remove the following lines:`nImport-Module Az.Tools.Predictor`nSet-PSReadLineOption -PredictionSource HistoryAndPlugin`n\"");
}

InvokeCommand.InvokeScript(scriptToRun.ToString());

if (PassThru.IsPresent)
{
WriteObject(true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Text;
using System.Management.Automation;

namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
{
/// <summary>
/// A cmdlet that enables Az Predictor with default settings.
/// </summary>
[Cmdlet("Enable", "AzPredictor"), OutputType(typeof(bool))]
public sealed class EnableAzPredictor : PSCmdlet
{
private static readonly string[] _EnableStatements = {
"Import-Module Az.Tools.Predictor",
"Set-PSReadLineOption -PredictionSource HistoryAndPlugin"
};

/// <summary>
/// Gets and sets the session that this cmdlet applies to.
/// </summary>
[Parameter(Position = 0)]
[ValidateSet(nameof(SessionParameterValue.All), nameof(SessionParameterValue.Current))]
public SessionParameterValue Session { get; set; }

/// <summary>
/// Indicates whether the user would like to receive output.
/// </summary>
[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

/// <inheritdoc/>
protected override void ProcessRecord()
{
var scriptToRun = new StringBuilder();
var _ = scriptToRun.Append(EnableAzPredictor._EnableStatements[1]);

if (Session == SessionParameterValue.All)
{
_ = scriptToRun.Append($";Add-Content -Path $PROFILE -Value \"`n{string.Join("`n", EnableAzPredictor._EnableStatements)}\" -NoNewline -Encoding UTF8 -Force")
.Append($";Write-Host \"User profile ($PROFILE) has been updated.`n\"");
}

InvokeCommand.InvokeScript(scriptToRun.ToString());

if (PassThru.IsPresent)
{
WriteObject(true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
{
/// <summary>
/// The value used in the parameter -Session in the cmdlets.
/// </summary>
public enum SessionParameterValue
{
/// <value>
/// All the session.
/// </value>
All,

/// <value>
/// The current session.
/// </value>
Current
}
}

0 comments on commit 8dbbb6a

Please sign in to comment.