🌳
pt0/deployF/k8sF/isActionSupportedF.mts
1import * as _ from 'lodash-es'
8import type { CliSchema } from '../../serverF/cliArgAI.mts'
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},
15})
17/** All actions including availActionsCtx - used for help display */
18export const resourcesActionsH = () => ({
19 ...coreResourcesActionsH(),
20 ...availActionsCtx.getStore(),
21})
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>()
30 knownAliases.add('h')
31 for (const spec of Object.values(schema || {})) {
32 if (spec && typeof spec === 'object' && 'alias' in spec && spec.alias) knownAliases.add(spec.alias)
33 }
34 const allowPositional = actionCfg?.cliAcceptsPositional
35 return getProcArgv().slice(3).filter(a => {
36 if (a.startsWith('--')) return !knownFlags.has(a.replace(/^--/, '').split('=')[0])
37 if (a.startsWith('-')) return !knownAliases.has(a.slice(1))
38 return !allowPositional
39 })
42export const exitOnUnknownArgs = (actionCfg: ActionCfg | undefined) => {
43 if (cliValidatedCtx.getStore()?.validated) return
44 const unknownArgs = getUnknownArgs(actionCfg)
45 if (!unknownArgs.length) return
46 console.error(`${unknownArgsPrefix} ${unknownArgs.join(', ')}`)
47 const schema = actionCfg?.cliSchema as CliSchema | undefined
48 if (schema) {
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}`)
51 }
52 process.exit(1)
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
61 if (cliFlag('--help')) 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
66 return true
69/** All action names for help display */
70export const resourcesActionNames = () => _.keys(resourcesActionsH())