Skip to content

Commit 19c9fbe

Browse files
author
ThePhoenixPixel
committed
cargo fmt
1 parent f559535 commit 19c9fbe

File tree

15 files changed

+141
-159
lines changed

15 files changed

+141
-159
lines changed

src/cloud.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::error::Error;
21
use bx::network::url::Url;
32
use colored::{ColoredString, Colorize};
4-
use std::{env, fs};
3+
use std::error::Error;
54
use std::path::PathBuf;
65
use std::sync::Arc;
76
use std::time::Duration;
7+
use std::{env, fs};
88
use tokio::sync::RwLock;
99

1010
use crate::core::services_all::AllServices;
@@ -17,7 +17,6 @@ use crate::terminal::cmd::Cmd;
1717
use crate::utils::logger::Logger;
1818
use crate::{log_error, log_info, log_warning};
1919

20-
2120
#[cfg(feature = "rest-api")]
2221
use crate::rest_api::restapi_main::ApiMain;
2322
use crate::utils::service_status::ServiceStatus;
@@ -80,7 +79,9 @@ impl Cloud {
8079
SoftwareConfig::check(&url).await;
8180

8281
// check the software files
83-
Cloud::check_software().await.expect("Checking Software failed");
82+
Cloud::check_software()
83+
.await
84+
.expect("Checking Software failed");
8485

8586
// NodeServer
8687
{
@@ -144,7 +145,6 @@ impl Cloud {
144145
}
145146
}
146147

147-
148148
pub fn print_icon() {
149149
println!(" ");
150150
println!(
@@ -430,4 +430,3 @@ impl Cloud {
430430
Ok(())
431431
}
432432
}
433-

src/core/group.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::{fs, io};
2-
use std::path::PathBuf;
31
use bx::path::Directory;
42
use rand::Rng;
53
use rand::seq::IndexedRandom;
64
use serde::{Deserialize, Serialize};
5+
use std::path::PathBuf;
6+
use std::{fs, io};
77

88
use crate::core::installer::Installer;
99
use crate::core::template::Template;
@@ -13,14 +13,13 @@ use crate::utils::error::CloudError;
1313
use crate::utils::error_kind::CloudErrorKind::*;
1414

1515
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
16-
pub struct Group{
16+
pub struct Group {
1717
name: String,
1818
installer: Installer,
1919
templates: Vec<Template>,
2020
}
2121

2222
impl Group {
23-
2423
pub fn get_name(&self) -> String {
2524
self.name.clone()
2625
}
@@ -34,9 +33,7 @@ impl Group {
3433
}
3534

3635
pub fn get_all() -> Vec<Group> {
37-
let group_path = CloudConfig::get()
38-
.get_cloud_path()
39-
.get_group_folder_path();
36+
let group_path = CloudConfig::get().get_cloud_path().get_group_folder_path();
4037

4138
if !group_path.exists() || !group_path.is_dir() {
4239
return Vec::new();
@@ -49,16 +46,13 @@ impl Group {
4946
.filter_map(|entry| {
5047
let file_str = entry.file_name().to_str()?.to_string();
5148

52-
file_str.strip_suffix(".json")
53-
.and_then(Self::get_from_name)
49+
file_str.strip_suffix(".json").and_then(Self::get_from_name)
5450
})
5551
.collect()
5652
}
5753

5854
pub fn get_from_name(name: &str) -> Option<Group> {
59-
let group_path = CloudConfig::get()
60-
.get_cloud_path()
61-
.get_group_folder_path();
55+
let group_path = CloudConfig::get().get_cloud_path().get_group_folder_path();
6256

6357
Directory::get_files_name_from_path(&group_path)
6458
.into_iter()
@@ -71,8 +65,7 @@ impl Group {
7165

7266
pub fn get_from_path(path: &PathBuf) -> io::Result<Group> {
7367
let content = fs::read_to_string(path)?;
74-
serde_json::from_str(&content)
75-
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
68+
serde_json::from_str(&content).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
7669
}
7770

7871
pub fn get_templates_sorted_by_priority(&self) -> Vec<Template> {
@@ -121,14 +114,12 @@ impl Group {
121114
pub fn install_in_path(&self, target_path: &PathBuf) -> Result<(), CloudError> {
122115
let mut templates: Vec<Template> = Vec::new();
123116
match self.get_installer() {
124-
Installer::InstallAll => templates = self.get_templates_sorted_by_priority(),
125-
Installer::InstallAllDesc => templates = self.get_templates_sorted_by_priority_desc(),
126-
Installer::InstallRandom => {
127-
match self.get_template_rng() {
128-
Some(template) => templates.push(template.clone()),
129-
None => return Err(error!(GroupTemplateNotFound)),
130-
}
131-
}
117+
Installer::InstallAll => templates = self.get_templates_sorted_by_priority(),
118+
Installer::InstallAllDesc => templates = self.get_templates_sorted_by_priority_desc(),
119+
Installer::InstallRandom => match self.get_template_rng() {
120+
Some(template) => templates.push(template.clone()),
121+
None => return Err(error!(GroupTemplateNotFound)),
122+
},
132123
Installer::InstallRandomWithPriority => {
133124
match self.get_template_rng_based_on_priority() {
134125
Some(template) => templates.push(template.clone()),
@@ -138,9 +129,9 @@ impl Group {
138129
}
139130

140131
for template in templates {
141-
Directory::copy_folder_contents(&template.get_path(), &target_path).map_err(|e| error!(CantCopyGroupTemplateToNewServiceFolder, e))?;
132+
Directory::copy_folder_contents(&template.get_path(), &target_path)
133+
.map_err(|e| error!(CantCopyGroupTemplateToNewServiceFolder, e))?;
142134
}
143135
Ok(())
144136
}
145-
146137
}

src/core/installer.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ pub enum Installer {
1111
impl Installer {
1212
pub fn from(s: &str) -> Self {
1313
match s {
14-
"InstallAll" => Installer::InstallAll,
15-
"InstallAllDesc" => Installer::InstallAllDesc,
16-
"InstallRandom" => Installer::InstallRandom,
17-
"InstallRandomWithPriority" => Installer::InstallRandomWithPriority,
18-
_ => Installer::InstallAll,
14+
"InstallAll" => Installer::InstallAll,
15+
"InstallAllDesc" => Installer::InstallAllDesc,
16+
"InstallRandom" => Installer::InstallRandom,
17+
"InstallRandomWithPriority" => Installer::InstallRandomWithPriority,
18+
_ => Installer::InstallAll,
1919
}
2020
}
2121
}
2222

2323
impl Into<&str> for Installer {
2424
fn into(self) -> &'static str {
2525
match self {
26-
Installer::InstallAll => "InstallAll",
27-
Installer::InstallAllDesc => "InstallAllDesc",
28-
Installer::InstallRandom => "InstallRandom",
29-
Installer::InstallRandomWithPriority => "InstallRandomWithPriority",
26+
Installer::InstallAll => "InstallAll",
27+
Installer::InstallAllDesc => "InstallAllDesc",
28+
Installer::InstallRandom => "InstallRandom",
29+
Installer::InstallRandomWithPriority => "InstallRandomWithPriority",
3030
}
3131
}
3232
}

src/core/service.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use chrono::{DateTime, Local};
55
use serde::{Deserialize, Serialize};
66
use serde_json::json;
77
use std::collections::HashMap;
8-
use std::{fs, io};
98
use std::fs::{File, read_to_string};
109
use std::io::{Error, ErrorKind, Write};
1110
use std::path::PathBuf;
1211
use std::process::{Child, Command, Stdio};
1312
use std::sync::Arc;
1413
use std::time::Duration;
14+
use std::{fs, io};
1515
use tokio::sync::RwLock;
1616
use tokio::time::timeout;
1717
use uuid::Uuid;
@@ -22,12 +22,12 @@ use crate::core::task::Task;
2222
use crate::node_api::node_service::ServiceInfoResponse;
2323
use crate::sys_config::cloud_config::CloudConfig;
2424
use crate::sys_config::software_config::SoftwareName;
25+
use crate::utils::error::CloudError;
26+
use crate::utils::error_kind::CloudErrorKind::*;
2527
use crate::utils::logger::Logger;
2628
use crate::utils::service_status::ServiceStatus;
2729
use crate::utils::utils::Utils;
2830
use crate::{error, log_error, log_info, log_warning};
29-
use crate::utils::error::CloudError;
30-
use crate::utils::error_kind::CloudErrorKind::*;
3131

3232
#[derive(Serialize)]
3333
struct RegisterServerData {
@@ -56,7 +56,7 @@ impl Service {
5656
&CloudConfig::get().get_server_host(),
5757
&task.get_start_port(),
5858
)) {
59-
Some(port ) => port,
59+
Some(port) => port,
6060
None => return Err(error!(NextFreePortNotFound)),
6161
};
6262
let server_address = Address::new(&CloudConfig::get().get_server_host(), &port);
@@ -192,7 +192,8 @@ impl Service {
192192
return Err(error!(CantFindIPConfigFilePath));
193193
}
194194

195-
let file_content_ip = read_to_string(&path_ip).map_err(|e| error!(CantReadFileToString, e))?;
195+
let file_content_ip =
196+
read_to_string(&path_ip).map_err(|e| error!(CantReadFileToString, e))?;
196197
let edit_file_ip = file_content_ip.replace("%ip%", &*address.get_ip());
197198
fs::write(&path_ip, edit_file_ip).map_err(|e| error!(CantWriteIP, e))?;
198199

@@ -203,7 +204,8 @@ impl Service {
203204
return Err(error!(CantFindPortConfigFilePath));
204205
}
205206

206-
let file_content_port = read_to_string(&path_port).map_err(|e| error!(CantReadFileToString, e))?;
207+
let file_content_port =
208+
read_to_string(&path_port).map_err(|e| error!(CantReadFileToString, e))?;
207209
let edit_file_port =
208210
file_content_port.replace("%port%", address.get_port().to_string().as_str());
209211
fs::write(&path_port, edit_file_port).map_err(|e| error!(CantWritePort, e))?;
@@ -264,10 +266,10 @@ impl Service {
264266
}
265267
Err(e) => {
266268
log_error!(
267-
"Stop command nicht senden an {} \n Error: {}",
268-
self.get_name(),
269-
e.to_string()
270-
);
269+
"Stop command nicht senden an {} \n Error: {}",
270+
self.get_name(),
271+
e.to_string()
272+
);
271273

272274
if self.get_process().is_none() {
273275
self.set_status(ServiceStatus::Stop);
@@ -283,19 +285,18 @@ impl Service {
283285
Err(..) => log_warning!("Service konnte nicht gekillt werden"),
284286
}
285287
}
286-
288+
287289
if self.is_delete() {
288-
self.delete_files();
290+
self.delete_files();
289291
} else {
290292
self.set_status(ServiceStatus::Stop);
291-
self.save_to_file();
293+
self.save_to_file();
292294
}
293-
294295
} else {
295296
// TODO: Remote/Cluster shutdown
296297
}
297298
}
298-
299+
299300
pub fn is_delete(&self) -> bool {
300301
!self.get_task().is_static_service() && self.get_task().is_delete_on_stop()
301302
}
@@ -502,23 +503,22 @@ impl Service {
502503

503504
match url.post(&body, Duration::from_secs(3)).await {
504505
Ok(_) => log_info!(
505-
"Service {} successfully connected to Proxy [{}]",
506-
self.get_name(),
506+
"Service {} successfully connected to Proxy [{}]",
507+
self.get_name(),
507508
service_proxy.get_name()
508-
),
509+
),
509510
Err(e) => log_warning!(
510-
"Service | {} | can't send request connect to Network \n Error: {}",
511-
self.get_name(),
512-
e.to_string()
513-
),
511+
"Service | {} | can't send request connect to Network \n Error: {}",
512+
self.get_name(),
513+
e.to_string()
514+
),
514515
}
515516
}
516517

517518
// TODO: Send New Started Service To Cluster
518519
Ok(())
519520
}
520521

521-
522522
pub async fn disconnect_from_network(&self, cloud: Arc<RwLock<Cloud>>) -> Result<(), Error> {
523523
if self.is_proxy() {
524524
return Ok(());
@@ -552,13 +552,15 @@ impl Service {
552552
self.prepare_to_start()?;
553553
let server_file_path = match self.get_path_with_server_file().to_str() {
554554
Some(server_file_path) => server_file_path.to_string(),
555-
None => return Err(error!(CantConvertServerFilePathToString))
555+
None => return Err(error!(CantConvertServerFilePathToString)),
556556
};
557557

558558
let software_name = self.get_software_name();
559559
let mut placeholders = HashMap::new();
560-
let stdout_file = File::create(self.get_path_stdout_file()).map_err(|e| error!(CantCreateSTDOUTFile, e))?;
561-
let stderr_file = File::create(self.get_path_stderr_file()).map_err(|e| error!(CantCreateSTDERRFile, e))?;
560+
let stdout_file = File::create(self.get_path_stdout_file())
561+
.map_err(|e| error!(CantCreateSTDOUTFile, e))?;
562+
let stderr_file = File::create(self.get_path_stderr_file())
563+
.map_err(|e| error!(CantCreateSTDERRFile, e))?;
562564

563565
placeholders.insert("ip", self.get_server_address().get_ip().to_string());
564566
placeholders.insert("port", self.get_server_address().get_port().to_string());
@@ -576,7 +578,8 @@ impl Service {
576578
.stdout(Stdio::from(stdout_file))
577579
.stderr(Stdio::from(stderr_file))
578580
.stdin(Stdio::piped())
579-
.spawn().map_err(|e| error!(CantStartServer, e))?;
581+
.spawn()
582+
.map_err(|e| error!(CantStartServer, e))?;
580583

581584
self.set_process(Some(child));
582585
Ok(self)
@@ -635,7 +638,8 @@ impl Service {
635638
.join(self.get_task().get_software().get_software_type())
636639
.join(self.get_task().get_software().get_name());
637640

638-
Directory::copy_folder_contents(&software_lib_path, &self.get_path()).map_err(|e| error!(Internal, e))
641+
Directory::copy_folder_contents(&software_lib_path, &self.get_path())
642+
.map_err(|e| error!(Internal, e))
639643
}
640644

641645
pub fn is_proxy(&self) -> bool {
@@ -651,7 +655,8 @@ impl Service {
651655

652656
fn find_port(ports: Vec<u32>, mut port: u32, server_host: &String) -> u32 {
653657
while ports.contains(&port) || !Address::is_port_available(&Address::new(&server_host, &port)) {
654-
port = Address::find_next_port(&mut Address::new(&server_host, &(port + 1))).unwrap_or_else(|| 0);
658+
port = Address::find_next_port(&mut Address::new(&server_host, &(port + 1)))
659+
.unwrap_or_else(|| 0);
655660
}
656661
port
657662
}

src/core/services_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::core::service::Service;
44
use crate::core::services_local::LocalServices;
55
use crate::core::services_network::NetworkServices;
66
use crate::core::task::Task;
7+
use crate::utils::error::CloudError;
78
use crate::utils::logger::Logger;
89
use crate::{log_error, log_info};
9-
use crate::utils::error::CloudError;
1010

1111
pub struct AllServices {
1212
local_services: LocalServices,

0 commit comments

Comments
 (0)