@smbcloud / cli / commits / 00e06a0

move to crud read

Seto Elkahfi committed May 28, 2025 at 19:27 UTC 00e06a018bfe261a92fbed173a6413c85524ae33
2 files changed +81 -37
crates/cli/src/project/crud_read.rs new
+77
@@ -0,0 +1,77 @@
1 +use crate::{
2 + cli::CommandResult,
3 + ui::{fail_message, fail_symbol, succeed_message, succeed_symbol},
4 +};
5 +use anyhow::Result;
6 +use smbcloud_model::project::Project;
7 +use smbcloud_networking::environment::Environment;
8 +use smbcloud_networking_project::get_all;
9 +use spinners::Spinner;
10 +use tabled::{Table, Tabled};
11 +
12 +#[derive(Tabled)]
13 +struct ProjectRow {
14 + #[tabled(rename = "ID")]
15 + id: i32,
16 + #[tabled(rename = "Name")]
17 + name: String,
18 + #[tabled(rename = "Description")]
19 + description: String,
20 + #[tabled(rename = "Created at")]
21 + created_at: String,
22 + #[tabled(rename = "Updated at")]
23 + updated_at: String,
24 +}
25 +
26 +pub async fn process_project_list(env: Environment) -> Result<CommandResult> {
27 + let mut spinner = Spinner::new(
28 + spinners::Spinners::SimpleDotsScrolling,
29 + succeed_message("Loading"),
30 + );
31 +
32 + match get_all(env).await {
33 + Ok(projects) => {
34 + spinner.stop_and_persist(&succeed_symbol(), succeed_message("Loaded."));
35 + let msg = if projects.is_empty() {
36 + succeed_message("No projects found.")
37 + } else {
38 + succeed_message("Showing all projects.")
39 + };
40 + show_projects(projects);
41 + Ok(CommandResult {
42 + spinner: Spinner::new(
43 + spinners::Spinners::SimpleDotsScrolling,
44 + succeed_message("Loading"),
45 + ),
46 + symbol: succeed_symbol(),
47 + msg,
48 + })
49 + }
50 + Err(e) => {
51 + println!("Error: {e:#?}");
52 + Ok(CommandResult {
53 + spinner,
54 + symbol: fail_symbol(),
55 + msg: fail_message("Failed to get all projects."),
56 + })
57 + }
58 + }
59 +}
60 +
61 +pub(crate) fn show_projects(projects: Vec<Project>) {
62 + if projects.is_empty() {
63 + return;
64 + }
65 + let rows: Vec<ProjectRow> = projects
66 + .into_iter()
67 + .map(|p| ProjectRow {
68 + id: p.id,
69 + name: p.name,
70 + description: p.description,
71 + created_at: p.created_at.date_naive().to_string(),
72 + updated_at: p.updated_at.date_naive().to_string(),
73 + })
74 + .collect();
75 + let table = Table::new(rows);
76 + println!("{table}");
77 +}
crates/cli/src/project/mod.rs
+4 -37
@@ -1,18 +1,19 @@
1 pub mod cli;
2 +pub mod crud_read;
3 pub mod init;
4
5 use self::cli::Commands;
6 use crate::{
7 cli::CommandResult,
8 + project::{crud_read::process_project_list, init::process_project_init},
9 ui::{fail_message, fail_symbol, succeed_message, succeed_symbol},
10 };
11 use anyhow::{anyhow, Result};
12 use dialoguer::{theme::ColorfulTheme, Input};
11 -use init::process_project_init;
13 use log::debug;
14 use smbcloud_model::project::{Config, Project};
15 use smbcloud_networking::environment::Environment;
15 -use smbcloud_networking_project::{delete_project, get_all, get_project};
16 +use smbcloud_networking_project::{delete_project, get_project};
17 use spinners::Spinner;
18 use std::{fs::OpenOptions, io::Write};
19 use tabled::{Table, Tabled};
@@ -34,41 +35,7 @@ struct ProjectRow {
35 pub async fn process_project(env: Environment, commands: Commands) -> Result<CommandResult> {
36 match commands {
37 Commands::New {} => process_project_init(env).await,
37 - Commands::List {} => {
38 - let mut spinner = Spinner::new(
39 - spinners::Spinners::SimpleDotsScrolling,
40 - succeed_message("Loading"),
41 - );
42 -
43 - // Get all
44 - match get_all(env).await {
45 - Ok(projects) => {
46 - spinner.stop_and_persist(&succeed_symbol(), succeed_message("Loaded."));
47 - let msg = if projects.is_empty() {
48 - succeed_message("No projects found.")
49 - } else {
50 - succeed_message("Showing all projects.")
51 - };
52 - show_projects(projects);
53 - Ok(CommandResult {
54 - spinner: Spinner::new(
55 - spinners::Spinners::SimpleDotsScrolling,
56 - succeed_message("Loading"),
57 - ),
58 - symbol: succeed_symbol(),
59 - msg,
60 - })
61 - }
62 - Err(e) => {
63 - println!("Error: {e:#?}");
64 - Ok(CommandResult {
65 - spinner,
66 - symbol: fail_symbol(),
67 - msg: fail_message("Failed to get all projects."),
68 - })
69 - }
70 - }
71 - }
38 + Commands::List {} => process_project_list(env).await,
39 Commands::Show { id } => {
40 let mut spinner = Spinner::new(
41 spinners::Spinners::SimpleDotsScrolling,