import path from 'path' ; import { Uri , window , workspace } from 'vscode' ; import { AzureClusterProvider , azureTools , isAzureProvider } from '../azure/azureTools' ; import { fluxTools } from '../flux/fluxTools' ; import { validateKustomizationName } from '../flux/fluxUtils' ; import { checkIfOpenedFolderGitRepositorySourceExists } from '../git/checkIfOpenedFolderGitRepositorySourceExists' ; import { ClusterContextNode } from '../views/nodes/clusterContextNode' ; import { getCurrentClusterInfo , refreshWorkloadsTreeView } from '../views/treeViews' ; import { createGitRepository } from './createGitRepository' ; param fileExplorerUri export async function createKustomization ( fileExplorerUri ? ) { const currentClusterInfo = await getCurrentClusterInfo ( ) ; if ( ! currentClusterInfo ) { return ; } let kustomizationFsPath = '' ; let relativeKustomizationPath = '' ; /** Use this fsPath to create Git Repository when it doesn't exist  */ let workspaceFolderUri ; if ( fileExplorerUri ) { kustomizationFsPath = fileExplorerUri . fsPath ; } else { const pickedFolder = await window . showOpenDialog ( { title : 'Select a folder (used for "path" property on the new Kustomization object)' , canSelectFiles : false , canSelectFolders : true , canSelectMany : false , defaultUri : workspace . workspaceFolders ?. [ 0 ] . uri , } ) ; if ( ! pickedFolder ) { return ; } kustomizationFsPath = pickedFolder [ 0 ] . fsPath ; workspaceFolderUri = pickedFolder [ 0 ] ; } for ( const folder of workspace . workspaceFolders || [ ] ) { const relativePath = path . relative ( folder . uri . fsPath , kustomizationFsPath ) ; if ( relativePath && ! relativePath . startsWith ( '..' ) && ! path . isAbsolute ( relativePath ) ) { relativeKustomizationPath = relativePath ; if ( ! workspaceFolderUri ) { workspaceFolderUri = folder . uri ; } break ; } } if ( ! workspaceFolderUri ) { window . showErrorMessage ( 'Failed to infer the opened workspace folder Uri.' ) ; return ; } let gitSourceExists = await checkIfOpenedFolderGitRepositorySourceExists ( ) ; let gitRepositoryName = gitSourceExists ?. gitRepositoryName ; if ( isAzureProvider ( currentClusterInfo . clusterProvider ) ) { await createKustomizationAzureCluster ( gitRepositoryName , relativeKustomizationPath , workspaceFolderUri , currentClusterInfo . clusterNode , currentClusterInfo . clusterProvider ) ; } else { await createKustomizationGenericCluster ( gitRepositoryName , relativeKustomizationPath , workspaceFolderUri ) ; } refreshWorkloadsTreeView ( ) ; } param gitRepositoryName param relativeKustomizationPath param workspaceFolderUri async function createKustomizationGenericCluster ( gitRepositoryName , relativeKustomizationPath , workspaceFolderUri ) { if ( ! gitRepositoryName ) { const createGitSourceButton = 'Create Git Repository' ; const modalResult = await window . showWarningMessage ( 'Git repository source does not exist yet.' , { modal : true , } , createGitSourceButton ) ; if ( modalResult !== createGitSourceButton ) { return ; } gitRepositoryName = await createGitRepository ( workspaceFolderUri ) ; } if ( ! gitRepositoryName ) { window . showErrorMessage ( 'Failed to get Git Repository Name' ) ; return ; } let newKustomizationName = await window . showInputBox ( { title : 'Kustomization Name' , value : `${ gitRepositoryName }-kustomization` , validateInput : value => validateKustomizationName ( value , gitRepositoryName , false ) , } ) ; if ( newKustomizationName === undefined ) { return ; } if ( ! newKustomizationName ) { newKustomizationName = gitRepositoryName ; } await fluxTools . createKustomization ( newKustomizationName , gitRepositoryName , relativeKustomizationPath ) ; } param gitRepositoryName param relativeKustomizationPath param workspaceFolderUri async function createKustomizationAzureCluster ( gitRepositoryName , relativeKustomizationPath , workspaceFolderUri , clusterNode , clusterProvider ) { let newKustomizationName = await window . showInputBox ( { title : 'Kustomization Name' , value : '' , validateInput : value => validateKustomizationName ( value , gitRepositoryName , true ) , } ) ; if ( newKustomizationName === undefined ) { return ; } if ( ! gitRepositoryName ) { const createGitSourceButton = 'Create Git Repository' ; const modalResult = await window . showWarningMessage ( 'Git repository source does not exist yet.' , { modal : true , } , createGitSourceButton ) ; if ( modalResult !== createGitSourceButton ) { return ; } await createGitRepository ( workspaceFolderUri , newKustomizationName , relativeKustomizationPath ) ; } else { await azureTools . createKustomization ( newKustomizationName , gitRepositoryName , relativeKustomizationPath , clusterNode , clusterProvider ) ; } }