mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-24 08:53:04 +10:00
feat: Storing configs
This commit is contained in:
+14
-76
@@ -1,36 +1,31 @@
|
||||
use crate::config::configure::Configurable;
|
||||
use crate::{
|
||||
cli::{Cli, Commands},
|
||||
commands::{configure::interactive_configure, upload},
|
||||
config::{
|
||||
config::{Config, ConfigOption},
|
||||
configurable::Configurable,
|
||||
s3::S3Config,
|
||||
},
|
||||
commands::upload,
|
||||
config::config::Config,
|
||||
};
|
||||
use clap::Parser;
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
use log::LevelFilter;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
mod cli;
|
||||
mod commands;
|
||||
mod config;
|
||||
mod logging;
|
||||
mod manifest;
|
||||
|
||||
#[macro_use]
|
||||
pub mod interactive;
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
configure_logging()?;
|
||||
crate::logging::configure_logging()?;
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
let mut config = Config::read();
|
||||
match &cli.command {
|
||||
Commands::Configure(options) => {
|
||||
configure_command(&mut config, options).await?;
|
||||
}
|
||||
Commands::Configure { name, option } => match option {
|
||||
config::config::ConfigOptionCli::Server(server_config) => todo!(),
|
||||
config::config::ConfigOptionCli::S3(s3_config_cli) => config.add_item(
|
||||
name.clone(),
|
||||
config::config::ConfigOption::S3(s3_config_cli.clone().configure()),
|
||||
),
|
||||
},
|
||||
Commands::Upload(info) => {
|
||||
upload::interface::upload(info, config).await?;
|
||||
}
|
||||
@@ -38,60 +33,3 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn configure_command(config: &mut Config, options: &ConfigOption) -> anyhow::Result<()> {
|
||||
let configuration: Box<dyn Configurable> = match options {
|
||||
ConfigOption::Server(options) => Box::new(options.clone()),
|
||||
ConfigOption::S3(options) => Box::new(S3Config::from(options.clone())),
|
||||
};
|
||||
configuration.configure(config);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn configure_logging() -> anyhow::Result<()> {
|
||||
let log_level = env::var("RUST_LOG")
|
||||
.unwrap_or_else(|_| "info".to_string())
|
||||
.parse::<LevelFilter>()?;
|
||||
|
||||
let log_dir = env::var("LOG_FILE_DIR").unwrap_or_else(|_| "logs".to_string());
|
||||
|
||||
fs::create_dir_all(&log_dir)?;
|
||||
|
||||
let colors = ColoredLevelConfig::new()
|
||||
.error(Color::Red)
|
||||
.warn(Color::Yellow)
|
||||
.info(Color::Blue)
|
||||
.debug(Color::Green)
|
||||
.trace(Color::Magenta);
|
||||
|
||||
fern::Dispatch::new()
|
||||
.chain(
|
||||
fern::Dispatch::new()
|
||||
.format(move |out, message, record| {
|
||||
out.finish(format_args!(
|
||||
"[{}] {}: {}",
|
||||
chrono::Local::now().format("%H:%M:%S%.3f"),
|
||||
colors.color(record.level()),
|
||||
message
|
||||
))
|
||||
})
|
||||
.chain(io::stdout()),
|
||||
)
|
||||
.chain(
|
||||
fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
out.finish(format_args!(
|
||||
"[{}] {} {} - {}",
|
||||
chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f"),
|
||||
record.level(),
|
||||
record.target(),
|
||||
message
|
||||
))
|
||||
})
|
||||
.chain(fern::log_file(format!("{}/app.log", log_dir))?),
|
||||
)
|
||||
.level(log_level)
|
||||
.apply()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user