1pub mod cmds;2pub mod command;3pub mod host;4pub mod keys;56mod fleetdata;78use std::ffi::OsString;9use std::io;1011use anyhow::{anyhow, bail, Result};12use clap::Parser;1314use cmds::{build_systems::BuildSystems, info::Info, secrets::Secrets};15use host::{Config, FleetOpts};16use tokio::fs;17use tokio::process::Command;18use tracing::{info, metadata::LevelFilter};19use tracing_subscriber::EnvFilter;2021#[derive(Parser)]22struct Prefetch {}23impl Prefetch {24 async fn run(&self, config: &Config) -> Result<()> {25 let mut prefetch_dir = config.directory.to_path_buf();26 prefetch_dir.push("prefetch");27 if !prefetch_dir.is_dir() {28 info!("nothing to prefetch: no prefetch directory");29 return Ok(());30 }31 for entry in std::fs::read_dir(&prefetch_dir)? {32 let entry = entry?;33 if !entry.metadata()?.is_file() {34 bail!("only files should exist in prefetch directory");35 }36 info!("prefetching {:?}", entry.file_name());37 let mut path = OsString::new();38 path.push("file://");39 path.push(entry.path());40 let status = Command::new("nix-prefetch-url").arg(path).status().await?;41 if !status.success() {42 bail!("failed with {status}");43 }44 }45 Ok(())46 }47}4849#[derive(Parser)]50enum Opts {51 52 BuildSystems(BuildSystems),53 54 #[clap(subcommand)]55 Secrets(Secrets),56 57 Prefetch(Prefetch),58 59 Info(Info),60}6162#[derive(Parser)]63#[clap(version = "1.0", author)]64struct RootOpts {65 #[clap(flatten)]66 fleet_opts: FleetOpts,67 #[clap(subcommand)]68 command: Opts,69}7071async fn run_command(config: &Config, command: Opts) -> Result<()> {72 match command {73 Opts::BuildSystems(c) => c.run(config).await?,74 Opts::Secrets(s) => s.run(config).await?,75 Opts::Info(i) => i.run(config).await?,76 Opts::Prefetch(p) => p.run(config).await?,77 };78 Ok(())79}8081#[tokio::main]82async fn main() -> Result<()> {83 let filter = EnvFilter::from_default_env().add_directive(LevelFilter::INFO.into());84 tracing_subscriber::FmtSubscriber::builder()85 .with_env_filter(filter)86 .without_time()87 .with_target(false)88 .with_writer(|| {89 90 io::stderr()91 })92 .try_init()93 .map_err(|e| anyhow!("Failed to initialize logger: {}", e))?;9495 info!("Starting");96 let mut os_args = std::env::args_os();97 let opts = RootOpts::parse_from((&mut os_args).take_while(|v| v != "--"));98 let config = opts.fleet_opts.build(os_args.collect()).await?;99100 match run_command(&config, opts.command).await {101 Ok(()) => {102 config.save()?;103 Ok(())104 }105 Err(e) => {106 let _ = config.save();107 Err(e)108 }109 }110}