import * as vscode from 'vscode' ; import { Platform , platform } from "./shell" ; export const VS_KUBE_K3D_CFG_KEY = "k3d" ; export const VS_KUBE_CFG_KEY = "vs-kubernetes" ; export const VS_KUBE_K3D_UPDATE_KUBECONFIG_CFG_KEY = `${ VS_KUBE_K3D_CFG_KEY }.updateKubeconfig` ; export const VS_KUBE_K3D_UPDATE_CHANNEL_CFG_KEY = `${ VS_KUBE_K3D_CFG_KEY }.updateChannel` ; export const VS_KUBE_K3D_DRAIN_CFG_KEY = `${ VS_KUBE_K3D_CFG_KEY }.drainBeforeDelete` ; export const VS_KUBE_K3D_REPLACE_CONTEXT_CFG_KEY = `${ VS_KUBE_K3D_CFG_KEY }.replaceContext` ; export const VS_KUBE_K3D_CREATE_DEFAULS_CFG_KEY = `${ VS_KUBE_K3D_CFG_KEY }.defaults` ; export const VS_KUBE_K3D_IMAGES_CFG_KEY = `${ VS_KUBE_K3D_CFG_KEY }.images` ; export const VS_KUBE_K3D_DOCKERHOST_CFG_KEY = `${ VS_KUBE_K3D_CFG_KEY }.dockerHost` ; const USE_WSL_KEY = "use-wsl" ; export enum UpdateKubeconfig { OnCreate = 1 , OnDelete , Always , Never } export function getK3DConfigUpdateKubeconfig ( ) { const config = vscode . workspace . getConfiguration ( ) ; const value = config . get < string > ( VS_KUBE_K3D_UPDATE_KUBECONFIG_CFG_KEY , "always" ) ; switch ( value ) { case "onCreate" : return UpdateKubeconfig . OnCreate ; case "onDelete" : return UpdateKubeconfig . OnDelete ; case "always" : return UpdateKubeconfig . Always ; case "never" : return UpdateKubeconfig . Never ; } return undefined ; } export enum ReplaceContext { NewCluster , OldestCluster } export function getK3DReplaceContext ( ) { const config = vscode . workspace . getConfiguration ( ) ; const value = config . get < string > ( VS_KUBE_K3D_REPLACE_CONTEXT_CFG_KEY , "new" ) ; switch ( value ) { case "new" : return ReplaceContext . NewCluster ; case "oldest" : return ReplaceContext . OldestCluster ; } return undefined ; } export function getK3DConfigCreateDefaults < T > ( key ) { const config = vscode . workspace . getConfiguration ( VS_KUBE_K3D_CREATE_DEFAULS_CFG_KEY ) ; return config . get < T > ( `${ VS_KUBE_K3D_CREATE_DEFAULS_CFG_KEY }.${ key }` ) ; } export function getK3DConfigImages ( key , fallback ) { const baseKey = `${ VS_KUBE_K3D_IMAGES_CFG_KEY }.${ key }` ; const configKey = enclosingKey ( baseKey ) ; const config = vscode . workspace . getConfiguration ( configKey ) [ baseKey ] ; return config ? config : fallback ; } export enum UpdateChannel { Stable = 1 , All } export function getK3DConfigUpdateChannel ( ) { const config = vscode . workspace . getConfiguration ( ) ; const value = config . get < string > ( VS_KUBE_K3D_UPDATE_CHANNEL_CFG_KEY , "stable" ) ; switch ( value ) { case "stable" : return UpdateChannel . Stable ; case "all" : return UpdateChannel . All ; } return undefined ; } export function getK3DConfigDrainBeforeDelete ( ) { const config = vscode . workspace . getConfiguration ( ) ; const value = config . get < boolean > ( VS_KUBE_K3D_DRAIN_CFG_KEY , true ) ; return value ? value : true ; } export function getK3DDockerHost ( ) { const config = vscode . workspace . getConfiguration ( ) ; const k3dDockerHost = config . get < string > ( VS_KUBE_K3D_DOCKERHOST_CFG_KEY ) ; if ( k3dDockerHost ) { return k3dDockerHost ; } const extensionDockerHost = config . get < string > ( "docker.host" ) ; if ( extensionDockerHost ) { return extensionDockerHost ; } const dockerHost = process . env [ 'DOCKER_HOST' ] ; if ( dockerHost ) { return dockerHost ; } return undefined ; } export function getK3DConfigPathFor ( tool ) { const baseKey = getK3DKeyFor ( tool ) ; const configKey = enclosingKey ( baseKey ) ; const os = platform ( ) ; const osOverridePath = vscode . workspace . getConfiguration ( configKey ) [ osOverrideKey ( os , baseKey ) ] ; return osOverridePath || vscode . workspace . getConfiguration ( configKey ) [ baseKey ] ; } export function getUseWsl ( ) { return vscode . workspace . getConfiguration ( VS_KUBE_CFG_KEY ) [ USE_WSL_KEY ] ; } export async function addPathToConfig ( configKey , value ) { await setConfigValue ( configKey , value ) ; } async function setConfigValue ( configKey , value ) { await atAllConfigScopes ( addValueToConfigAtScope , configKey , value ) ; } async function addValueToConfigAtScope ( configKey , value , scope , valueAtScope , createIfNotExist ) { if ( ! createIfNotExist ) { if ( ! valueAtScope || ! ( valueAtScope [ configKey ] ) ) { return ; } } let newValue = { } ; if ( valueAtScope ) { newValue = Object . assign ( { } , valueAtScope ) ; } newValue [ configKey ] = value ; await vscode . workspace . getConfiguration ( ) . update ( enclosingKey ( configKey ) , newValue , scope ) ; } type ConfigUpdater < T > = ( configKey : string , value : T , scope : vscode . ConfigurationTarget , valueAtScope : any , createIfNotExist : boolean ) => Promise < void > ; async function atAllConfigScopes < T > ( fn , configKey , value ) { const config = vscode . workspace . getConfiguration ( ) . inspect ( enclosingKey ( configKey ) ) ! ; await fn ( configKey , value , vscode . ConfigurationTarget . Global , config . globalValue , true ) ; await fn ( configKey , value , vscode . ConfigurationTarget . Workspace , config . workspaceValue , false ) ; await fn ( configKey , value , vscode . ConfigurationTarget . WorkspaceFolder , config . workspaceFolderValue , false ) ; } export function toolPathOSKey ( os , tool ) { const baseKey = getK3DKeyFor ( tool ) ; const osSpecificKey = osOverrideKey ( os , baseKey ) ; return osSpecificKey ; } export function getK3DKeyFor ( tool ) { return `${ VS_KUBE_K3D_CFG_KEY }.paths.${ tool }` ; } export function osOverrideKey ( os , baseKey ) { const osKey = osKeyString ( os ) ; return osKey ? `${ baseKey }-${ osKey }` : baseKey ; } function osKeyString ( os ) { switch ( os ) { case Platform . Windows : return 'windows' ; case Platform . MacOS : return 'mac' ; case Platform . Linux : return 'linux' ; default : return null ; } } function enclosingKey ( configKey ) { const enclosingKeyElements = configKey . split ( "." ) ; return enclosingKeyElements . slice ( 0 , - 1 ) . join ( "." ) ; }