Skip to content

Commit b9a2d60

Browse files
author
chenjunliang
committed
更新代码:
1.分离unix系统依赖及代码
1 parent a0dc28d commit b9a2d60

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ repository = "https://gitee.com/seeker_rs/mysql-audit-extend"
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14-
daemonize = "0.4.1"
15-
users = "0.11.0"
1614
rcron = "1.2.1"
1715
clap = { version = "3.2.16", features = ["derive"] }
1816
anyhow = "1.0.61"
1917
chrono = "0.4.21"
2018
rust_decimal = "1.25.0"
2119
log = "0.4.14"
22-
simple_logger = "2.1.0"
20+
simple_logger = "2.1.0"
21+
22+
[target.'cfg(unix)'.dependencies]
23+
daemonize = "0.4.1"
24+
users = "0.11.0"

src/daemon_util.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
#[cfg(target_family = "unix")]
12
use crate::tasks::start_backstage_task;
3+
#[cfg(target_family = "unix")]
24
use daemonize::Daemonize;
5+
#[cfg(target_family = "unix")]
36
use std::fs;
7+
#[cfg(target_family = "unix")]
48
use std::fs::File;
59

10+
#[cfg(target_family = "unix")]
611
pub fn daemonize(
712
username: &str,
813
pkg_name: &str,

src/libc_util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#[cfg(target_family = "unix")]
12
use users::{get_current_uid, get_user_by_uid};
23

4+
#[cfg(target_family = "unix")]
35
pub fn get_current_user() -> String {
46
let user = get_user_by_uid(get_current_uid()).unwrap();
57
let username = user.name().to_str().unwrap().to_string();

src/main.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
#[cfg(target_family = "unix")]
12
mod daemon_util;
3+
#[cfg(target_family = "unix")]
24
mod libc_util;
35
mod tasks;
46

57
use anyhow::Result;
68
use clap::Parser;
79
use std::fs;
10+
use crate::tasks::start_backstage_task;
811

912
/// An extension tool of mysql-audit, which provides functions such as log rotation and log cleaning.
1013
#[derive(Parser, Debug)]
@@ -39,20 +42,28 @@ fn main() -> Result<()> {
3942

4043
init_log();
4144

42-
let username = libc_util::get_current_user();
43-
let pkg_name = env!("CARGO_PKG_NAME");
44-
let author_name = env!("CARGO_PKG_AUTHORS");
4545
let path: String = options.path;
4646
let max_size: u32 = options.max_size;
4747
let max_file: u32 = options.max_file;
48-
daemon_util::daemonize(
49-
username.as_str(),
50-
pkg_name,
51-
author_name,
52-
path,
53-
max_size,
54-
max_file,
55-
);
48+
49+
if cfg!(target_family = "unix") {
50+
#[cfg(target_family = "unix")]
51+
{
52+
let username = libc_util::get_current_user();
53+
let pkg_name = env!("CARGO_PKG_NAME");
54+
let author_name = env!("CARGO_PKG_AUTHORS");
55+
daemon_util::daemonize(
56+
username.as_str(),
57+
pkg_name,
58+
author_name,
59+
path,
60+
max_size,
61+
max_file,
62+
);
63+
}
64+
} else {
65+
start_backstage_task(path, max_size, max_file);
66+
}
5667

5768
Ok(())
5869
}

0 commit comments

Comments
 (0)