1use anyhow::Result;
2use std::path::Path;
3
4use crate::cli::ProjectAction;
5
6pub fn run(action: &ProjectAction, json: bool) -> Result<()> {
7 let cwd = std::env::current_dir()?;
8
9 match action {
10 ProjectAction::Init { name } => init(&cwd, name, json),
11 ProjectAction::Bind { name } => bind(&cwd, name, json),
12 ProjectAction::Unbind => unbind(&cwd, json),
13 ProjectAction::Delete { name } => delete(name, json),
14 ProjectAction::List => list(json),
15 }
16}
17
18fn init(cwd: &Path, name: &str, json: bool) -> Result<()> {
19 crate::db::init(cwd, name)?;
20
21 if json {
22 println!(
23 "{}",
24 serde_json::json!({"success": true, "project": name, "bound_path": cwd})
25 );
26 } else {
27 let c = crate::color::stderr_theme();
28 eprintln!("{}info:{} initialized project '{name}'", c.blue, c.reset);
29 }
30
31 Ok(())
32}
33
34fn bind(cwd: &Path, name: &str, json: bool) -> Result<()> {
35 crate::db::use_project(cwd, name)?;
36
37 if json {
38 println!(
39 "{}",
40 serde_json::json!({"success": true, "project": name, "bound_path": cwd})
41 );
42 } else {
43 let c = crate::color::stdout_theme();
44 println!("{}bound{} {} -> {name}", c.green, c.reset, cwd.display());
45 }
46
47 Ok(())
48}
49
50fn unbind(cwd: &Path, json: bool) -> Result<()> {
51 crate::db::unbind_project(cwd)?;
52
53 if json {
54 println!("{}", serde_json::json!({"success": true}));
55 } else {
56 let c = crate::color::stderr_theme();
57 eprintln!("{}info:{} unbound {}", c.blue, c.reset, cwd.display());
58 }
59
60 Ok(())
61}
62
63fn delete(name: &str, json: bool) -> Result<()> {
64 crate::db::delete_project(name)?;
65
66 if json {
67 println!("{}", serde_json::json!({"success": true, "project": name}));
68 } else {
69 let c = crate::color::stderr_theme();
70 eprintln!("{}info:{} deleted project '{name}'", c.blue, c.reset);
71 }
72
73 Ok(())
74}
75
76fn list(json: bool) -> Result<()> {
77 let projects = crate::db::list_projects()?;
78
79 if json {
80 println!("{}", serde_json::to_string(&projects)?);
81 } else {
82 for project in projects {
83 println!("{project}");
84 }
85 }
86
87 Ok(())
88}