mod caches;
mod clean;
mod demo;
mod fmt;
mod ports;
mod progress;
mod tui;
mod up;
use std::{env, path::PathBuf};
use anyhow::{Context, Result, anyhow, bail};
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "tidyup", version, about, long_about)]
struct Cli {
#[command(subcommand)]
command: Option<Command>,
}
#[derive(Subcommand)]
enum Command {
#[command(visible_alias = "ls")]
Ports {
#[arg(short, long)]
all: bool,
},
Kill {
#[arg(required = true)]
ports: Vec<u16>,
#[arg(short, long)]
force: bool,
},
Clean {
path: Option<PathBuf>,
#[arg(long, value_name = "DAYS")]
older_than: Option<u64>,
#[arg(short, long)]
caches: bool,
#[arg(long)]
trash: bool,
#[arg(short = 'n', long)]
dry_run: bool,
#[arg(short, long, conflicts_with = "dry_run")]
yes: bool,
},
Up {
services: Vec<String>,
#[arg(long, conflicts_with = "services")]
init: bool,
},
Ui {
path: Option<PathBuf>,
},
#[command(external_subcommand)]
Shorthand(Vec<String>),
}
fn main() -> Result<()> {
match Cli::parse().command {
None => tui::run(folder(None)?),
Some(Command::Ui { path }) => tui::run(folder(path)?),
Some(Command::Ports { all }) => ports::print(all),
Some(Command::Kill { ports, force }) => ports::kill(&ports, force),
Some(Command::Clean {
path,
older_than,
caches,
trash,
dry_run,
yes,
}) => clean::run(clean::Options {
root: folder(path)?,
older_than,
caches,
trash,
dry_run,
yes,
}),
Some(Command::Up { init: true, .. }) => up::init(),
Some(Command::Up { services, .. }) => up::run(&services),
Some(Command::Shorthand(args)) => {
let ports = args
.iter()
.map(|arg| arg.parse())
.collect::<Result<Vec<u16>, _>>()
.map_err(|_| anyhow!("unknown command '{}' (see `tidyup --help`)", args[0]))?;
ports::kill(&ports, false)
}
}
}
fn folder(path: Option<PathBuf>) -> Result<PathBuf> {
let path = match path {
Some(path) => path,
None => env::current_dir().context("could not read the current folder")?,
};
if !path.is_dir() {
bail!("{} is not a folder", path.display());
}
path.canonicalize()
.with_context(|| format!("could not open {}", path.display()))
}