feature/apps
claude/smbcloud-github-app-k9641p
development
feature/apps
feature/build-release
feature/ci-docs
feature/ci-ruby-gems
feature/cicd
feature/completion
feature/dist-nuget
feature/fix-ci-sdk-pypi-rubygem
feature/fix-clippy-on-stable-channel
feature/fix-deployment-script
feature/github-email
feature/gresiq-v1
feature/project-new-structure
feature/rsync-deploy
feature/ruby-runner
feature/sdk-npm
feature/sdk-workflows
feature/smbcloud-cli-release-npm
feature/smbcloud-deploy
feature/wasm
main
release/v0.7.4
| 1 | use crate::{cli::Commands, create_pkt_app, delete_pkt_app, get_pkt_app, get_pkt_apps}; |
| 2 | use anyhow::{anyhow, Result}; |
| 3 | use console::style; |
| 4 | use dialoguer::{theme::ColorfulTheme, Confirm, Input}; |
| 5 | use smbpndk_model::{AppCreate, CommandResult}; |
| 6 | use smbpndk_utils::{get_config, write_config}; |
| 7 | use spinners::Spinner; |
| 8 | |
| 9 | pub async fn process_pkt_app(commands: Commands) -> Result<CommandResult> { |
| 10 | match commands { |
| 11 | Commands::New {} => { |
| 12 | let app_name = Input::<String>::with_theme(&ColorfulTheme::default()) |
| 13 | .with_prompt("App name") |
| 14 | .interact() |
| 15 | .unwrap(); |
| 16 | let description = Input::<String>::with_theme(&ColorfulTheme::default()) |
| 17 | .with_prompt("Description") |
| 18 | .interact() |
| 19 | .unwrap(); |
| 20 | |
| 21 | let spinner = Spinner::new( |
| 22 | spinners::Spinners::SimpleDotsScrolling, |
| 23 | style("Creating a Pkt app...").green().bold().to_string(), |
| 24 | ); |
| 25 | |
| 26 | match create_pkt_app(AppCreate { |
| 27 | name: app_name.clone(), |
| 28 | description: description.clone(), |
| 29 | }) |
| 30 | .await |
| 31 | { |
| 32 | Ok(_) => Ok(CommandResult { |
| 33 | spinner, |
| 34 | symbol: "✅".to_owned(), |
| 35 | msg: format!("Pkt app created: {app_name}."), |
| 36 | }), |
| 37 | Err(e) => { |
| 38 | println!("Error: {e:#?}"); |
| 39 | Ok(CommandResult { |
| 40 | spinner, |
| 41 | symbol: "😩".to_owned(), |
| 42 | msg: format!("Failed to create a Pkt app: {app_name}."), |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | Commands::List {} => { |
| 48 | let spinner = Spinner::new( |
| 49 | spinners::Spinners::SimpleDotsScrolling, |
| 50 | style("Loading...").green().bold().to_string(), |
| 51 | ); |
| 52 | match get_pkt_apps().await { |
| 53 | Ok(apps) => { |
| 54 | println!("apps: {apps:#?}"); |
| 55 | println!( |
| 56 | "{0: <10} | {1: <30} | {2: <10} | {3: <10}", |
| 57 | "ID", "Name", "Created at", "Updated at" |
| 58 | ); |
| 59 | for project in apps { |
| 60 | let id = project.id.split('-').collect::<Vec<&str>>()[0].to_owned(); |
| 61 | let created_at = project.created_at.date_naive(); |
| 62 | let updated_at = project.updated_at.date_naive(); |
| 63 | println!( |
| 64 | "{0: <10} | {1: <30} | {2: <10} | {3: <10}", |
| 65 | id, project.name, created_at, updated_at |
| 66 | ); |
| 67 | } |
| 68 | Ok(CommandResult { |
| 69 | spinner, |
| 70 | symbol: "✅".to_owned(), |
| 71 | msg: "Showing all Pkt apps.".to_owned(), |
| 72 | }) |
| 73 | } |
| 74 | Err(e) => { |
| 75 | println!("Error: {e:#?}"); |
| 76 | Ok(CommandResult { |
| 77 | spinner, |
| 78 | symbol: "😩".to_owned(), |
| 79 | msg: "Failed to get all Pkt apps.".to_owned(), |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | Commands::Show { id } => { |
| 85 | let spinner = Spinner::new( |
| 86 | spinners::Spinners::SimpleDotsScrolling, |
| 87 | style("Loading...").green().bold().to_string(), |
| 88 | ); |
| 89 | match get_pkt_app(&id).await { |
| 90 | Ok(_) => Ok(CommandResult { |
| 91 | spinner, |
| 92 | symbol: "✅".to_owned(), |
| 93 | msg: "Showing Pkt app.".to_owned(), |
| 94 | }), |
| 95 | Err(e) => { |
| 96 | println!("Error: {e:#?}"); |
| 97 | Ok(CommandResult { |
| 98 | spinner, |
| 99 | symbol: "😩".to_owned(), |
| 100 | msg: "Failed to get an Pkt app.".to_owned(), |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | Commands::Delete { id } => { |
| 106 | let spinner = Spinner::new( |
| 107 | spinners::Spinners::SimpleDotsScrolling, |
| 108 | style("Loading...").green().bold().to_string(), |
| 109 | ); |
| 110 | match delete_pkt_app(id).await { |
| 111 | Ok(_) => Ok(CommandResult { |
| 112 | spinner, |
| 113 | symbol: "✅".to_owned(), |
| 114 | msg: "Delete Pkt app succeed.".to_owned(), |
| 115 | }), |
| 116 | Err(e) => { |
| 117 | println!("Error: {e:#?}"); |
| 118 | Ok(CommandResult { |
| 119 | spinner, |
| 120 | symbol: "😩".to_owned(), |
| 121 | msg: "Failed to delete Pkt app.".to_owned(), |
| 122 | }) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | Commands::Use { id } => { |
| 127 | let mut spinner = Spinner::new( |
| 128 | spinners::Spinners::SimpleDotsScrolling, |
| 129 | style("Checking config file...").green().bold().to_string(), |
| 130 | ); |
| 131 | |
| 132 | let pkt_app = get_pkt_app(&id).await?; |
| 133 | let mut config = get_config().await?; |
| 134 | |
| 135 | if let Some(pkt) = config.current_pkt_app { |
| 136 | if pkt.id != id { |
| 137 | spinner.stop_with_message("Found a config.".to_string()); |
| 138 | let yes = Confirm::new() |
| 139 | .with_prompt(format!( |
| 140 | "Will change active Pkt app to {}. Do you want to continue?", |
| 141 | &id |
| 142 | )) |
| 143 | .interact()?; |
| 144 | if !yes { |
| 145 | let spinner = Spinner::new( |
| 146 | spinners::Spinners::SimpleDotsScrolling, |
| 147 | style("Cancelling operation...").green().bold().to_string(), |
| 148 | ); |
| 149 | return Ok(CommandResult { |
| 150 | spinner, |
| 151 | symbol: "✅".to_owned(), |
| 152 | msg: "Operation cancelled.".to_string(), |
| 153 | }); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | config.current_pkt_app = Some(pkt_app); |
| 159 | let spinner = Spinner::new( |
| 160 | spinners::Spinners::SimpleDotsScrolling, |
| 161 | style("Saving config...").green().bold().to_string(), |
| 162 | ); |
| 163 | match write_config(config) { |
| 164 | Ok(_) => Ok(CommandResult { |
| 165 | spinner, |
| 166 | symbol: "✅".to_owned(), |
| 167 | msg: format!("Using Pkt app: {:?}", &id), |
| 168 | }), |
| 169 | Err(_) => { |
| 170 | let error = anyhow!("Failed while writing config."); |
| 171 | Err(error) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |