4 files changed
+84
-36
smbpndk-cli/src/auth_app/mod.rs
+43
-30
@@ -1,12 +1,10 @@
1
-use std::{fs::OpenOptions, io::Write};
2
-
1
use crate::{auth_app::cli::Commands, cli::CommandResult};
2
use anyhow::{anyhow, Result};
3
use console::style;
6
-use dialoguer::{theme::ColorfulTheme, Input};
7
-use log::debug;
8
-use smbpndk_model::{AuthAppCreate, Config};
4
+use dialoguer::{theme::ColorfulTheme, Confirm, Input};
5
+use smbpndk_model::AuthAppCreate;
6
use smbpndk_networking_auth_app::{create_auth_app, delete_auth_app, get_auth_app, get_auth_apps};
7
+use smbpndk_utils::{get_config, write_config};
8
use spinners::Spinner;
9
10
pub(crate) mod cli;
@@ -88,7 +86,7 @@ pub async fn process_auth_app(commands: Commands) -> Result<CommandResult> {
86
spinners::Spinners::SimpleDotsScrolling,
87
style("Loading...").green().bold().to_string(),
88
);
91
- match get_auth_app(id).await {
89
+ match get_auth_app(&id).await {
90
Ok(_) => Ok(CommandResult {
91
spinner,
92
symbol: "✅".to_owned(),
@@ -126,35 +124,50 @@ pub async fn process_auth_app(commands: Commands) -> Result<CommandResult> {
124
}
125
}
126
Commands::Use { id } => {
129
- let project = get_auth_app(id).await?;
127
+ let mut spinner = Spinner::new(
128
+ spinners::Spinners::SimpleDotsScrolling,
129
+ style("Checking config file...").green().bold().to_string(),
130
+ );
131
131
- let config = Config {
132
- current_project: None,
133
- current_auth_app: Some(project),
134
- };
132
+ let auth_app = get_auth_app(&id).await?;
133
+ let mut config = get_config().await?;
134
+
135
+ if let Some(auth_app) = config.current_auth_app {
136
+ if auth_app.id != id {
137
+ spinner.stop_with_message("Found a config.".to_string());
138
+ let yes = Confirm::new()
139
+ .with_prompt(format!(
140
+ "Will change active auth_app to {}. Do you want to continue?",
141
+ &id
142
+ ))
143
+ .interact()?;
144
+ if !yes {
145
+ let spinner = Spinner::new(
146
+ spinners::Spinners::SimpleDotsScrolling,
147
+ style("Cancelling operation...").green().bold().to_string(),
148
+ );
149
+ return Ok(CommandResult {
150
+ spinner,
151
+ symbol: "✅".to_owned(),
152
+ msg: "Operation cancelled.".to_string(),
153
+ });
154
+ }
155
+ }
156
+ }
157
158
+ config.current_auth_app = Some(auth_app);
159
let spinner = Spinner::new(
160
spinners::Spinners::SimpleDotsScrolling,
138
- style("Loading...").green().bold().to_string(),
161
+ style("Saving config...").green().bold().to_string(),
162
);
140
- match home::home_dir() {
141
- Some(path) => {
142
- debug!("{}", path.to_str().unwrap());
143
- let mut file = OpenOptions::new()
144
- .create(true)
145
- .write(true)
146
- .open([path.to_str().unwrap(), "/.smb/config.json"].join(""))?;
147
- let json = serde_json::to_string(&config)?;
148
- file.write_all(json.as_bytes())?;
149
-
150
- Ok(CommandResult {
151
- spinner,
152
- symbol: "✅".to_owned(),
153
- msg: "Use project successful.".to_string(),
154
- })
155
- }
156
- None => {
157
- let error = anyhow!("Failed to get home directory.");
163
+ match write_config(config) {
164
+ Ok(_) => Ok(CommandResult {
165
+ spinner,
166
+ symbol: "✅".to_owned(),
167
+ msg: format!("Using auth_app: {:?}", &id),
168
+ }),
169
+ Err(_) => {
170
+ let error = anyhow!("Failed while writing config.");
171
Err(error)
172
}
173
}
smbpndk-networking-auth-app/src/lib.rs
+7
-4
@@ -23,12 +23,12 @@ pub async fn get_auth_apps() -> Result<Vec<AuthApp>> {
23
}
24
}
25
26
-pub async fn get_auth_app(id: String) -> Result<AuthApp> {
26
+pub async fn get_auth_app(id: &str) -> Result<AuthApp> {
27
// Get current token
28
let token = get_token().await?;
29
30
let response = Client::new()
31
- .get([BASE_URL, "v1/auth_apps/", &id].join(""))
31
+ .get([BASE_URL, "v1/auth_apps/", id].join(""))
32
.header("Authorization", token)
33
.send()
34
.await?;
@@ -36,10 +36,13 @@ pub async fn get_auth_app(id: String) -> Result<AuthApp> {
36
match response.status() {
37
reqwest::StatusCode::OK => {
38
let auth_app: AuthApp = response.json().await?;
39
- println!("Project requested: {auth_app:#?}");
39
+ println!("Auth app requested: {auth_app:#?}");
40
Ok(auth_app)
41
}
42
- _ => Err(anyhow!("Failed to request an auth app.")),
42
+ _ => Err(anyhow!(format!(
43
+ "Failed to find an auth app with id: {}.",
44
+ id
45
+ ))),
46
}
47
}
48
smbpndk-networking/src/lib.rs
+1
-1
@@ -8,7 +8,7 @@ pub async fn get_token() -> Result<String> {
8
path.push(".smb/token");
9
std::fs::read_to_string(path).map_err(|e| {
10
debug!("Error while reading token: {}", &e);
11
- anyhow!("Are you logged in?")
11
+ anyhow!("Error while reading token. Are you logged in?")
12
})
13
} else {
14
Err(anyhow!("Failed to get home directory."))
smbpndk-utils/src/lib.rs
+33
-1
@@ -18,6 +18,38 @@ pub fn email_validation(input: &str) -> Result<(), &'static str> {
18
}
19
}
20
21
+pub async fn get_config() -> Result<Config> {
22
+ match home::home_dir() {
23
+ Some(mut path) => {
24
+ path.push(".smb/config.json");
25
+ if !path.exists() {
26
+ let config = Config {
27
+ current_project: None,
28
+ current_auth_app: None,
29
+ };
30
+ return Ok(config);
31
+ }
32
+ let config_string = std::fs::read_to_string(path).map_err(|e| {
33
+ debug!("Error while reading config file: {}", &e);
34
+ anyhow!("Error while reading config file. Are you logged in?")
35
+ })?;
36
+ let config: Config = serde_json::from_str(&config_string).map_err(|e| {
37
+ debug!("Error while parsing config: {}", &e);
38
+ anyhow!("Error while parsing config. Are you logged in?")
39
+ })?;
40
+
41
+ Ok(config)
42
+ }
43
+ None => {
44
+ let config = Config {
45
+ current_project: None,
46
+ current_auth_app: None,
47
+ };
48
+ Ok(config)
49
+ }
50
+ }
51
+}
52
+
53
pub fn write_config(config: Config) -> Result<Config> {
54
match home::home_dir() {
55
Some(path) => {
@@ -31,6 +63,6 @@ pub fn write_config(config: Config) -> Result<Config> {
63
64
Ok(config)
65
}
34
- None => Err(anyhow!("Error getting config.")),
66
+ None => Err(anyhow!("Error getting home directory.")),
67
}
68
}