Skip to content

Commit ab8e609

Browse files
author
ThePhoenixPixel
committed
change create dir's to fs::create_dir_all
1 parent 350e8f9 commit ab8e609

File tree

6 files changed

+52
-48
lines changed

6 files changed

+52
-48
lines changed

src/cloud.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
use std::error::Error;
2+
use bx::network::url::Url;
3+
use colored::{ColoredString, Colorize};
4+
use std::{env, fs};
5+
use std::path::PathBuf;
6+
use std::sync::Arc;
7+
use std::time::Duration;
8+
use tokio::sync::RwLock;
9+
110
use crate::core::services_all::AllServices;
211
use crate::core::services_local::LocalServices;
312
use crate::core::services_network::NetworkServices;
@@ -7,14 +16,7 @@ use crate::sys_config::software_config::SoftwareConfig;
716
use crate::terminal::cmd::Cmd;
817
use crate::utils::logger::Logger;
918
use crate::{log_error, log_info, log_warning};
10-
use bx::network::url::Url;
11-
use bx::path::Directory;
12-
use colored::{ColoredString, Colorize};
13-
use std::env;
14-
use std::path::PathBuf;
15-
use std::sync::Arc;
16-
use std::time::Duration;
17-
use tokio::sync::RwLock;
19+
1820

1921
#[cfg(feature = "rest-api")]
2022
use crate::rest_api::restapi_main::ApiMain;
@@ -72,13 +74,13 @@ impl Cloud {
7274
CloudConfig::check(&url).await;
7375

7476
// check folder
75-
Cloud::check_folder();
77+
Cloud::check_folder().expect("Checking Folder failed");
7678

7779
// check software config file
7880
SoftwareConfig::check(&url).await;
7981

8082
// check the software files
81-
Cloud::check_software().await;
83+
Cloud::check_software().await.expect("Checking Software failed");
8284

8385
// NodeServer
8486
{
@@ -252,46 +254,48 @@ impl Cloud {
252254
}
253255
}
254256

255-
pub fn check_folder() {
257+
pub fn check_folder() -> Result<(), Box<dyn Error>> {
256258
let config_path = CloudConfig::get().get_cloud_path();
257259

258260
// create task folder
259-
Directory::create_path(&config_path.get_task_folder_path());
261+
fs::create_dir_all(&config_path.get_task_folder_path())?;
260262

261263
// create template folder
262-
Directory::create_path(&config_path.get_template_folder_path());
264+
fs::create_dir_all(&config_path.get_template_folder_path())?;
263265

264266
// create service temp folder
265-
Directory::create_path(&config_path.get_service_folder().get_temp_folder_path());
267+
fs::create_dir_all(&config_path.get_service_folder().get_temp_folder_path())?;
266268

267269
// create service static folder
268-
Directory::create_path(&config_path.get_service_folder().get_static_folder_path());
270+
fs::create_dir_all(&config_path.get_service_folder().get_static_folder_path())?;
269271

270272
// create system_plugins_folder
271-
Directory::create_path(
273+
fs::create_dir_all(
272274
&config_path
273275
.get_system_folder()
274276
.get_system_plugins_folder_path(),
275-
);
277+
)?;
276278

277279
// create software_files_folder
278-
Directory::create_path(
280+
fs::create_dir_all(
279281
&config_path
280282
.get_system_folder()
281283
.get_software_files_folder_path(),
282-
);
284+
)?;
283285

284286
// create software_lib_folder
285-
Directory::create_path(
287+
fs::create_dir_all(
286288
&config_path
287289
.get_system_folder()
288290
.get_software_lib_folder_path(),
289-
);
291+
)?;
290292
log_info!("All Folders are safe :=)");
293+
Ok(())
291294
}
292295

293296
// check software && system plugins
294-
pub async fn check_software() {
297+
pub async fn check_software() -> Result<(), Box<dyn Error>> {
298+
// Todo: refactoren 'check_software()'
295299
let software_types = SoftwareConfig::get().get_software_types();
296300
let cloud_config_system = CloudConfig::get().get_cloud_path().get_system_folder();
297301

@@ -309,9 +313,9 @@ impl Cloud {
309313
.join(&software_type_name);
310314

311315
// create the software types folder
312-
Directory::create_path(&software_path);
313-
Directory::create_path(&system_plugins_path);
314-
Directory::create_path(&software_lib_path);
316+
fs::create_dir_all(&software_path)?;
317+
fs::create_dir_all(&system_plugins_path)?;
318+
fs::create_dir_all(&software_lib_path)?;
315319

316320
// iter to software names
317321
for software in software_type.get_software_names() {
@@ -344,7 +348,7 @@ impl Cloud {
344348
}
345349
Err(e) => {
346350
log_error!("{}", e.to_string());
347-
panic!("Can not download the software {}", software_file_url);
351+
return Err(e);
348352
}
349353
}
350354
}
@@ -369,10 +373,7 @@ impl Cloud {
369373
}
370374
Err(e) => {
371375
log_error!("{}", e.to_string());
372-
panic!(
373-
"Can not download the Software System Plugin {}",
374-
software.get_system_plugin().get_download()
375-
);
376+
return Err(e);
376377
}
377378
}
378379
}
@@ -387,7 +388,7 @@ impl Cloud {
387388
{
388389
let mut path = software_lib_path.clone();
389390
path.pop();
390-
Directory::create_path(&path);
391+
fs::create_dir_all(&path)?;
391392

392393
match Url::download_file(&url_str, &software_lib_path).await {
393394
Ok(_) => log_info!(
@@ -405,6 +406,7 @@ impl Cloud {
405406
}
406407
}
407408
}
409+
Ok(())
408410
}
409411
}
410412

src/core/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl Service {
422422

423423
pub fn save_to_file(&self) {
424424
let path = self.get_path_with_service_config();
425-
Directory::create_path(&path);
425+
fs::create_dir_all(&path).expect("Cant create Service File in 'save_to_file'");
426426
if File::create(self.get_path_with_service_file()).is_err() {
427427
log_error!("Error by create to service config file");
428428
return;
@@ -637,7 +637,7 @@ impl Service {
637637
.join(&software.get_system_plugin().get_path());
638638

639639
if !target_path.exists() {
640-
Directory::create_path(&target_path);
640+
fs::create_dir_all(&target_path)?;
641641
}
642642

643643
target_path.push(self.get_task().get_software().get_system_plugin_name());

src/core/services_local.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use crate::core::service::Service;
2-
use crate::core::task::Task;
3-
use crate::sys_config::cloud_config::CloudConfig;
41
use bx::path::Directory;
52
use std::io::Error;
63
use std::path::PathBuf;
74
use uuid::Uuid;
85

6+
use crate::core::service::Service;
7+
use crate::core::task::Task;
8+
use crate::sys_config::cloud_config::CloudConfig;
9+
10+
911
pub struct LocalServices {
1012
services: Vec<Service>,
1113
}
@@ -117,7 +119,7 @@ impl LocalServices {
117119
}
118120

119121
pub async fn stop_all(&mut self, shutdown_msg: &str) {
120-
let ids: Vec<Uuid> = self.get_started_services()
122+
let ids: Vec<Uuid> = self.get_all()
121123
.iter()
122124
.map(|s| s.get_id())
123125
.collect();

src/core/task.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bx::path::Directory;
22
use rand::Rng;
33
use serde::{Deserialize, Serialize};
44
use std::fs::File;
5-
use std::io::{Read, Write};
5+
use std::io::{Error, Read, Write};
66
use std::path::PathBuf;
77
use std::{fs, io};
88

@@ -423,7 +423,7 @@ impl Task {
423423

424424
pub fn prepared_to_service(&self) -> Result<PathBuf, String> {
425425
// create the next free service folder with the template
426-
let target_path = &self.create_next_free_service_folder();
426+
let target_path = &self.create_next_free_service_folder().map_err(|e| e.to_string())?;
427427
let templates = &self.get_templates();
428428
let template = match select_template_with_priority(&templates) {
429429
Some(template) => template,
@@ -442,7 +442,7 @@ impl Task {
442442
}
443443

444444
// create the next not exist service folder
445-
fn create_next_free_service_folder(&self) -> PathBuf {
445+
fn create_next_free_service_folder(&self) -> Result<PathBuf, Error> {
446446
let mut folder_index: u32 = 1;
447447
let target_base_path = self.get_service_path();
448448
let mut target_service_folder_path =
@@ -454,8 +454,8 @@ impl Task {
454454
target_base_path.join(format!("{}-{}", &self.get_name(), folder_index));
455455
}
456456

457-
Directory::create_path(&target_service_folder_path);
458-
target_service_folder_path
457+
fs::create_dir_all(&target_service_folder_path)?;
458+
Ok(target_service_folder_path)
459459
}
460460

461461
//get temp or static for the service

src/core/template.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use std::fs;
12
use serde::{Deserialize, Serialize};
23
use std::path::PathBuf;
3-
use bx::path::Directory;
44

55
use crate::core::task::Task;
66
use crate::sys_config::cloud_config::CloudConfig;
@@ -83,12 +83,12 @@ impl Template {
8383
template_path.push("default");
8484

8585
if !template_path.exists() {
86-
Directory::create_path(&template_path);
86+
fs::create_dir_all(template_path).expect("Cant create Template Path in 'create_by_task'");
8787
}
8888
}
8989

9090
pub fn create(&self) {
91-
Directory::create_path(&self.get_path());
91+
fs::create_dir_all(&self.get_path()).expect("Cant create Template Path in 'create'");
9292
}
9393

9494
pub fn exists(&self) -> bool {

src/utils/logger.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use chrono::Local;
22
use colored::{ColoredString, Colorize};
3-
use std::env;
3+
use std::{env, fs};
44
use std::fs::OpenOptions;
55
use std::io::Write;
6-
use bx::path::Directory;
76

87
use crate::sys_config::cloud_config::CloudConfig;
98
use crate::utils::log::Log;
109

10+
1111
pub struct Logger;
1212

1313
impl Logger {
@@ -47,7 +47,7 @@ impl Logger {
4747
env::current_exe().expect("Cloud Error can not get the exe path of the cloud system");
4848
log_path.pop();
4949
log_path.push("log");
50-
Directory::create_path(&log_path);
50+
fs::create_dir_all(&log_path).expect("Cant create Log File path in 'write_in_file'");
5151

5252
let file_name = format!("log_{}.log", Local::now().format("%Y-%m-%d"));
5353
log_path.push(&file_name);

0 commit comments

Comments
 (0)