import Debug from 'debug' ; import { Arguments , ExecOptions , Registrar , NavResponse , i18n } from '@kui-shell/core' ; import { doHelp , KubeOptions } from '@kui-shell/plugin-kubectl' ; import isUsage from './usage' ; import doExecWithStdout from './exec' ; const strings = i18n ( 'plugin-kubectl' ) ; const strings2 = i18n ( 'plugin-kubectl' , 'helm' ) ; const debug = Debug ( 'plugin-kubectl/helm/controller/helm/status' ) ; /**
 * Format the output of a helm status command
 *
 */ export const format = async ( name , args , response , execOptions ) => { const command = 'kubectl' ; const verb = 'get' ; debug ( 'nested?' , execOptions . nested ) ; debug ( 'command' , command ) ; debug ( 'verb' , verb ) ; const [ headerString ] = response . split ( /RESOURCES:|(?=NOTES:)/ ) ; const namespaceMatch = response . match ( /^NAMESPACE:\s+(.*)$/m ) || [ ] ; const namespaceFromHelmStatusOutput = namespaceMatch [ 1 ] ; debug ( 'namespace' , namespaceFromHelmStatusOutput ) ; const revisionMatch = response . match ( /^REVISION:\s+(.*)$/m ) || [ ] ; const revisionFromHelmStatusOutput = revisionMatch [ 1 ] ; debug ( 'revision' , revisionFromHelmStatusOutput ) ; const resources = [ ] ; debug ( 'resources' , resources ) ; if ( execOptions . nested ) { debug ( 'returning tables for nested call' ) ; return Promise . all ( resources . map ( async ( { kind , table } ) => { const T = await table ; T . title = kind ; return T ; } ) ) ; } else { const notesMatch = response . match ( /NOTES:\n([\s\S]+)?/ ) ; const statusMatch = headerString . match ( /LAST DEPLOYED: (.*)\nNAMESPACE: (.*)\nSTATUS: (.*)/ ) ; const status = ! statusMatch ? headerString : `### ${ strings2 ( 'Last Deployed' ) }
${ statusMatch [ 1 ] }

### ${ strings2 ( 'Namespace' ) }
${ statusMatch [ 2 ] }

### ${ strings2 ( 'Revision' ) }
${ revisionFromHelmStatusOutput }

### ${ strings ( 'status' ) }
\`${ statusMatch [ 3 ] }\`
` ; const summary = '' ; const notes = notesMatch && notesMatch [ 1 ] ; const overviewMenu = { label : 'Overview' , items : [ { mode : 'status' , label : strings ( 'status' ) , content : status , contentType : 'text/markdown' } ] . concat ( ! summary ? [ ] : [ { mode : 'summary' , label : strings ( 'summary' ) , content : summary , contentType : 'text/markdown' } ] ) . concat ( ! notes ? [ ] : [ { mode : 'notes' , label : strings2 ( 'Notes' ) , content : `\`\`\`${ notes }\`\`\`` , contentType : 'text/markdown' } ] ) } ; const resourcesMenu = { label : 'Resources' , items : await Promise . all ( resources . map ( async ( _ ) => ( { mode : _ . kind , content : await _ . table } ) ) ) } ; const commandResponse = { apiVersion : 'kui-shell/v1' , kind : 'NavResponse' , breadcrumbs : [ { label : 'helm' } , { label : 'release' , command : `helm ls` } , { label : name } ] , menus : [ overviewMenu , resourcesMenu ] } ; return commandResponse ; } } ; async function doStatus ( args ) { if ( isUsage ( args , 'status' ) ) { return doHelp ( 'helm' , args ) ; } const name = args . argvNoOptions [ args . argvNoOptions . indexOf ( 'status' ) + 1 ] ; const response = await doExecWithStdout ( args ) ; try { return format ( name , args , response , args . execOptions ) ; } catch ( err ) { console . error ( 'error formatting status' , err ) ; return response ; } } export default ( registrar ) => { registrar . listen ( '/helm/status' , doStatus ) ; } ;