Files
orca/src/cli/handlers/environment.ts
T
2026-05-14 23:13:37 -07:00

76 lines
2.6 KiB
TypeScript

import type { CommandHandler } from '../dispatch'
import { formatEnvironment, formatEnvironmentList, printResult } from '../format'
import { getDefaultUserDataPath } from '../runtime-client'
import type { RuntimeRpcSuccess } from '../runtime-client'
import { RuntimeClientError } from '../runtime-client'
import { redactRuntimeEnvironment } from '../../shared/runtime-environments'
import {
addEnvironmentFromPairingCode,
listEnvironments,
removeEnvironment,
resolveEnvironment,
type EnvironmentAddResult,
type EnvironmentRemoveResult
} from '../runtime/environments'
export const ENVIRONMENT_HANDLERS: Record<string, CommandHandler> = {
'environment add': async ({ flags, json }) => {
const name = getRequiredStringFlag(flags, 'name')
const pairingCode = getRequiredStringFlag(flags, 'pairing-code')
const environment = redactRuntimeEnvironment(
addEnvironmentFromPairingCode(getDefaultUserDataPath(), {
name,
pairingCode
})
)
printResult(
localSuccess({ environment }),
json,
(result: EnvironmentAddResult) =>
`Saved environment ${result.environment.name} (${result.environment.id}).`
)
},
'environment list': async ({ json }) => {
const environments = listEnvironments(getDefaultUserDataPath()).map(redactRuntimeEnvironment)
printResult(localSuccess({ environments }), json, formatEnvironmentList)
},
'environment show': async ({ flags, json }) => {
const selector = getRequiredStringFlag(flags, 'environment')
const environment = redactRuntimeEnvironment(
resolveEnvironment(getDefaultUserDataPath(), selector)
)
printResult(localSuccess({ environment }), json, ({ environment: value }) =>
formatEnvironment(value)
)
},
'environment rm': async ({ flags, json }) => {
const selector = getRequiredStringFlag(flags, 'environment')
const removed = redactRuntimeEnvironment(removeEnvironment(getDefaultUserDataPath(), selector))
printResult(
localSuccess({ removed }),
json,
(result: EnvironmentRemoveResult) =>
`Removed environment ${result.removed.name} (${result.removed.id}).`
)
}
}
function getRequiredStringFlag(flags: Map<string, string | boolean>, name: string): string {
const value = flags.get(name)
if (typeof value !== 'string' || value.length === 0) {
throw new RuntimeClientError('invalid_argument', `Missing required --${name}`)
}
return value
}
function localSuccess<TResult>(result: TResult): RuntimeRpcSuccess<TResult> {
return {
id: 'local',
ok: true,
result,
_meta: {
runtimeId: 'local'
}
}
}