import * as vscode from 'vscode' ; import { Host } from '../../host' ; import { Shell , Platform } from '../../shell' ; import { Dictionary } from '../../utils/dictionary' ; const EXTENSION_CONFIG_KEY = "vs-kubernetes" ; const KUBECONFIG_PATH_KEY = "vs-kubernetes.kubeconfig" ; const KNOWN_KUBECONFIGS_KEY = "vs-kubernetes.knownKubeconfigs" ; const KUBECTL_VERSIONING_KEY = "vs-kubernetes.kubectlVersioning" ; const RESOURCES_TO_WATCH_KEY = "vs-kubernetes.resources-to-watch" ; export enum KubectlVersioning { UserProvided = 1 , Infer = 2 } export enum LogsDisplay { Webview = 1 , Terminal = 2 } 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 ( EXTENSION_CONFIG_KEY , newValue , scope ) ; } async function addValueToConfigArray ( configKey , value ) { await atAllConfigScopes ( addValueToConfigArrayAtScope , configKey , value ) ; } async function addValueToConfigArrayAtScope ( configKey , value , scope , valueAtScope , createIfNotExist ) { if ( ! createIfNotExist ) { if ( ! valueAtScope || ! ( valueAtScope [ configKey ] ) ) { return ; } } let newValue = { } ; if ( valueAtScope ) { newValue = Object . assign ( { } , valueAtScope ) ; } const arrayEntry = newValue [ configKey ] || [ ] ; arrayEntry . push ( value ) ; newValue [ configKey ] = arrayEntry ; await vscode . workspace . getConfiguration ( ) . update ( EXTENSION_CONFIG_KEY , 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 ( EXTENSION_CONFIG_KEY ) ! ; 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 getKnownKubeconfigs ( ) { const kkcConfig = vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ KNOWN_KUBECONFIGS_KEY ] ; if ( ! kkcConfig || ! kkcConfig . length ) { return [ ] ; } return kkcConfig as string [ ] ; } export async function addKnownKubeconfig ( kubeconfigPath ) { await addValueToConfigArray ( KNOWN_KUBECONFIGS_KEY , kubeconfigPath ) ; } export async function setActiveKubeconfig ( kubeconfig ) { await addPathToConfig ( KUBECONFIG_PATH_KEY , kubeconfig ) ; } export function getActiveKubeconfig ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ KUBECONFIG_PATH_KEY ] ; } export function getToolPath ( _host , shell , tool ) { const os = shell . platform ( ) ; const config = vscode . workspace . getConfiguration ( ) ; const baseBackCompatKey = toolPathBackCompatBaseKey ( tool ) ; const osBackCompatKey = osOverrideKey ( os , baseBackCompatKey ) ; const backCompatSettings = config . inspect < Dictionary < any > > ( EXTENSION_CONFIG_KEY ) || Dictionary . of < any > ( ) ; const wsFolderValues = backCompatSettings . workspaceFolderValue || { } ; const wsValues = backCompatSettings . workspaceValue || { } ; const userValues = backCompatSettings . globalValue || { } ; const defaultValues = backCompatSettings . defaultValue || { } ; const localBackCompatSetting = wsFolderValues [ osBackCompatKey ] || wsFolderValues [ baseBackCompatKey ] || wsValues [ osBackCompatKey ] || wsValues [ baseBackCompatKey ] ; if ( localBackCompatSetting ) { console . warn ( `Ignoring workspace-level setting ${ baseBackCompatKey }; paths are not allowed at workspace level` ) ; } const globalBackCompatSetting = userValues [ osBackCompatKey ] || userValues [ baseBackCompatKey ] || defaultValues [ osBackCompatKey ] || defaultValues [ baseBackCompatKey ] ; const baseKey = toolPathNewBaseKey ( tool ) ; const osKey = osOverrideKey ( os , baseKey ) ; const topLevelToolPath = userValues [ osKey ] || defaultValues [ osKey ] || userValues [ baseKey ] || defaultValues [ baseKey ] ; return topLevelToolPath || globalBackCompatSetting ; } export function toolPathOSKey ( os , tool ) { const baseKey = toolPathNewBaseKey ( tool ) ; const osSpecificKey = osOverrideKey ( os , baseKey ) ; return osSpecificKey ; } function toolPathBackCompatBaseKey ( tool ) { return `vs-kubernetes.${ tool }-path` ; } function toolPathNewBaseKey ( tool ) { return `vscode-kubernetes.${ tool }-path` ; } 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 ; } } export function getKubectlVersioning ( ) { const configValue = vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ KUBECTL_VERSIONING_KEY ] ; if ( configValue === "infer" ) { return KubectlVersioning . Infer ; } return KubectlVersioning . UserProvided ; } const AUTO_CLEANUP_DEBUG_KEY = "vs-kubernetes.autoCleanupOnDebugTerminate" ; export function getAutoCompleteOnDebugTerminate ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ AUTO_CLEANUP_DEBUG_KEY ] ; } export async function setAlwaysCleanUp ( ) { await setConfigValue ( AUTO_CLEANUP_DEBUG_KEY , true ) ; } const USE_WSL_KEY = "use-wsl" ; export function getUseWsl ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ USE_WSL_KEY ] ; } const MK_CHECK_UPGRADE_KEY = 'checkForMinikubeUpgrade' ; export function getCheckForMinikubeUpgrade ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ MK_CHECK_UPGRADE_KEY ] ; } export function getOutputFormat ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.outputFormat' ] ; } export function getConfiguredNamespace ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.namespace' ] ; } export function affectsUs ( change ) { return change . affectsConfiguration ( EXTENSION_CONFIG_KEY ) ; } export function getDisableLint ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'disable-lint' ] === 'true' ; } export function getDisabledLinters ( ) { const config = vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) ; return config [ 'disable-linters' ] as string [ ] || [ ] ; } export function getNodejsAutoDetectRemoteRoot ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.nodejs-autodetect-remote-root' ] ; } export function getNodejsRemoteRoot ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.nodejs-remote-root' ] ; } export function getNodejsDebugPort ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.nodejs-debug-port' ] ; } const IMAGE_BUILD_TOOL = "imageBuildTool" ; export function getImageBuildTool ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ IMAGE_BUILD_TOOL ] ; } export function getPythonAutoDetectRemoteRoot ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.python-autodetect-remote-root' ] ; } export function getPythonRemoteRoot ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.python-remote-root' ] ; } export function getPythonDebugPort ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.python-debug-port' ] ; } export function getDotnetVsdbgPath ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.dotnet-vsdbg-path' ] ; } export function getDotnetDebugSourceFileMap ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.dotnet-source-file-map' ] ; } export function getResourcesToBeWatched ( ) { const krwConfig = vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ RESOURCES_TO_WATCH_KEY ] ; if ( ! krwConfig || ! krwConfig . length ) { return [ ] ; } return krwConfig as string [ ] ; } export function getEnableSnapFlag ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.enable-snap-flag' ] ; } export function isContextStatusBarDisabled ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.disable-context-info-status-bar' ] ; } export function isNamespaceStatusBarDisabled ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.disable-namespace-info-status-bar' ] ; } export function getLocalTunnelDebugProvider ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.local-tunnel-debug-provider' ] ; } export function isMinimalDescribeWorkflow ( ) { return vscode . workspace . getConfiguration ( EXTENSION_CONFIG_KEY ) [ 'vs-kubernetes.enable-minimal-describe-workflow' ] ; }