use camino::Utf8PathBuf;
use clap::Parser;
use color_eyre::{eyre::WrapErr, Result};
use include_graph::dependencies::configfile::build_graph;
use tokio::{
fs::File,
io::{self},
};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
#[derive(Parser, Debug)]
#[command(version)]
struct Args {
#[arg(short, long)]
config: Utf8PathBuf,
#[arg(short, long)]
output: Option<Utf8PathBuf>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing::subscriber::set_global_default(
FmtSubscriber::builder()
.with_env_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::WARN.into())
.from_env_lossy(),
)
.finish(),
)
.unwrap();
color_eyre::install()?;
let args = Args::parse();
let data = tokio::fs::read_to_string(&args.config)
.await
.wrap_err_with(|| format!("Failed to open {:?}", &args.config))?;
let graph = build_graph(&data).await?;
match args.output {
Some(path) => {
graph
.write_dot(
File::create(&path)
.await
.wrap_err_with(|| format!("Failed to create {:?}", path))?,
)
.await
.wrap_err_with(|| format!("Failed to write into {:?}", path))?;
}
None => {
graph
.write_dot(io::stdout())
.await
.wrap_err("Failed to write to stdout")?;
}
};
Ok(())
}