use keyflux::{cli::Cli, KeyManager};
use keyflux::config::{ConfigHolder, KeyFluxConfig};
use log::{error, info, warn, trace};
use tokio;
use std::path::{Path, PathBuf};
use sodiumoxide;
use std::collections::HashMap;
use std::{env, io};
use std::fs;
use env_logger::WriteStyle;
use keyflux::config::env::EnvConfig;
use keyflux::_rust_i18n_translate;
use keyflux::cli::Commands;
use keyflux::flux::register_all_fluxes;
use clap::{Args, Command, CommandFactory, Parser, Subcommand, ValueHint};
use clap_complete::{generate, Generator, Shell};
#[macro_use]
extern crate rust_i18n;
use keyflux::key::Key;
async fn process_env_config(env_config: &EnvConfig, config_path: &PathBuf) {
trace!("{}", t!("trace.env_config", env_config = format!("{:?}", env_config)));
let manager = FormatManager::read().await;
let mut keys = KeyCollection::new();
match env_config {
EnvConfig::Variable { name, value } => {
info!("{}", t!("info.setting_env_var", name = name));
std::env::set_var(name, value);
}
EnvConfig::File(file) => {
let file_path = if file.is_absolute() {
file
} else {
&config_path.parent().unwrap().join(file)
};
trace!("{}: {}", "trace.loading_env_file", file_path.display());
trace!("{}", t!("trace.loading_env_file", file = file_path.display()));
let new_keys = manager.load_keys(&file_path, None).unwrap_or_else(|err| {
error!("{}: {}", t!("error.loading_env_file"), err.to_string());
std::process::exit(1);
});
for key in new_keys.iter() {
std::env::set_var(key.name(), key.value());
}
keys.merge(new_keys);
}
}
}
use keyflux::command::diff::diff;
use keyflux::command::sort::sort;
use keyflux::command::sync::sync;
use keyflux::command::convert::convert;
use keyflux::file::format_manager::FormatManager;
use keyflux::file::key_collection::KeyCollection;
use keyflux::key::KeyDetail;
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}
#[tokio::main]
async fn main() {
let mut cli = Cli::parse();
if let Some(generator) = cli.generator {
let mut cmd = Cli::command();
let name = cmd.get_name().to_string();
generate(generator, &mut cmd, &name, &mut std::io::stdout());
return;
}
rust_i18n::set_locale(cli.locale.clone().into());
register_all_fluxes();
env_logger::Builder::new()
.write_style(WriteStyle::Always)
.filter_level(cli.log_level.clone().into())
.init();
sodiumoxide::init().expect(&t!("error.sodiumoxide_init"));
trace!("{}", t!("trace.parsed_cli_args", cli = format!("{:?}", cli)));
trace!("{}", t!("trace.loading_config_file", file = format!("{:?}", cli.config_file)));
let config_manager = ConfigHolder::load_config(cli.config_file).unwrap_or_else(|err| {
error!("{}: {}", t!("error.loading_config_file"), err.to_string()); trace!("{}", t!("trace.exiting")); std::process::exit(1); });
if let Some(env_configs) = config_manager.config().env() {
for env_config in env_configs {
process_env_config(env_config, config_manager.path()).await;
}
}
trace!("{}", t!("trace.loaded_config"));
let mut manager = KeyManager::new(config_manager.config);
match cli.command {
None => {
eprintln!("No command specified.");
std::process::exit(1);
}
Some(command) => {
match command {
Commands::Sort { file } => {
if let Err(e) = sort(&file).await {
eprintln!("Error sorting file: {}", e);
std::process::exit(1);
}
}
Commands::Diff { file1, file2 } => {
if let Err(e) = diff(&file1, &file2).await {
eprintln!("Error diffing files: {}", e);
std::process::exit(1);
}
}
Commands::Sync => {
if let Err(e) = sync(&mut manager).await {
eprintln!("Error syncing environment: {}", e);
std::process::exit(1);
}
}
Commands::Convert { file, fmt } => {
if let Err(e) = convert(&file, &fmt).await {
eprintln!("Error converting file: {}", e);
std::process::exit(1);
}
}
Commands::Sub { input, output } => {
if let Err(e) = keyflux::command::sub(input, output) {
eprintln!("Error subbing files: {}", e);
std::process::exit(1);
}
}
}
}
}
}