Skip to content

Commit

Permalink
Start work on rewriting add subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 23, 2024
1 parent f4f7196 commit 6af0c2e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use anyhow::Result;
use clap::Parser;

use aftman::{storage::Home, tool::ToolAlias};

use crate::util::ToolIdOrSpec;

/// Adds a new tool to Aftman and installs it.
#[derive(Debug, Parser)]
pub struct AddSubcommand {
/// A tool identifier or specification describing where
/// to get the tool and what version to install.
pub tool_spec: ToolIdOrSpec,

/// The name that will be used to run the tool.
pub tool_alias: Option<ToolAlias>,

/// Install this tool globally by adding it to ~/.aftman/aftman.toml
/// instead of installing it to the nearest aftman.toml file.
#[clap(long)]
pub global: bool,
}

impl AddSubcommand {
pub async fn run(&self, home: &Home) -> Result<()> {
// TODO: Implement the add subcommand

Ok(())
}
}
4 changes: 4 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use anyhow::{Context, Result};
use clap::Parser;
use tokio::time::Instant;

mod add;
mod debug_system_info;
mod debug_trusted_tools;
mod list;
mod trust;
mod untrust;

use self::add::AddSubcommand;
use self::debug_system_info::DebugSystemInfoSubcommand;
use self::debug_trusted_tools::DebugTrustedToolsSubcommand;
use self::list::ListSubcommand;
Expand Down Expand Up @@ -70,6 +72,7 @@ pub enum Subcommand {
#[clap(hide = true)]
DebugTrustedTools(DebugTrustedToolsSubcommand),
// Public subcommands
Add(AddSubcommand),
List(ListSubcommand),
Trust(TrustSubcommand),
Untrust(UntrustSubcommand),
Expand All @@ -82,6 +85,7 @@ impl Subcommand {
Self::DebugSystemInfo(cmd) => cmd.run(home).await,
Self::DebugTrustedTools(cmd) => cmd.run(home).await,
// Public subcommands
Self::Add(cmd) => cmd.run(home).await,
Self::List(cmd) => cmd.run(home).await,
Self::Trust(cmd) => cmd.run(home).await,
Self::Untrust(cmd) => cmd.run(home).await,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use tracing::{error, level_filters::LevelFilter};
use tracing_subscriber::EnvFilter;

mod cli;
mod util;
use cli::Cli;

#[tokio::main]
Expand Down
40 changes: 40 additions & 0 deletions src/util/id_or_spec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::str::FromStr;

use serde_with::DeserializeFromStr;

use aftman::tool::{ToolId, ToolSpec, ToolSpecParseError};

/**
A tool identifier *or* specification, which includes
the author, name, and *maybe* a version of a tool.
See [`ToolId`] and [`ToolSpec`] for more information.
*/
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, DeserializeFromStr)]
pub enum ToolIdOrSpec {
Id(ToolId),
Spec(ToolSpec),
}

impl FromStr for ToolIdOrSpec {
type Err = ToolSpecParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.contains('@') {
Ok(Self::Spec(s.parse()?))
} else {
Ok(Self::Id(s.parse()?))
}
}
}

impl From<ToolId> for ToolIdOrSpec {
fn from(id: ToolId) -> Self {
Self::Id(id)
}
}

impl From<ToolSpec> for ToolIdOrSpec {
fn from(spec: ToolSpec) -> Self {
Self::Spec(spec)
}
}
3 changes: 3 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod id_or_spec;

pub use self::id_or_spec::ToolIdOrSpec;

0 comments on commit 6af0c2e

Please sign in to comment.