use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use arcbox_constants::paths::HostLayout;
use clap::{Subcommand, ValueEnum};
use super::OutputFormat;
mod completions;
mod install;
mod profile;
mod status;
pub(super) use install::remove_integration;
pub(super) use status::{ComponentStatus, shell_integration_status};
pub(super) struct Integration {
pub(super) bin: PathBuf,
pub(super) shell: PathBuf,
pub(super) completions: PathBuf,
pub(super) profile: PathBuf,
pub(super) shell_kind: ShellKind,
pub(super) docker_config: PathBuf,
}
impl Integration {
pub(super) async fn from_env() -> Result<Self> {
let home = dirs::home_dir().context("could not determine home directory")?;
Self::under(
&home,
&arcbox_home(),
super::cli_plugins::default_docker_config_dir()?,
)
.await
}
pub(super) async fn under(
home: &Path,
data_dir: &Path,
docker_config: PathBuf,
) -> Result<Self> {
let shell_kind = profile::detect_shell();
Ok(Self {
bin: data_dir.join("bin"),
shell: data_dir.join("shell"),
completions: data_dir.join("completions"),
profile: profile::profile_path_under(shell_kind, home).await?,
shell_kind,
docker_config,
})
}
}
#[derive(Subcommand)]
pub enum SetupCommands {
Install,
Uninstall,
Status,
Completions(CompletionsArgs),
}
#[derive(clap::Args)]
pub struct CompletionsArgs {
#[arg(long, value_enum)]
pub shell: ShellKind,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum ShellKind {
Zsh,
Bash,
Fish,
}
impl ShellKind {
pub(super) const fn as_str(self) -> &'static str {
match self {
Self::Zsh => "zsh",
Self::Bash => "bash",
Self::Fish => "fish",
}
}
}
pub async fn execute(command: SetupCommands, format: OutputFormat) -> Result<()> {
match command {
SetupCommands::Install => install::install(format).await,
SetupCommands::Uninstall => install::uninstall(format).await,
SetupCommands::Status => status::status(format).await,
SetupCommands::Completions(args) => {
completions::print(args.shell);
Ok(())
}
}
}
fn arcbox_home() -> PathBuf {
HostLayout::from_env_or_default().data_dir
}
fn bin_dir() -> PathBuf {
arcbox_home().join("bin")
}
fn shell_dir() -> PathBuf {
arcbox_home().join("shell")
}
fn completions_dir() -> PathBuf {
arcbox_home().join("completions")
}