import * as vscode from 'vscode' ; import * as v1 from './v1' ; import * as _ from 'lodash' ; import * as fs from './wsl-fs' ; import * as filepath from 'path' ; import { shell } from './shell' ; export class Resources { public all ( ) { const home = shell . home ( ) ; const schemaDir = filepath . join ( home , ".kube/schema" ) ; if ( ! fs . existsSync ( schemaDir ) ) { return this . v1 ( ) ; } const stat = fs . statSync ( schemaDir ) ; if ( ! stat . isDirectory ( ) ) { return this . v1 ( ) ; } const schemaDirContents = shell . ls ( schemaDir ) ; if ( schemaDirContents . length === 0 ) { return this . v1 ( ) ; } const kversion = _ . last ( schemaDirContents ) ! ; console . log ( "Loading schema for version " + kversion ) ; let res = Array . of < vscode . CompletionItem > ( ) ; const path = filepath . join ( schemaDir , kversion ) ; shell . ls ( path ) . forEach ( ( item ) => { const itemPath = filepath . join ( path , item ) ; const stat = fs . statSync ( itemPath ) ; if ( stat . isDirectory ( ) ) { return ; } const schema = JSON . parse ( shell . cat ( itemPath ) ) ; if ( ! schema . models ) { return ; } console . log ( "Adding schema " + itemPath ) ; res = res . concat ( this . fromSchema ( schema . models ) ) ; } ) ; console . log ( `Attached ${ res . length } resource kinds` ) ; return res ; } v1 ( ) { return this . fromSchema ( v1 . default . models ) ; } fromSchema ( schema ) { const res = Array . of < vscode . CompletionItem > ( ) ; _ . each ( schema , ( v , k ) => { const i = k . lastIndexOf ( "." ) ; const kind = k . substr ( i + 1 ) ; res . push ( val ( kind , `kind: ${ kind }` , v . description ) ) ; _ . each ( v . properties , ( spec , label ) => { let type = "undefined" ; switch ( spec . type ) { case undefined : if ( spec [ "$ref" ] ) { type = spec [ "$ref" ] ; } break ; case "array" : if ( spec . items . type ) { type = spec . items . type + "[]" ; break ; } else if ( spec . items [ "$ref" ] ) { type = spec . items [ "$ref" ] + "[]" ; break ; } type = "[]" ; break ; default : if ( spec . type ) { type = spec . type ; } break ; } res . push ( d ( label , `${ label }: ${ type }` , spec . description ) ) ; } ) ; } ) ; return res ; } } function d ( name , use , doc ) { const i = new vscode . CompletionItem ( name , vscode . CompletionItemKind . Variable ) ; i . detail = use ; i . documentation = doc ; return i ; } function val ( name , use , doc ) { const i = new vscode . CompletionItem ( name , vscode . CompletionItemKind . Value ) ; i . detail = use ; i . documentation = doc ; return i ; }