diff --git a/src/commands/get/projects.rs b/src/commands/get/projects.rs index dce0040..35a0ed9 100644 --- a/src/commands/get/projects.rs +++ b/src/commands/get/projects.rs @@ -18,8 +18,9 @@ pub async fn command(args: Args) -> Result<()> { .context("Failed to get projects from server".red())?; let remote_projects = remote_projects .iter() - .filter(|p| !local_projects.iter().any(|lp| lp.project_id == **p)) - .map(|p| format!("{} - {}", p, "Remote".green())); + .filter(|p| !local_projects.iter().any(|lp| lp.project_id == p.project_id)) + .map(|p| format!("{} - {} - {}", p.project_id, p.project_name, "Remote".green())); + let local_projects = local_projects .iter() .map(|p| format!("{} - {}", p.project_id, p.path.display())); diff --git a/src/commands/new/project.rs b/src/commands/new/project.rs index f9c14d7..30e65fd 100644 --- a/src/commands/new/project.rs +++ b/src/commands/new/project.rs @@ -1,4 +1,5 @@ use super::*; +use crate::utils::prompt::prompt_text; use crate::{sdk::SDK, utils::config::get_config}; /// Create a new project @@ -7,12 +8,36 @@ pub struct Args { /// Key #[clap(short, long)] key: Option, + + /// Project name + #[clap(short, long)] + name: Option, + + #[clap(long)] + nn: bool, } pub async fn command(args: Args) -> Result<()> { let config = get_config()?; let key = config.get_key_or_default(args.key)?; - let new_project_id = SDK::new_project(&key.fingerprint).await?; + + // check if nn flag is set + if args.nn { + let new_project_id = SDK::new_project(&key.fingerprint, "").await?; + println!("Created new project with ID: {}", new_project_id); + return Ok(()); + } + + let name; + if args.nn { + name = "".to_string(); + } else { + name = args.name.unwrap_or_else(|| { + prompt_text("What is the name of this project?").unwrap() + }); + } + + let new_project_id = SDK::new_project(&key.fingerprint, &name).await?; println!("Created new project with ID: {}", new_project_id); Ok(()) } diff --git a/src/sdk.rs b/src/sdk.rs index bce3937..7f36f38 100644 --- a/src/sdk.rs +++ b/src/sdk.rs @@ -1,6 +1,7 @@ use super::*; use crate::{ types::ProjectInfo, + types::ListProjects, utils::{ auth::get_token, config::get_config, @@ -81,9 +82,10 @@ impl SDK { project_id: &str, partial_fingerprint: &str, ) -> Result { + // GET /v2/project/:id let client = reqwest::Client::new(); - let url = get_api_url().join("project/")?.join(project_id)?; + let url = get_api_url().join("v2/project/")?.join(project_id)?; let project_info = client .get(url) @@ -431,11 +433,11 @@ impl SDK { pub async fn list_projects( partial_fingerprint: &str, - ) -> Result> { - // GET /projects + ) -> Result> { + // GET /v2/projects let client = reqwest::Client::new(); - let url = get_api_url().join("projects")?; + let url = get_api_url().join("v2/projects")?; let res = client .get(url) @@ -447,24 +449,37 @@ impl SDK { .await .context("Failed to get projects")?; - let res = res - .json::>() + let projects = res + .json::>() .await - .context("Failed to parse API response into Vec")?; + .context("Failed to parse API response into Vec")?; - Ok(res) + let project_data = projects + .iter() + .map(|p| ListProjects { + project_id: p.project_id.clone(), + project_name: p.project_name.clone(), + }) + .collect::>(); + + Ok(project_data) } - pub async fn new_project(partial_fingerprint: &str) -> Result { - // POST /projects/new + pub async fn new_project(partial_fingerprint: &str, project_name: &str) -> Result { + // POST /v2/projects/new let client = reqwest::Client::new(); + let body = json!({ + "name": project_name + }); + let res = client - .post(get_api_url().join("projects/new")?) + .post(get_api_url().join("v2/projects/new")?) .header( header::AUTHORIZATION, Self::auth_header(partial_fingerprint).await?, ) + .json(&body) .send() .await? .text() diff --git a/src/types.rs b/src/types.rs index d972c99..66b4711 100644 --- a/src/types.rs +++ b/src/types.rs @@ -18,9 +18,16 @@ pub struct PartialUser { #[derive(Serialize, Deserialize, Debug)] pub struct ProjectInfo { pub project_id: String, + pub project_name: String, pub users: Vec, } +#[derive(Serialize, Deserialize, Debug)] +pub struct ListProjects { + pub project_id: String, + pub project_name: String, +} + impl Display for User { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{} - {}", self.username, self.id) diff --git a/src/utils/choice.rs b/src/utils/choice.rs index 45279b2..93313e5 100644 --- a/src/utils/choice.rs +++ b/src/utils/choice.rs @@ -14,7 +14,6 @@ impl Choice { let key = config.get_key(partial_fingerprint)?; Ok((key, config)) } - pub async fn choose_project(partial_fingerprint: &str) -> Result { let (key, config) = Self::get_key(partial_fingerprint)?; let all_projects = SDK::list_projects(&key.fingerprint).await?; @@ -23,7 +22,7 @@ impl Choice { let all_projects = all_projects .iter() - .filter(|p| !local_projects.iter().any(|lp| &lp.project_id == *p)) + .filter(|p| !local_projects.iter().any(|lp| lp.project_id == p.project_id)) .collect::>(); let mut options = local_projects @@ -32,7 +31,7 @@ impl Choice { .collect::>(); all_projects.iter().for_each(|p| { - options.push(format!("{} - {}", p, "Remote")); + options.push(format!("{} - {} - {}", p.project_id, p.project_name, "Remote")); }); let selected =