1import * as _ from 'lodash-es' 11/** Actions that resourcesActionCore/res1Action can dispatch - used for isActionSupported */ 12const coreResourcesActionsH = () => ({ 13 apply: {cliSchema: applyCli}, testdeploy: {cliSchema: testdeployCli}, delete: {cliSchema: scopeCli}, info: {cliSchema: scopeCli}, 17/** All actions including availActionsCtx - used for help display */ 18export const resourcesActionsH = () => ({ 19 ...coreResourcesActionsH(), 20 ...availActionsCtx.getStore(), 23type ActionCfg = { cliSchema?: Record<string, unknown>; cliAcceptsPositional?: boolean } 25export const getUnknownArgs = (actionCfg: ActionCfg | undefined) => { 26 const schema = actionCfg?.cliSchema as CliSchema | undefined 27 const knownFlags = new Set(Object.keys(schema || {})) 28 knownFlags.add('help') 29 const knownAliases = new Set<string>() 31 for (const spec of Object.values(schema || {})) { 32 if (spec && typeof spec === 'object' && 'alias' in spec && spec.alias) knownAliases.add(spec.alias) 34 const allowPositional = actionCfg?.cliAcceptsPositional 36 if (a.startsWith('--')) return !knownFlags.has(a.replace(/^--/, '').split('=')[0]) 37 if (a.startsWith('-')) return !knownAliases.has(a.slice(1)) 38 return !allowPositional 42export const exitOnUnknownArgs = (actionCfg: ActionCfg | undefined) => { 43 if (cliValidatedCtx.getStore()?.validated) return 44 const unknownArgs = getUnknownArgs(actionCfg) 45 if (!unknownArgs.length) return 47 const schema = actionCfg?.cliSchema as CliSchema | undefined 49 const validFlags = Object.keys(schema).filter(k => k !== 'help' && !((schema as Record<string, unknown>)[k] as Record<string, unknown>)?.hidden).map(k => `--${k}`).join(', ') 50 if (validFlags) console.error(`Valid flags: ${validFlags}`) 55/** Check if action is a core k8s resource action (apply/delete/info/pods/logs/etc) - these need cluster_name */ 56export const isResourceAction = (action: string) => _.includes(_.keys(coreResourcesActionsH()), action) 58/** Check if action can be handled by resourcesActionCore (not custom actions in availActionsCtx) */ 59export const isActionSupported = (action: string) => { 60 if (!isResourceAction(action)) return false 62 // Skip flag validation if already validated (inner dispatch from runtests/etc) 63 if (cliValidatedCtx.getStore()?.validated) return true 64 const actionsH = resourcesActionsH() as Record<string, ActionCfg | undefined> 65 if (getUnknownArgs(actionsH[action]).length > 0) return false 69/** All action names for help display */ 70export const resourcesActionNames = () => _.keys(resourcesActionsH())