|
| 1 | +use { |
| 2 | + crate::token::get_smb_token, |
| 3 | + crate::{deploy::config::Config, ui::highlight}, |
| 4 | + dialoguer::{theme::ColorfulTheme, Confirm, Input, Select}, |
| 5 | + regex::Regex, |
| 6 | + smbcloud_model::{ |
| 7 | + error_codes::{ErrorCode, ErrorResponse}, |
| 8 | + project::{Project, ProjectCreate}, |
| 9 | + }, |
| 10 | + smbcloud_network::environment::Environment, |
| 11 | + smbcloud_networking_project::{ |
| 12 | + crud_project_create::create_project, crud_project_read::get_projects, |
| 13 | + }, |
| 14 | + std::{env, fs, path::Path}, |
| 15 | +}; |
| 16 | + |
| 17 | +pub async fn setup_project( |
| 18 | + env: Environment, |
| 19 | + access_token: Option<&str>, |
| 20 | +) -> Result<Config, ErrorResponse> { |
| 21 | + let path = env::current_dir().ok(); |
| 22 | + let path_str = path |
| 23 | + .as_ref() |
| 24 | + .map(|p| p.display().to_string()) |
| 25 | + .unwrap_or_else(|| ".".to_string()); |
| 26 | + |
| 27 | + let confirm = Confirm::with_theme(&ColorfulTheme::default()) |
| 28 | + .with_prompt(format!("Setup project in {}? y/n", highlight(&path_str))) |
| 29 | + .interact() |
| 30 | + .map_err(|_| ErrorResponse::Error { |
| 31 | + error_code: ErrorCode::InputError, |
| 32 | + message: ErrorCode::InputError.message(None).to_string(), |
| 33 | + })?; |
| 34 | + |
| 35 | + if !confirm { |
| 36 | + return Err(ErrorResponse::Error { |
| 37 | + error_code: ErrorCode::Cancel, |
| 38 | + message: ErrorCode::Cancel.message(None).to_string(), |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + let access_token = match access_token { |
| 43 | + Some(token) => token.to_string(), |
| 44 | + None => { |
| 45 | + let access_token = match get_smb_token(env).await { |
| 46 | + Ok(token) => token, |
| 47 | + Err(_) => { |
| 48 | + return Err(ErrorResponse::Error { |
| 49 | + error_code: ErrorCode::Unauthorized, |
| 50 | + message: ErrorCode::Unauthorized.message(None).to_string(), |
| 51 | + }) |
| 52 | + } |
| 53 | + }; |
| 54 | + access_token |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + let projects = match get_projects(env, access_token).await { |
| 59 | + Ok(x) => x, |
| 60 | + Err(_) => { |
| 61 | + return Err(ErrorResponse::Error { |
| 62 | + error_code: ErrorCode::InputError, |
| 63 | + message: ErrorCode::InputError.message(None).to_string(), |
| 64 | + }) |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + let project: Project = if !projects.is_empty() { |
| 69 | + select_project(env, projects, &path_str).await? |
| 70 | + } else { |
| 71 | + create_new_project(env, &path_str).await? |
| 72 | + }; |
| 73 | + |
| 74 | + let name = project.name.clone(); |
| 75 | + let description = project.description.clone(); |
| 76 | + |
| 77 | + // Create config struct |
| 78 | + let config = Config { |
| 79 | + project, |
| 80 | + name, |
| 81 | + description, |
| 82 | + }; |
| 83 | + |
| 84 | + // Ensure .smb directory exists |
| 85 | + let smb_dir = Path::new(".smb"); |
| 86 | + if !smb_dir.exists() { |
| 87 | + fs::create_dir(smb_dir).map_err(|_| ErrorResponse::Error { |
| 88 | + error_code: ErrorCode::MissingConfig, |
| 89 | + message: ErrorCode::MissingConfig.message(None).to_string(), |
| 90 | + })?; |
| 91 | + } |
| 92 | + |
| 93 | + // Write config to .smb/config.toml |
| 94 | + let config_toml = toml::to_string(&config).map_err(|_| ErrorResponse::Error { |
| 95 | + error_code: ErrorCode::MissingConfig, |
| 96 | + message: ErrorCode::MissingConfig.message(None).to_string(), |
| 97 | + })?; |
| 98 | + fs::write(".smb/config.toml", config_toml).map_err(|_| ErrorResponse::Error { |
| 99 | + error_code: ErrorCode::MissingConfig, |
| 100 | + message: ErrorCode::MissingConfig.message(None).to_string(), |
| 101 | + })?; |
| 102 | + |
| 103 | + Ok(config) |
| 104 | +} |
| 105 | + |
| 106 | +async fn select_project( |
| 107 | + env: Environment, |
| 108 | + projects: Vec<Project>, |
| 109 | + path: &str, |
| 110 | +) -> Result<Project, ErrorResponse> { |
| 111 | + let confirm = Confirm::with_theme(&ColorfulTheme::default()) |
| 112 | + .with_prompt("Use existing project? y/n") |
| 113 | + .interact() |
| 114 | + .map_err(|_| ErrorResponse::Error { |
| 115 | + error_code: ErrorCode::InputError, |
| 116 | + message: ErrorCode::InputError.message(None).to_string(), |
| 117 | + })?; |
| 118 | + |
| 119 | + if !confirm { |
| 120 | + return create_new_project(env, path).await; |
| 121 | + } |
| 122 | + let selection = match Select::with_theme(&ColorfulTheme::default()) |
| 123 | + .with_prompt("Use space/enter to select. Press q or esc to create a new project.") |
| 124 | + .items(&projects) |
| 125 | + .default(0) |
| 126 | + .interact_opt() |
| 127 | + .map_err(|_| ErrorResponse::Error { |
| 128 | + error_code: ErrorCode::InputError, |
| 129 | + message: ErrorCode::InputError.message(None).to_string(), |
| 130 | + }) { |
| 131 | + Ok(selection) => match selection { |
| 132 | + Some(selection) => selection, |
| 133 | + None => { |
| 134 | + return create_new_project(env, path).await; |
| 135 | + } |
| 136 | + }, |
| 137 | + _ => { |
| 138 | + return Err(ErrorResponse::Error { |
| 139 | + error_code: ErrorCode::InputError, |
| 140 | + message: ErrorCode::InputError.message(None).to_string(), |
| 141 | + }) |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + let project = projects[selection].clone(); |
| 146 | + |
| 147 | + Ok(project) |
| 148 | +} |
| 149 | + |
| 150 | +async fn create_new_project(env: Environment, path: &str) -> Result<Project, ErrorResponse> { |
| 151 | + let default_name = Path::new(path) |
| 152 | + .file_name() |
| 153 | + .and_then(|os_str| os_str.to_str()) |
| 154 | + .unwrap_or("project") |
| 155 | + .to_lowercase() |
| 156 | + .replace([' ', '-'], "") |
| 157 | + .replace('-', ""); |
| 158 | + |
| 159 | + let name = match Input::<String>::with_theme(&ColorfulTheme::default()) |
| 160 | + .with_prompt("Project name") |
| 161 | + .default(default_name.to_string()) |
| 162 | + .interact() |
| 163 | + { |
| 164 | + Ok(project_name) => project_name, |
| 165 | + Err(_) => { |
| 166 | + return Err(ErrorResponse::Error { |
| 167 | + error_code: ErrorCode::InputError, |
| 168 | + message: ErrorCode::InputError.message(None).to_string(), |
| 169 | + }); |
| 170 | + } |
| 171 | + }; |
| 172 | + |
| 173 | + // Create a repository name: lowercased, remove spaces and special characters |
| 174 | + let re = Regex::new(r"[^a-zA-Z0-9_-]").unwrap(); |
| 175 | + let default_repository = name |
| 176 | + .clone() |
| 177 | + .to_lowercase() |
| 178 | + .replace(' ', "_") |
| 179 | + .replace('-', ""); |
| 180 | + let default_repo = re.replace_all(&default_repository, ""); |
| 181 | + |
| 182 | + let repository = match Input::<String>::with_theme(&ColorfulTheme::default()) |
| 183 | + .default(default_repo.to_string()) |
| 184 | + .with_prompt("Repository") |
| 185 | + .interact() |
| 186 | + { |
| 187 | + Ok(repo) => repo, |
| 188 | + Err(_) => { |
| 189 | + return Err(ErrorResponse::Error { |
| 190 | + error_code: ErrorCode::InputError, |
| 191 | + message: ErrorCode::InputError.message(None).to_string(), |
| 192 | + }); |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + let description = match Input::<String>::with_theme(&ColorfulTheme::default()) |
| 197 | + .with_prompt("Description") |
| 198 | + .interact() |
| 199 | + { |
| 200 | + Ok(description) => description, |
| 201 | + Err(_) => { |
| 202 | + return Err(ErrorResponse::Error { |
| 203 | + error_code: ErrorCode::InputError, |
| 204 | + message: ErrorCode::InputError.message(None).to_string(), |
| 205 | + }); |
| 206 | + } |
| 207 | + }; |
| 208 | + |
| 209 | + let access_token = match get_smb_token(env).await { |
| 210 | + Ok(token) => token, |
| 211 | + Err(_) => { |
| 212 | + return Err(ErrorResponse::Error { |
| 213 | + error_code: ErrorCode::Unauthorized, |
| 214 | + message: ErrorCode::Unauthorized.message(None).to_string(), |
| 215 | + }) |
| 216 | + } |
| 217 | + }; |
| 218 | + |
| 219 | + match create_project( |
| 220 | + env, |
| 221 | + access_token, |
| 222 | + ProjectCreate { |
| 223 | + name, |
| 224 | + repository, |
| 225 | + description, |
| 226 | + }, |
| 227 | + ) |
| 228 | + .await |
| 229 | + { |
| 230 | + Ok(project) => Ok(project), |
| 231 | + Err(e) => Err(e), |
| 232 | + } |
| 233 | +} |
0 commit comments