|
| 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 Microsoft.Azure.Commands.Common.Exceptions; |
| 16 | +using Microsoft.Azure.Commands.ResourceManager.Common; |
| 17 | +using Microsoft.Azure.PowerShell.Common.Config; |
| 18 | +using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; |
| 19 | +using System.Management.Automation; |
| 20 | + |
| 21 | +namespace Microsoft.Azure.Commands.Common.Authentication.Config |
| 22 | +{ |
| 23 | + [Cmdlet("Export", AzureRMConstants.AzureRMPrefix + "Config", SupportsShouldProcess = true)] |
| 24 | + [CmdletPreview(ConfigCommandBase.PreviewMessage)] |
| 25 | + [OutputType(typeof(bool))] |
| 26 | + public class ExportConfigCommand : AzureRMCmdlet |
| 27 | + { |
| 28 | + [Parameter(Position = 1, Mandatory = true, HelpMessage = "Specifies the path of the file to which to save the configs.")] |
| 29 | + [ValidateNotNullOrEmpty] |
| 30 | + public string Path { get; set; } |
| 31 | + |
| 32 | + [Parameter(HelpMessage = "Overwrites the given file if it exists.")] |
| 33 | + public SwitchParameter Force { get; set; } |
| 34 | + |
| 35 | + [Parameter(HelpMessage = "Returns a boolean value indicating success or failure.")] |
| 36 | + public SwitchParameter PassThru { get; set; } |
| 37 | + |
| 38 | + public override void ExecuteCmdlet() |
| 39 | + { |
| 40 | + base.ExecuteCmdlet(); |
| 41 | + var dataStore = AzureSession.Instance.DataStore; |
| 42 | + AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager); |
| 43 | + if (!dataStore.FileExists(configManager.ConfigFilePath)) |
| 44 | + { |
| 45 | + throw new AzPSApplicationException("No configs to export. Make sure at least one config is set by `Update-AzConfig` with the `CurrentUser` scope.", ErrorKind.UserError); |
| 46 | + } |
| 47 | + |
| 48 | + Path = ResolveUserPath(Path); |
| 49 | + WriteDebugWithTimestamp($"[{nameof(ExportConfigCommand)}] Exporting configs to {Path}."); |
| 50 | + ConfirmAction( |
| 51 | + Force, |
| 52 | + $"Overwrite existing file at {Path}", |
| 53 | + $"Export configs to file at {Path}", |
| 54 | + $"all the configs at {ConfigScope.CurrentUser} scope", |
| 55 | + () => |
| 56 | + { |
| 57 | + new JsonConfigHelper(configManager.ConfigFilePath, dataStore).ExportConfigFile(Path); |
| 58 | + WriteDebugWithTimestamp($"[{nameof(ExportConfigCommand)}] Configs are exported to {Path} successfully."); |
| 59 | + if (PassThru) |
| 60 | + { |
| 61 | + WriteObject(true); |
| 62 | + } |
| 63 | + }, |
| 64 | + () => dataStore.FileExists(Path)); // only ask for confirmation if file exists |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments