Skip to content

Commit af7f140

Browse files
committed
feat: add initialization command and enhance app setup with async file handling
1 parent 386125b commit af7f140

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

.env-example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DS_DB_NAME=""
2+
DS_DB_PORT=""
3+
DS_DB_DSN=""
4+
DS_DB_USER=""
5+
DS_DB_PASSWORD=""
6+
DS_DB_HOST=""
7+
8+
DS_DUMP_INTERVAL=""
9+
DS_DUMP_PATH=""

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ reqwest = { version = "0.12.9", features = ["blocking"] }
2626
serde = { version = "1.0.215", features = ["derive"] }
2727
serde_json = "1.0.133"
2828
serde_yaml = "0.9.34"
29+
tokio = { version = "1.41.1", features = ["full"] }
2930

3031
[profile.release]
3132
lto = true

dumpsync1.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
exports:
2+
dump_data: true
3+
compress_data: true
4+
insert_ignore_into: false
5+
drop_table_if_exists: true
6+
database_if_not_exists: true
7+
8+
connection:
9+
max_retries: 3
10+
retry_connection_interval: 5

src/args_cli.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clap_cargo::style;
2-
use clap::builder::styling::Styles;
32

43
use clap::{
54
Parser,
65
Subcommand,
76
ColorChoice,
7+
builder::styling::Styles,
88
};
99

1010
pub const CLAP_STYLING: Styles = Styles::styled()
@@ -32,6 +32,9 @@ pub enum Commands {
3232

3333
/// Import the database dump
3434
Import(ImportOptions),
35+
36+
/// Initialize the new dump sync project
37+
Init,
3538
}
3639

3740
#[derive(Parser)]

src/dump_sync.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
use clap::Parser;
22

3+
use reqwest;
4+
use tokio::fs::File;
5+
use tokio::io::AsyncWriteExt;
6+
7+
use std::error::Error;
8+
39
use crate::{
410
ui::ui_base::UI,
511
helpers::env::Env,
612
engine::dump::Dump,
13+
consts::global::Global,
14+
ui::success_alerts::SuccessAlerts,
715

816
args_cli::{
917
Cli,
@@ -17,6 +25,17 @@ pub struct DumpSync;
1725

1826
impl DumpSync {
1927

28+
async fn initialize(&self) -> Result<(), Box<dyn Error>> {
29+
let response = reqwest::get(Global::APP_CONFIGS).await?;
30+
let content = response.bytes().await?;
31+
32+
let mut file = File::create(Global::app_config()).await?;
33+
file.write_all(&content).await?;
34+
35+
SuccessAlerts::settings();
36+
Ok(())
37+
}
38+
2039
fn import(&self, options: ImportOptions) {
2140
Env::new();
2241
UI::header();
@@ -70,7 +89,7 @@ impl DumpSync {
7089
Dump::new(&host, port, &user, &password, &dbname, &backup_path, Some(interval), &backup_path).export();
7190
}
7291

73-
pub fn init(&self) {
92+
pub async fn init(&self) -> Result<(), Box<dyn Error>> {
7493
let cli = Cli::parse();
7594

7695
match cli.command {
@@ -81,7 +100,13 @@ impl DumpSync {
81100
Commands::Import(options) => {
82101
self.import(options);
83102
},
103+
104+
Commands::Init => {
105+
self.initialize().await?;
106+
},
84107
}
108+
109+
Ok(())
85110
}
86111

87112
}

src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ mod dump_sync;
88

99
use crate::dump_sync::DumpSync;
1010

11-
fn main() {
12-
DumpSync.init();
11+
#[tokio::main]
12+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
13+
if let Err(e) = DumpSync.init().await {
14+
eprintln!("Error initializing app: {}", e);
15+
return Err(e);
16+
}
17+
18+
Ok(())
1319
}

src/ui/success_alerts.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ impl SuccessAlerts {
4949
);
5050
}
5151

52+
pub fn settings() {
53+
let current_datetime = Date::date_time();
54+
55+
println!(
56+
"\r{} The settings file was successfully created",
57+
current_datetime.green().bold(),
58+
);
59+
}
60+
5261
}

0 commit comments

Comments
 (0)