Skip to content

Commit 8dbbb6a

Browse files
authored
Add cmdlet to enable/disable az predictor (#14188)
* Add cmdlet. * Modify the parmaeter values.
1 parent db56960 commit 8dbbb6a

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ PowerShellVersion = '7.1'
5050

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

53+
CmdletsToExport = @("Enable-AzPredictor", "Disable-AzPredictor")
54+
5355
# Format files (.ps1xml) to be loaded when importing this module
5456

5557
# 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.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
using System;
16+
using System.Text;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
20+
{
21+
/// <summary>
22+
/// A cmdlet that disable Az Predictor.
23+
/// </summary>
24+
[Cmdlet("Disable", "AzPredictor")]
25+
public sealed class DisableAzPredictor : PSCmdlet
26+
{
27+
private static readonly string[] _DisableStatements = {
28+
"Set-PSReadLineOption -PredictionSource History"
29+
};
30+
31+
/// <summary>
32+
/// Gets and sets the session that this cmdlet applies to.
33+
/// </summary>
34+
[Parameter(Position = 0)]
35+
[ValidateSet(nameof(SessionParameterValue.All), nameof(SessionParameterValue.Current))]
36+
public SessionParameterValue Session { get; set; }
37+
38+
/// <summary>
39+
/// Indicates whether the user would like to receive output.
40+
/// </summary>
41+
[Parameter(Mandatory = false)]
42+
public SwitchParameter PassThru { get; set; }
43+
44+
/// <inheritdoc/>
45+
protected override void ProcessRecord()
46+
{
47+
var scriptToRun = new StringBuilder();
48+
var _ = scriptToRun.Append(DisableAzPredictor._DisableStatements[0]);
49+
50+
if (Session == SessionParameterValue.All)
51+
{
52+
_ = 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\"");
53+
}
54+
55+
InvokeCommand.InvokeScript(scriptToRun.ToString());
56+
57+
if (PassThru.IsPresent)
58+
{
59+
WriteObject(true);
60+
}
61+
}
62+
}
63+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
using System;
16+
using System.Text;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
20+
{
21+
/// <summary>
22+
/// A cmdlet that enables Az Predictor with default settings.
23+
/// </summary>
24+
[Cmdlet("Enable", "AzPredictor"), OutputType(typeof(bool))]
25+
public sealed class EnableAzPredictor : PSCmdlet
26+
{
27+
private static readonly string[] _EnableStatements = {
28+
"Import-Module Az.Tools.Predictor",
29+
"Set-PSReadLineOption -PredictionSource HistoryAndPlugin"
30+
};
31+
32+
/// <summary>
33+
/// Gets and sets the session that this cmdlet applies to.
34+
/// </summary>
35+
[Parameter(Position = 0)]
36+
[ValidateSet(nameof(SessionParameterValue.All), nameof(SessionParameterValue.Current))]
37+
public SessionParameterValue Session { get; set; }
38+
39+
/// <summary>
40+
/// Indicates whether the user would like to receive output.
41+
/// </summary>
42+
[Parameter(Mandatory = false)]
43+
public SwitchParameter PassThru { get; set; }
44+
45+
/// <inheritdoc/>
46+
protected override void ProcessRecord()
47+
{
48+
var scriptToRun = new StringBuilder();
49+
var _ = scriptToRun.Append(EnableAzPredictor._EnableStatements[1]);
50+
51+
if (Session == SessionParameterValue.All)
52+
{
53+
_ = scriptToRun.Append($";Add-Content -Path $PROFILE -Value \"`n{string.Join("`n", EnableAzPredictor._EnableStatements)}\" -NoNewline -Encoding UTF8 -Force")
54+
.Append($";Write-Host \"User profile ($PROFILE) has been updated.`n\"");
55+
}
56+
57+
InvokeCommand.InvokeScript(scriptToRun.ToString());
58+
59+
if (PassThru.IsPresent)
60+
{
61+
WriteObject(true);
62+
}
63+
}
64+
}
65+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.Azure.PowerShell.Tools.AzPredictor
16+
{
17+
/// <summary>
18+
/// The value used in the parameter -Session in the cmdlets.
19+
/// </summary>
20+
public enum SessionParameterValue
21+
{
22+
/// <value>
23+
/// All the session.
24+
/// </value>
25+
All,
26+
27+
/// <value>
28+
/// The current session.
29+
/// </value>
30+
Current
31+
}
32+
}

0 commit comments

Comments
 (0)