| 1 | //! Host registration/action conversion for the portable policy inventory. |
| 2 | use super::CommandResult; |
| 3 | use super::groups::config::policy::portable_handlers; |
| 4 | use crate::tui::app::App; |
| 5 | use codewhale_command_contract::handler::CommandHandler; |
| 6 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 7 | use codewhale_command_contract::outcome::ConfigPolicyCommandResult; |
| 8 | |
| 9 | struct Registration<const INDEX: usize>; |
| 10 | impl<const INDEX: usize> RegisterCommand<CommandResult> for Registration<INDEX> { |
| 11 | fn info() -> &'static CommandInfo { |
| 12 | portable_handlers()[INDEX].0 |
| 13 | } |
| 14 | fn handler() -> CommandHandler<CommandResult> { |
| 15 | match portable_handlers()[INDEX].1 { |
| 16 | CommandHandler::Contextual { capabilities, .. } => CommandHandler::Contextual { |
| 17 | capabilities, |
| 18 | handler: |contexts, args| match portable_handlers()[INDEX].1 { |
| 19 | CommandHandler::Contextual { handler, .. } => { |
| 20 | host_result(handler(contexts, args)) |
| 21 | } |
| 22 | _ => CommandResult::error("policy command handler shape changed"), |
| 23 | }, |
| 24 | }, |
| 25 | _ => CommandHandler::Pure(|_| { |
| 26 | CommandResult::error("policy command requires a contextual handler") |
| 27 | }), |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | pub(super) fn permissions(app: &mut App, args: Option<&str>) -> CommandResult { |
| 32 | execute::<0>(app, args) |
| 33 | } |
| 34 | #[cfg(test)] |
| 35 | pub(super) fn status(app: &mut App, args: Option<&str>) -> CommandResult { |
| 36 | execute::<1>(app, args) |
| 37 | } |
| 38 | fn execute<const INDEX: usize>(app: &mut App, args: Option<&str>) -> CommandResult { |
| 39 | match Registration::<INDEX>::handler() { |
| 40 | CommandHandler::Contextual { |
| 41 | capabilities, |
| 42 | handler, |
| 43 | } => handler( |
| 44 | app.command_contexts_with_config(None) |
| 45 | .contexts(capabilities), |
| 46 | args, |
| 47 | ), |
| 48 | CommandHandler::Pure(handler) => handler(args), |
| 49 | } |
| 50 | } |
| 51 | fn host_result(result: ConfigPolicyCommandResult) -> CommandResult { |
| 52 | CommandResult { |
| 53 | message: result.message, |
| 54 | is_error: result.is_error, |
| 55 | action: result |
| 56 | .action |
| 57 | .map(super::config_host::config_action_to_app_action), |
| 58 | } |
| 59 | } |
| 60 |