use std::env::current_dir;
use std::path::PathBuf;
use anyhow::{bail, Context};
use structopt::StructOpt;
use crate::home::Home;
use crate::manifest::Manifest;
use crate::tool_alias::ToolAlias;
use crate::tool_name::ToolName;
use crate::tool_spec::ToolSpec;
use crate::tool_storage::ToolStorage;
use crate::trust::{TrustCache, TrustMode};
#[derive(Debug, StructOpt)]
pub struct Args {
#[structopt(subcommand)]
pub subcommand: Subcommand,
}
impl Args {
pub fn run(self, home: &Home, tools: ToolStorage) -> anyhow::Result<()> {
match self.subcommand {
Subcommand::Init(sub) => sub.run(),
Subcommand::Add(sub) => sub.run(tools),
Subcommand::Install(sub) => sub.run(tools),
Subcommand::Trust(sub) => sub.run(home),
Subcommand::SelfInstall(sub) => sub.run(home, tools),
Subcommand::List(_) => bail!("This command is not yet implemented."),
Subcommand::Update(_) => bail!("This command is not yet implemented."),
Subcommand::SelfUpdate(_) => bail!("This command is not yet implemented."),
}
}
}
#[derive(Debug, StructOpt)]
pub enum Subcommand {
Init(InitSubcommand),
List(ListSubcommand),
Add(AddSubcommand),
Update(UpdateSubcommand),
Install(InstallSubcommand),
Trust(TrustSubcommand),
SelfUpdate(SelfUpdateSubcommand),
SelfInstall(SelfInstallSubcommand),
}
#[derive(Debug, StructOpt)]
pub struct InitSubcommand {
pub path: Option<PathBuf>,
}
impl InitSubcommand {
pub fn run(self) -> anyhow::Result<()> {
let path = match self.path {
Some(v) => v,
None => current_dir().context("Could not read current directory")?,
};
Manifest::init_local(&path)?;
Ok(())
}
}
#[derive(Debug, StructOpt)]
pub struct ListSubcommand {}
#[derive(Debug, StructOpt)]
pub struct AddSubcommand {
pub tool_spec: ToolSpec,
pub tool_alias: Option<ToolAlias>,
#[structopt(long)]
pub global: bool,
}
impl AddSubcommand {
pub fn run(self, tools: ToolStorage) -> anyhow::Result<()> {
tools.add(&self.tool_spec, self.tool_alias.as_ref(), self.global)
}
}
#[derive(Debug, StructOpt)]
pub struct UpdateSubcommand {
pub aliases_or_specs: Vec<String>,
#[structopt(long)]
pub global: bool,
#[structopt(long)]
pub latest: bool,
}
#[derive(Debug, StructOpt)]
pub struct InstallSubcommand {
#[structopt(long)]
pub no_trust_check: bool,
}
impl InstallSubcommand {
pub fn run(self, tools: ToolStorage) -> anyhow::Result<()> {
let trust = if self.no_trust_check {
TrustMode::NoCheck
} else {
TrustMode::Check
};
tools.install_all(trust)
}
}
#[derive(Debug, StructOpt)]
pub struct TrustSubcommand {
pub name: ToolName,
}
impl TrustSubcommand {
pub fn run(self, home: &Home) -> anyhow::Result<()> {
if TrustCache::add(home, self.name.clone())? {
log::info!("Added {} to the set of trusted tools.", self.name);
} else {
log::info!("{} was already a trusted tool.", self.name);
}
Ok(())
}
}
#[derive(Debug, StructOpt)]
pub struct SelfUpdateSubcommand {}
#[derive(Debug, StructOpt)]
pub struct SelfInstallSubcommand {}
impl SelfInstallSubcommand {
pub fn run(self, home: &Home, tools: ToolStorage) -> anyhow::Result<()> {
tools.update_links()?;
if crate::system_path::add(&home)? {
log::info!(
"Added ~/.aftman/bin to your PATH. Restart your terminal for this to take effect."
);
} else {
log::debug!("Did not modify PATH.");
}
Ok(())
}
}