2 files changed
+95
-12
src/projects/crud/mod.rs
+29
-1
@@ -3,7 +3,7 @@ use anyhow::{anyhow, Result};
3
use reqwest::Client;
4
use serde::{Deserialize, Serialize};
5
6
-#[derive(Deserialize, Debug)]
6
+#[derive(Deserialize, Debug, Serialize)]
7
pub struct Project {
8
pub id: i32,
9
pub name: String,
@@ -16,6 +16,11 @@ pub struct ProjectCreate {
16
pub description: String,
17
}
18
19
+#[derive(Deserialize, Debug, Serialize)]
20
+pub struct Config {
21
+ pub(crate) current_project: Option<Project>,
22
+}
23
+
24
pub async fn get_all() -> Result<Vec<Project>> {
25
// Get current token
26
let token = get_token().await.unwrap();
@@ -62,6 +67,29 @@ pub async fn create_project(project: ProjectCreate) -> Result<Project> {
67
}
68
}
69
70
+pub async fn get_project(id: String) -> Result<Project> {
71
+ // Get current token
72
+ let token = get_token().await.unwrap();
73
+
74
+ let response = Client::new()
75
+ .get([BASE_URL, "v1/projects/", &id].join(""))
76
+ .header("Authorization", token)
77
+ .send()
78
+ .await?;
79
+
80
+ match response.status() {
81
+ reqwest::StatusCode::OK => {
82
+ let project: Project = response.json().await?;
83
+ println!("Project requested: {project:#?}");
84
+ Ok(project)
85
+ }
86
+ _ => {
87
+ debug!("Failed to request a project.", response.status());
88
+ Err(anyhow!("Failed to request a project."))
89
+ }
90
+ }
91
+}
92
+
93
async fn get_token() -> Result<String> {
94
if let Some(mut path) = dirs::home_dir() {
95
path.push(".smb/token");
src/projects/mod.rs
+66
-11
@@ -1,14 +1,19 @@
1
mod crud;
2
3
-use anyhow::Result;
3
+use std::{
4
+ fs::{create_dir_all, File, OpenOptions},
5
+ io::Write,
6
+};
7
+
8
+use anyhow::{anyhow, Result};
9
use clap::Subcommand;
10
use console::style;
11
use dialoguer::{theme::ColorfulTheme, Input};
12
use spinners::Spinner;
13
9
-use crate::util::CommandResult;
14
+use crate::{debug, util::CommandResult};
15
11
-use self::crud::{create_project, get_all, ProjectCreate};
16
+use self::crud::{create_project, get_all, get_project, Config, ProjectCreate};
17
18
#[derive(Subcommand)]
19
pub enum Commands {
@@ -20,9 +25,9 @@ pub enum Commands {
25
26
#[clap(about = "Show detail of a project.")]
27
Show {
23
- /// Project name
28
+ /// Project Id
29
#[clap(short, long, required = true)]
25
- name: String,
30
+ id: String,
31
},
32
33
#[clap(about = "Delete a project.")]
@@ -31,6 +36,12 @@ pub enum Commands {
36
#[clap(short, long, required = true)]
37
name: String,
38
},
39
+
40
+ #[clap(about = "Use project for current CLI session.")]
41
+ Use {
42
+ #[clap(short, long, required = true)]
43
+ id: String,
44
+ },
45
}
46
47
pub async fn process(commands: Commands) -> Result<CommandResult> {
@@ -107,16 +118,27 @@ pub async fn process(commands: Commands) -> Result<CommandResult> {
118
}
119
}
120
}
110
- Commands::Show { name } => {
121
+ Commands::Show { id } => {
122
let spinner = Spinner::new(
123
spinners::Spinners::SimpleDotsScrolling,
124
style("Loading...").green().bold().to_string(),
125
);
115
- Ok(CommandResult {
116
- spinner,
117
- symbol: "✅".to_owned(),
118
- msg: format!("Showing project {name}."),
119
- })
126
+ // Get Detail
127
+ match get_project(id).await {
128
+ Ok(_) => Ok(CommandResult {
129
+ spinner,
130
+ symbol: "✅".to_owned(),
131
+ msg: "Showing all projects.".to_owned(),
132
+ }),
133
+ Err(e) => {
134
+ println!("Error: {e:#?}");
135
+ Ok(CommandResult {
136
+ spinner,
137
+ symbol: "❌".to_owned(),
138
+ msg: "Failed to get all projects.".to_owned(),
139
+ })
140
+ }
141
+ }
142
}
143
Commands::Delete { name } => {
144
let spinner = Spinner::new(
@@ -129,5 +151,38 @@ pub async fn process(commands: Commands) -> Result<CommandResult> {
151
msg: format!("Deleting project {name}."),
152
})
153
}
154
+ Commands::Use { id } => {
155
+ let project = get_project(id).await?;
156
+
157
+ let config = Config {
158
+ current_project: Some(project),
159
+ };
160
+
161
+ let spinner = Spinner::new(
162
+ spinners::Spinners::SimpleDotsScrolling,
163
+ style("Loading...").green().bold().to_string(),
164
+ );
165
+ match home::home_dir() {
166
+ Some(path) => {
167
+ debug!(path.to_str().unwrap());
168
+ let mut file = OpenOptions::new()
169
+ .create(true)
170
+ .write(true)
171
+ .open([path.to_str().unwrap(), "/.smb/config.json"].join(""))?;
172
+ let json = serde_json::to_string(&config)?;
173
+ file.write_all(json.as_bytes())?;
174
+
175
+ Ok(CommandResult {
176
+ spinner,
177
+ symbol: "✅".to_owned(),
178
+ msg: format!("Use project successful."),
179
+ })
180
+ }
181
+ None => {
182
+ let error = anyhow!("Failed to get home directory.");
183
+ return Err(error);
184
+ }
185
+ }
186
+ }
187
}
188
}