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_oten_app, delete_oten_app, get_oten_app, get_oten_apps}; |
| 2 | use anyhow::{anyhow, Result}; |
| 3 | use console::style; |
| 4 | use dialoguer::{theme::ColorfulTheme, Confirm, Input}; |
| 5 | use smbpndk_model::{ |
| 6 | create_params::{self, AppCreate}, |
| 7 | CommandResult, |
| 8 | }; |
| 9 | use smbpndk_utils::{get_config, write_config}; |
| 10 | use spinners::Spinner; |
| 11 | |
| 12 | pub async fn process_oten_app(commands: Commands) -> Result<CommandResult> { |
| 13 | match commands { |
| 14 | Commands::New {} => { |
| 15 | let config = get_config().await?; |
| 16 | if config.current_project.is_none() { |
| 17 | return Err(anyhow!( |
| 18 | "No project selected. Please select a project first. Run `smb project use <id>`." |
| 19 | )); |
| 20 | } |
| 21 | |
| 22 | let app_name = Input::<String>::with_theme(&ColorfulTheme::default()) |
| 23 | .with_prompt("App name") |
| 24 | .interact() |
| 25 | .unwrap(); |
| 26 | |
| 27 | let spinner = Spinner::new( |
| 28 | spinners::Spinners::SimpleDotsScrolling, |
| 29 | style("Creating an Oten app...").green().bold().to_string(), |
| 30 | ); |
| 31 | |
| 32 | match create_oten_app(create_params::Oten { |
| 33 | oten_app: AppCreate { |
| 34 | name: app_name.clone(), |
| 35 | project_id: config.current_project.unwrap().id, |
| 36 | }, |
| 37 | }) |
| 38 | .await |
| 39 | { |
| 40 | Ok(_) => Ok(CommandResult { |
| 41 | spinner, |
| 42 | symbol: "✅".to_owned(), |
| 43 | msg: format!("An oten app created: {app_name}."), |
| 44 | }), |
| 45 | Err(e) => { |
| 46 | println!("Error: {e:#?}"); |
| 47 | Ok(CommandResult { |
| 48 | spinner, |
| 49 | symbol: "😩".to_owned(), |
| 50 | msg: format!("Failed to create an oten app: {app_name}."), |
| 51 | }) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | Commands::List {} => { |
| 56 | let spinner = Spinner::new( |
| 57 | spinners::Spinners::SimpleDotsScrolling, |
| 58 | style("Loading...").green().bold().to_string(), |
| 59 | ); |
| 60 | match get_oten_apps().await { |
| 61 | Ok(oten_apps) => { |
| 62 | println!("oten_apps: {oten_apps:#?}"); |
| 63 | println!( |
| 64 | "{0: <10} | {1: <30} | {2: <10} | {3: <10}", |
| 65 | "ID", "Name", "Created at", "Updated at" |
| 66 | ); |
| 67 | for project in oten_apps { |
| 68 | let id = project.id.split('-').collect::<Vec<&str>>()[0].to_owned(); |
| 69 | let created_at = project.created_at.date_naive(); |
| 70 | let updated_at = project.updated_at.date_naive(); |
| 71 | println!( |
| 72 | "{0: <10} | {1: <30} | {2: <10} | {3: <10}", |
| 73 | id, project.name, created_at, updated_at |
| 74 | ); |
| 75 | } |
| 76 | Ok(CommandResult { |
| 77 | spinner, |
| 78 | symbol: "✅".to_owned(), |
| 79 | msg: "Showing all oten apps.".to_owned(), |
| 80 | }) |
| 81 | } |
| 82 | Err(e) => { |
| 83 | println!("Error: {e:#?}"); |
| 84 | Ok(CommandResult { |
| 85 | spinner, |
| 86 | symbol: "😩".to_owned(), |
| 87 | msg: "Failed to get all oten apps.".to_owned(), |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | Commands::Show { id } => { |
| 93 | let spinner = Spinner::new( |
| 94 | spinners::Spinners::SimpleDotsScrolling, |
| 95 | style("Loading...").green().bold().to_string(), |
| 96 | ); |
| 97 | match get_oten_app(&id).await { |
| 98 | Ok(_) => Ok(CommandResult { |
| 99 | spinner, |
| 100 | symbol: "✅".to_owned(), |
| 101 | msg: "Showing oten app.".to_owned(), |
| 102 | }), |
| 103 | Err(e) => { |
| 104 | println!("Error: {e:#?}"); |
| 105 | Ok(CommandResult { |
| 106 | spinner, |
| 107 | symbol: "😩".to_owned(), |
| 108 | msg: "Failed to get an oten app.".to_owned(), |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | Commands::Delete { id } => { |
| 114 | let spinner = Spinner::new( |
| 115 | spinners::Spinners::SimpleDotsScrolling, |
| 116 | style("Loading...").green().bold().to_string(), |
| 117 | ); |
| 118 | match delete_oten_app(id).await { |
| 119 | Ok(_) => Ok(CommandResult { |
| 120 | spinner, |
| 121 | symbol: "✅".to_owned(), |
| 122 | msg: "Delete oten app succeed.".to_owned(), |
| 123 | }), |
| 124 | Err(e) => { |
| 125 | println!("Error: {e:#?}"); |
| 126 | Ok(CommandResult { |
| 127 | spinner, |
| 128 | symbol: "😩".to_owned(), |
| 129 | msg: "Failed to delete oten app.".to_owned(), |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | Commands::Use { id } => { |
| 135 | let mut spinner = Spinner::new( |
| 136 | spinners::Spinners::SimpleDotsScrolling, |
| 137 | style("Checking config file...").green().bold().to_string(), |
| 138 | ); |
| 139 | |
| 140 | let oten_app = get_oten_app(&id).await?; |
| 141 | let mut config = get_config().await?; |
| 142 | |
| 143 | if let Some(oten_app) = config.current_oten_app { |
| 144 | if oten_app.id != id { |
| 145 | spinner.stop_with_message("Found a config.".to_string()); |
| 146 | let yes = Confirm::new() |
| 147 | .with_prompt(format!( |
| 148 | "Will change active oten_app to {}. Do you want to continue?", |
| 149 | &id |
| 150 | )) |
| 151 | .interact()?; |
| 152 | if !yes { |
| 153 | let spinner = Spinner::new( |
| 154 | spinners::Spinners::SimpleDotsScrolling, |
| 155 | style("Cancelling operation...").green().bold().to_string(), |
| 156 | ); |
| 157 | return Ok(CommandResult { |
| 158 | spinner, |
| 159 | symbol: "✅".to_owned(), |
| 160 | msg: "Operation cancelled.".to_string(), |
| 161 | }); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | config.current_oten_app = Some(oten_app); |
| 167 | let spinner = Spinner::new( |
| 168 | spinners::Spinners::SimpleDotsScrolling, |
| 169 | style("Saving config...").green().bold().to_string(), |
| 170 | ); |
| 171 | match write_config(config) { |
| 172 | Ok(_) => Ok(CommandResult { |
| 173 | spinner, |
| 174 | symbol: "✅".to_owned(), |
| 175 | msg: format!("Using oten_app: {:?}", &id), |
| 176 | }), |
| 177 | Err(_) => { |
| 178 | let error = anyhow!("Failed while writing config."); |
| 179 | Err(error) |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |