reactive_graph_client/client/system/plugin/
mod.rs

1use std::sync::Arc;
2
3use crate::client::error::CommandError;
4use crate::client::error::CommandError::NoChange;
5use crate::client::error::CommandError::NotFound;
6use crate::client::result::CommandResult;
7use crate::client::system::plugin::args::PluginsArgs;
8use crate::client::system::plugin::commands::PluginsCommands;
9use reactive_graph_client::ReactiveGraphClient;
10use reactive_graph_table_model::system::plugin::PluginsTableContainer;
11
12pub(crate) mod args;
13pub(crate) mod commands;
14
15pub(crate) async fn plugins(client: &Arc<ReactiveGraphClient>, plugins_args: PluginsArgs) -> CommandResult {
16    let Some(command) = plugins_args.commands else {
17        return Err(CommandError::MissingSubCommand);
18    };
19    match command {
20        PluginsCommands::List => match client.runtime().plugins().get_all().await {
21            Ok(plugins) => Ok(PluginsTableContainer::from(plugins).into()),
22            Err(e) => Err(e.into()),
23        },
24        PluginsCommands::Search(args) => match client.runtime().plugins().search(args.into()).await {
25            Ok(plugins) => Ok(PluginsTableContainer::from(plugins).into()),
26            Err(e) => Err(e.into()),
27        },
28        PluginsCommands::Get(args) => match client.runtime().plugins().get_by_name(args.name.clone()).await {
29            Ok(Some(plugin)) => Ok(PluginsTableContainer::from(plugin).into()),
30            Ok(None) => Err(NotFound(format!("No plugin exists with name {}", args.name).to_string())),
31            Err(e) => Err(e.into()),
32        },
33        PluginsCommands::Dependencies(args) => match client.runtime().plugins().get_dependencies(args.name.clone()).await {
34            Ok(Some(plugins)) => Ok(PluginsTableContainer::from(plugins).into()),
35            Ok(None) => Err(NotFound(format!("No plugin exists with name {}", args.name).to_string())),
36            Err(e) => Err(e.into()),
37        },
38        PluginsCommands::Dependents(args) => match client.runtime().plugins().get_dependents(args.name.clone()).await {
39            Ok(Some(plugins)) => Ok(PluginsTableContainer::from(plugins).into()),
40            Ok(None) => Err(NotFound(format!("No plugin exists with name {}", args.name).to_string())),
41            Err(e) => Err(e.into()),
42        },
43        PluginsCommands::Start(args) => match client.runtime().plugins().start(args.name).await {
44            Ok(plugin) => Ok(PluginsTableContainer::from(plugin).into()),
45            Err(e) => Err(e.into()),
46        },
47        PluginsCommands::Stop(args) => match client.runtime().plugins().stop(args.name).await {
48            Ok(plugin) => Ok(PluginsTableContainer::from(plugin).into()),
49            Err(e) => Err(e.into()),
50        },
51        PluginsCommands::Restart(args) => match client.runtime().plugins().restart(args.name).await {
52            Ok(plugin) => Ok(PluginsTableContainer::from(plugin).into()),
53            Err(e) => Err(e.into()),
54        },
55        PluginsCommands::Uninstall(args) => match client.runtime().plugins().uninstall(args.name).await {
56            Ok(true) => Ok("Uninstalled plugin".into()),
57            Ok(false) => Err(NoChange("Plugin wasn't uninstalled.".to_string())),
58            Err(e) => Err(e.into()),
59        },
60    }
61}
62
63// fn print_plugin(plugin: Plugin) {
64//     print_plugins(vec![plugin]);
65// }
66//
67// fn print_plugins(plugins: Vec<Plugin>) {
68//     let plugins: Vec<reactive-graph-table-model::system::plugin::Plugin> = plugins.into_iter().map(|p| p.into()).collect();
69//     let table = Table::new(plugins)
70//         .with(Style::extended())
71//         .with(
72//             Modify::new(Rows::new(1..))
73//                 .with(Width::increase(10).priority())
74//                 .with(Width::truncate(40).suffix("...")),
75//         )
76//         .to_string();
77//     println!("{}", table);
78// }