'use strict' ; import * as vscode from 'vscode' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { FabricGatewayRegistryEntry , FabricGatewayRegistry , LogType } from 'ibm-blockchain-platform-common' ; import { IBlockchainQuickPickItem , UserInputUtil } from './UserInputUtil' ; import { InstantiatedTreeItem } from '../explorer/model/InstantiatedTreeItem' ; import { ContractTreeItem } from '../explorer/model/ContractTreeItem' ; import { FabricGatewayConnectionManager } from '../fabric/FabricGatewayConnectionManager' ; import { ExtensionCommands } from '../../ExtensionCommands' ; import { IAssociateFromViewOptions } from '../interfaces/IAssociateFromViewOptions' ; export async function dissociateTransactionDataDirectory ( chaincode ? , dissociateFromViewOptions ? ) { let gateway ; let chosenChaincode ; let chaincodeName ; let chaincodeLabel ; let channelName ; const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'dissociateTestDataDirectory' ) ; if ( dissociateFromViewOptions ) { chaincodeLabel = dissociateFromViewOptions . label ; chaincodeName = dissociateFromViewOptions . name ; channelName = dissociateFromViewOptions . channel ; } else { if ( ! chaincode ) { if ( ! FabricGatewayConnectionManager . instance ( ) . getConnection ( ) ) { await vscode . commands . executeCommand ( ExtensionCommands . CONNECT_TO_GATEWAY ) ; if ( ! FabricGatewayConnectionManager . instance ( ) . getConnection ( ) ) { return ; } } chosenChaincode = await UserInputUtil . showClientInstantiatedSmartContractsQuickPick ( 'Please choose instantiated smart contract to dissociate a transaction data directory from' , undefined , true ) ; if ( ! chosenChaincode ) { return ; } chaincodeLabel = chosenChaincode . label ; chaincodeName = chosenChaincode . data . name ; channelName = chosenChaincode . data . channel ; } else { if ( chaincode instanceof ContractTreeItem ) { chaincodeLabel = chaincode . instantiatedChaincode . label ; chaincodeName = chaincode . instantiatedChaincode . name ; channelName = chaincode . channelName ; } else { chaincodeLabel = chaincode . label ; chaincodeName = chaincode . name ; channelName = chaincode . channels [ 0 ] . label ; } } } gateway = await FabricGatewayConnectionManager . instance ( ) . getGatewayRegistryEntry ( ) ; try { const fabricGatewayRegistry = FabricGatewayRegistry . instance ( ) ; if ( ! gateway . transactionDataDirectories ) { throw new Error ( `no transaction data directories associated with ${ chaincodeLabel }` ) ; } const indexToRemove = gateway . transactionDataDirectories . findIndex ( ( item ) => { return item . chaincodeName === chaincodeName && item . channelName === channelName ; } ) ; if ( indexToRemove === - 1 ) { throw new Error ( `no transaction data directories associated with ${ chaincodeLabel }` ) ; } gateway . transactionDataDirectories . splice ( indexToRemove , 1 ) ; await fabricGatewayRegistry . update ( gateway ) ; outputAdapter . log ( LogType . SUCCESS , `Successfully dissociated "${ chaincodeLabel }" from its transaction data directory` ) ; if ( dissociateFromViewOptions ) { return undefined ; } } catch ( error ) { outputAdapter . log ( LogType . ERROR , `Unable to dissociate transaction data directory: ${ error . message }` , `Unable to dissociate transaction data directory: ${ error . toString ( ) }` ) ; } }