import { Arguments , REPL } from '@kui-shell/core' ; import { KubeOptions , KubeItems , KubeResource , getNamespace , isKubeResource , doExecWithStdout , doExecWithRadioTable } from '@kui-shell/plugin-kubectl' ; import Project from '../../../model/Project' ; /** onSelect handler for the RadioTable view model */ async function onSelectProject ( this , name ) { await this . pexec ( `odo project set ${ this . encodeComponent ( name ) }` ) ; } /**
 * Transform the KubeItems<Project> resource to a RadioTable view model.
 *
 */ export function projectListView ( args , response ) { if ( args . parsedOptions . o !== 'json' && isKubeResource ( response ) ) { const projects = JSON . parse ( response . kuiRawData ) . items as Project [ ] ; const defaultSelectedIdx = projects . findIndex ( _ => _ . status . active ) ; const opts = { title : 'Projects' , nameColumnTitle : 'PROJECT' } ; return doExecWithRadioTable ( projects , defaultSelectedIdx , onSelectProject . bind ( args . REPL ) , opts ) || response ; } else { return response ; } } /**
 * Always fetch the JSON data, even if the user did not request it.
 *
 * We will then transform the JSON KubeItems<Project> model into a
 * `RadioTable` view model via the `projectListView` viewTransformer.
 *
 */ function getRawData ( args ) { if ( args . parsedOptions . o !== 'json' ) { args . command += ' -o json' ; args . argv . push ( '-o' ) ; args . argv . push ( '-json' ) ; args . argvNoOptions . push ( '-o' ) ; args . argvNoOptions . push ( '-json' ) ; } return doExecWithStdout ( args , undefined , 'odo' ) ; } /** Fetch the KubeItems<Project> model */ export async function projectList ( args ) { const kuiRawData = await getRawData ( args ) ; const list = JSON . parse ( kuiRawData ) as KubeItems < Project > ; return Object . assign ( list , { metadata : { name : args . command , namespace : await getNamespace ( args ) } , modes : [ ] , kuiRawData , isSimulacrum : true , isKubeResource : true , originatingCommand : args } ) ; }