import * as vscode from 'vscode' ; import * as k3d from '../k3d/k3d' ; import { tryResolveClusterNode , promptCluster } from './utils' ; import { shell } from '../utils/shell' ; import { Errorable , succeeded } from '../utils/errorable' ; import { longRunning , confirm } from '../utils/host' ; import { refreshKubernetesToolsViews } from '../utils/host' ; export async function onDeleteCluster ( target ? ) { if ( target ) { await deleteCluster ( target ) ; } else { await deleteClusterInteractive ( ) ; } } async function deleteCluster ( target ) { const clusterNode = await tryResolveClusterNode ( target ) ; if ( ! clusterNode ) { return ; } await deleteClusterByName ( clusterNode . clusterName ) ; } async function deleteClusterInteractive ( ) { const clusterName = await promptCluster ( 'Cluster to delete' , 'Getting existing clusters...' ) ; if ( ! clusterName ) { return ; } await deleteClusterByName ( clusterName ) ; } export async function deleteClusterByName ( clusterName , askUser = true ) { if ( askUser ) { const confirmed = await confirm ( `This will delete "${ clusterName }". You will not be able to undo this.` , 'Delete cluster' ) ; if ( ! confirmed ) { return ; } } const result = await longRunning ( `Deleting cluster "${ clusterName }"...` , ( ) => k3d . deleteCluster ( shell , clusterName ) ) ; await displayClusterDeletionResult ( result , clusterName ) ; } async function displayClusterDeletionResult ( result , clusterName ) { if ( succeeded ( result ) ) { await Promise . all ( [ vscode . window . showInformationMessage ( `Deleted cluster "${ clusterName }"` ) , refreshKubernetesToolsViews ( ) ] ) ; } else { await vscode . window . showErrorMessage ( `Deleting cluster "${ clusterName }" failed: ${ result . error [ 0 ] }` ) ; } }