-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cmdlet to enable/disable az predictor (#14188)
* Add cmdlet. * Modify the parmaeter values.
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tools/Az.Tools.Predictor/Az.Tools.Predictor/Commands/DisableAzPredictor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
tools/Az.Tools.Predictor/Az.Tools.Predictor/Commands/EnableAzPredictor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tools/Az.Tools.Predictor/Az.Tools.Predictor/Commands/SessionParameterValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |