Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmdlet to enable/disable az predictor #14188

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}