'use strict' ; import * as vscode from 'vscode' ; import { UserInputUtil , IBlockchainQuickPickItem } from './UserInputUtil' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { FabricEnvironmentRegistry , FabricEnvironmentRegistryEntry , LogType , FabricGatewayRegistryEntry , EnvironmentType , FabricRuntimeUtil } from 'ibm-blockchain-platform-common' ; import { FabricEnvironmentTreeItem } from '../explorer/runtimeOps/disconnectedTree/FabricEnvironmentTreeItem' ; import { FabricEnvironmentManager } from '../fabric/environments/FabricEnvironmentManager' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { FabricGatewayConnectionManager } from '../fabric/FabricGatewayConnectionManager' ; import { SettingConfigurations } from '../configurations' ; import { GlobalState , ExtensionData } from '../util/GlobalState' ; import { LocalMicroEnvironmentManager } from '../fabric/environments/LocalMicroEnvironmentManager' ; export async function deleteEnvironment ( environment , force = false , ignoreRefresh = false ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , `delete environment` ) ; let environmentsToDelete ; try { if ( ! environment ) { const environments = await FabricEnvironmentRegistry . instance ( ) . getAll ( ) ; if ( environments . length === 0 ) { outputAdapter . log ( LogType . ERROR , `No environments to delete.` ) ; return ; } const chosenEnvironment = await UserInputUtil . showFabricEnvironmentQuickPickBox ( 'Choose the environment(s) that you want to delete' , true , false ) as IBlockchainQuickPickItem < FabricEnvironmentRegistryEntry > [ ] ; if ( ! chosenEnvironment || chosenEnvironment . length === 0 ) { return ; } environmentsToDelete = chosenEnvironment . map ( ( _environment ) => { return _environment . data ; } ) ; } else { if ( environment instanceof FabricEnvironmentTreeItem ) { environmentsToDelete = [ environment . environmentRegistryEntry ] ; } else { environmentsToDelete = [ environment ] ; } } if ( ! force ) { const reallyDoIt = await UserInputUtil . showConfirmationWarningMessage ( `This will remove the environment(s). Do you want to continue?` ) ; if ( ! reallyDoIt ) { return ; } } const connectedEnvironmentRegistry = FabricEnvironmentManager . instance ( ) . getEnvironmentRegistryEntry ( ) ; const connectedGatewayRegistry = await FabricGatewayConnectionManager . instance ( ) . getGatewayRegistryEntry ( ) ; const _settings = vscode . workspace . getConfiguration ( ) . get ( SettingConfigurations . FABRIC_RUNTIME , vscode . ConfigurationTarget . Global ) ; const localSettings = JSON . parse ( JSON . stringify ( _settings ) ) ; for ( const _environment of environmentsToDelete ) { if ( connectedEnvironmentRegistry && connectedEnvironmentRegistry . name === _environment . name ) { await vscode . commands . executeCommand ( ExtensionCommands . DISCONNECT_ENVIRONMENT ) ; } if ( connectedGatewayRegistry && connectedGatewayRegistry . fromEnvironment === _environment . name ) { await vscode . commands . executeCommand ( ExtensionCommands . DISCONNECT_GATEWAY ) ; } if ( _environment . environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT || _environment . environmentType === EnvironmentType . LOCAL_ENVIRONMENT ) { delete localSettings [ _environment . name ] ; try { await vscode . commands . executeCommand ( ExtensionCommands . TEARDOWN_FABRIC , undefined , true , _environment . name , ignoreRefresh ) ; } catch ( error ) { outputAdapter . log ( LogType . WARNING , undefined , `Error whilst tearing down ${ _environment . name } environment: ${ error . message }` ) ; } if ( _environment . name === FabricRuntimeUtil . LOCAL_FABRIC ) { const extensionData = GlobalState . get ( ) ; extensionData . deletedOneOrgLocalFabric = true ; await GlobalState . update ( extensionData ) ; } } await FabricEnvironmentRegistry . instance ( ) . delete ( _environment . name ) ; if ( _environment . environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { LocalMicroEnvironmentManager . instance ( ) . removeRuntime ( _environment . name ) ; } } await vscode . workspace . getConfiguration ( ) . update ( SettingConfigurations . FABRIC_RUNTIME , localSettings , vscode . ConfigurationTarget . Global ) ; if ( environmentsToDelete . length > 1 ) { outputAdapter . log ( LogType . SUCCESS , `Successfully deleted environments` ) ; return ; } else { outputAdapter . log ( LogType . SUCCESS , `Successfully deleted ${ environmentsToDelete [ 0 ] . name } environment` ) ; return ; } } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Error deleting environment: ${ error . message }` , `Error deleting environment: ${ error . toString ( ) }` ) ; } }