/ manager / src / main.rs
main.rs
  1  #[macro_use]
  2  extern crate log;
  3  
  4  mod builder;
  5  mod error;
  6  mod linker;
  7  mod peeker;
  8  
  9  use builder::build_tree;
 10  use error::Errors;
 11  use linker::link_tree;
 12  use log::LevelFilter;
 13  use peeker::peek_tree;
 14  use std::env;
 15  use std::path::PathBuf;
 16  use structopt::StructOpt;
 17  
 18  #[derive(StructOpt)]
 19  struct Opt {
 20      #[structopt(short, long)]
 21      template_dir: Option<PathBuf>,
 22  
 23      #[structopt(short, long)]
 24      build_dir: Option<PathBuf>,
 25  
 26      #[structopt(short, long)]
 27      link_dir: Option<PathBuf>,
 28  
 29      #[structopt(long = "variables")]
 30      variables_path: Option<PathBuf>,
 31  
 32      #[structopt(short, long)]
 33      print_variables: bool,
 34  
 35      #[structopt(short, parse(from_occurrences))]
 36      verbosity: u8,
 37  
 38      flags: Vec<String>,
 39  }
 40  
 41  #[derive(Debug)]
 42  pub struct Config {
 43      template_dir: PathBuf,
 44      build_dir: PathBuf,
 45      link_dir: PathBuf,
 46      variables_path: PathBuf,
 47      flags: Vec<String>,
 48  }
 49  
 50  #[tokio::main]
 51  async fn main() {
 52      match run().await {
 53          Ok(_) => {}
 54          Err(errors) => errors.log(),
 55      }
 56  }
 57  
 58  async fn run() -> Result<(), Errors> {
 59      let opt = Opt::from_args();
 60  
 61      let filter_level = match opt.verbosity {
 62          0 => LevelFilter::Warn,
 63          1 => LevelFilter::Info,
 64          2 => LevelFilter::Debug,
 65          _ => LevelFilter::Trace,
 66      };
 67  
 68      pretty_env_logger::formatted_builder()
 69          .filter_level(filter_level)
 70          .init();
 71  
 72      let xdg_dirs = xdg::BaseDirectories::with_prefix("dotfiles").unwrap();
 73  
 74      let cfg = Config {
 75          template_dir: opt
 76              .template_dir
 77              .unwrap_or_else(|| xdg_dirs.create_config_directory("tree").expect("xdg")),
 78          build_dir: opt
 79              .build_dir
 80              .unwrap_or_else(|| xdg_dirs.create_cache_directory("").expect("xdg")),
 81          link_dir: opt
 82              .link_dir
 83              .unwrap_or_else(|| env::var("HOME").expect("$HOME").into()),
 84          variables_path: opt
 85              .variables_path
 86              .unwrap_or_else(|| xdg_dirs.get_config_file("variables.toml")),
 87          flags: opt.flags,
 88      };
 89  
 90      if opt.print_variables {
 91          info!("peeking tree");
 92          peek_tree(&cfg).await?;
 93      } else {
 94          info!("building tree");
 95          build_tree(&cfg).await?;
 96  
 97          info!("linking tree");
 98          link_tree(&cfg).await?;
 99      }
100  
101      Ok(())
102  }