Skip to content

Commit

Permalink
Use an option instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 6, 2024
1 parent 156564b commit a84c955
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 82 deletions.
12 changes: 7 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#### 🚀 Updates

- Added support for updating the `~/.prototools` file (root of user home directory).
- Added `--pin=user` to `proto install`.
- Added `--user` to `proto alias`, `unalias`, `pin`, `unpin`, `plugin add`, and `plugin remove`.
- Added `store` as an alias for `global`.
- Added `cwd` as an alias for `local`.
- Added `home` as an alias for `user`.
- Added `--pin=user` to `proto install`.
- Added `--to=global|local|user` to `proto alias`, `pin`, and `plugin add`.
- Added `--from=global|local|user` to `proto unalias`, `unpin`, and `plugin remove`.
- Added aliases for pin locations.
- `cwd` -> `local`
- `home` -> `user`
- `store` -> `global`
- Added new `settings.offline` settings that control how offline checks work.

#### ⚙️ Internal
Expand Down
8 changes: 5 additions & 3 deletions crates/cli/src/commands/alias.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::ProtoCliError;
use crate::helpers::{map_pin_type, PinOption};
use crate::session::ProtoSession;
use clap::Args;
use proto_core::{is_alias_name, Id, ProtoConfig, UnresolvedVersionSpec};
Expand All @@ -19,8 +20,8 @@ pub struct AliasArgs {
#[arg(long, group = "pin", help = "Add to the global ~/.proto/.prototools")]
global: bool,

#[arg(long, group = "pin", help = "Add to the user ~/.prototools")]
user: bool,
#[arg(long, group = "pin", help = "Location of .prototools to add to")]
to: Option<PinOption>,
}

#[tracing::instrument(skip_all)]
Expand All @@ -41,7 +42,8 @@ pub async fn alias(session: ProtoSession, args: AliasArgs) -> AppResult {
let tool = session.load_tool(&args.id).await?;

let config_path = ProtoConfig::update(
tool.proto.get_config_dir_from_flags(args.global, args.user),
tool.proto
.get_config_dir(map_pin_type(args.global, args.to)),
|config| {
let tool_configs = config.tools.get_or_insert(Default::default());

Expand Down
26 changes: 8 additions & 18 deletions crates/cli/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::helpers::*;
use crate::session::ProtoSession;
use crate::shell::{self, Export};
use crate::telemetry::{track_usage, Metric};
use clap::{Args, ValueEnum};
use clap::Args;
use indicatif::ProgressBar;
use miette::IntoDiagnostic;
use proto_core::flow::install::{InstallOptions, InstallPhase};
Expand All @@ -19,16 +19,6 @@ use tokio::task::JoinSet;
use tokio::time::sleep;
use tracing::{debug, instrument};

#[derive(Clone, Debug, ValueEnum)]
pub enum PinOption {
#[value(alias = "store")]
Global,
#[value(alias = "cwd")]
Local,
#[value(alias = "home")]
User,
}

#[derive(Args, Clone, Debug, Default)]
pub struct InstallArgs {
#[arg(help = "ID of a single tool to install")]
Expand Down Expand Up @@ -87,30 +77,30 @@ async fn pin_version(
) -> miette::Result<bool> {
let config = tool.proto.load_config()?;
let spec = tool.get_resolved_version().to_unresolved_spec();
let mut global = false;
let mut pin_type = PinType::Local;
let mut pin = false;

// via `--pin` arg
if let Some(pin_type) = arg_pin_type {
global = matches!(pin_type, PinType::Global);
if let Some(custom_type) = arg_pin_type {
pin_type = *custom_type;
pin = true;
}
// Or the first time being installed
else if !config.versions.contains_key(&tool.id) {
global = true;
pin_type = PinType::Global;
pin = true;
}

// via `pin-latest` setting
if initial_version.is_latest() {
if let Some(pin_type) = &config.settings.pin_latest {
global = matches!(pin_type, PinType::Global);
if let Some(custom_type) = &config.settings.pin_latest {
pin_type = *custom_type;
pin = true;
}
}

if pin {
internal_pin(tool, &spec, global, false, true).await?;
internal_pin(tool, &spec, pin_type, true).await?;
}

Ok(pin)
Expand Down
32 changes: 15 additions & 17 deletions crates/cli/src/commands/pin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::helpers::{map_pin_type, PinOption};
use crate::session::ProtoSession;
use clap::Args;
use proto_core::{Id, ProtoConfig, Tool, UnresolvedVersionSpec};
use proto_core::{Id, PinType, ProtoConfig, Tool, UnresolvedVersionSpec};
use starbase::AppResult;
use starbase_styles::color;
use std::collections::BTreeMap;
Expand All @@ -18,34 +19,30 @@ pub struct PinArgs {
#[arg(long, group = "pin", help = "Pin to the global ~/.proto/.prototools")]
pub global: bool,

#[arg(long, group = "pin", help = "Pin to the user ~/.prototools")]
pub user: bool,

#[arg(long, help = "Resolve the version before pinning")]
pub resolve: bool,

#[arg(long, group = "pin", help = "Location of .prototools to pin to")]
pub to: Option<PinOption>,
}

pub async fn internal_pin(
tool: &mut Tool,
spec: &UnresolvedVersionSpec,
global: bool,
user: bool,
pin: PinType,
link: bool,
) -> miette::Result<PathBuf> {
// Create symlink to this new version
if global && link {
if pin == PinType::Global && link {
tool.symlink_bins(true).await?;
}

let config_path = ProtoConfig::update(
tool.proto.get_config_dir_from_flags(global, user),
|config| {
config
.versions
.get_or_insert(BTreeMap::default())
.insert(tool.id.clone(), spec.clone());
},
)?;
let config_path = ProtoConfig::update(tool.proto.get_config_dir(pin), |config| {
config
.versions
.get_or_insert(BTreeMap::default())
.insert(tool.id.clone(), spec.clone());
})?;

debug!(
version = spec.to_string(),
Expand All @@ -67,7 +64,8 @@ pub async fn pin(session: ProtoSession, args: PinArgs) -> AppResult {
args.spec.clone()
};

let config_path = internal_pin(&mut tool, &spec, args.global, args.user, false).await?;
let config_path =
internal_pin(&mut tool, &spec, map_pin_type(args.global, args.to), false).await?;

println!(
"Pinned {} to {} in {}",
Expand Down
7 changes: 4 additions & 3 deletions crates/cli/src/commands/plugin/add.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::helpers::{map_pin_type, PinOption};
use crate::session::ProtoSession;
use clap::Args;
use proto_core::{Id, PluginLocator, ProtoConfig};
Expand All @@ -15,16 +16,16 @@ pub struct AddPluginArgs {
#[arg(long, group = "pin", help = "Add to the global ~/.proto/.prototools")]
global: bool,

#[arg(long, group = "pin", help = "Add to the user ~/.prototools")]
user: bool,
#[arg(long, group = "pin", help = "Location of .prototools to add to")]
to: Option<PinOption>,
}

#[tracing::instrument(skip_all)]
pub async fn add(session: ProtoSession, args: AddPluginArgs) -> AppResult {
let config_path = ProtoConfig::update(
session
.env
.get_config_dir_from_flags(args.global, args.user),
.get_config_dir(map_pin_type(args.global, args.to)),
|config| {
config
.plugins
Expand Down
31 changes: 14 additions & 17 deletions crates/cli/src/commands/plugin/remove.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::ProtoCliError;
use crate::helpers::{map_pin_type, PinOption};
use crate::session::ProtoSession;
use clap::Args;
use proto_core::{Id, ProtoConfig, PROTO_CONFIG_NAME};
Expand All @@ -17,30 +18,26 @@ pub struct RemovePluginArgs {
)]
global: bool,

#[arg(long, group = "pin", help = "Remove from the user ~/.prototools")]
user: bool,
#[arg(long, group = "pin", help = "Location of .prototools to remove from")]
from: Option<PinOption>,
}

#[tracing::instrument(skip_all)]
pub async fn remove(session: ProtoSession, args: RemovePluginArgs) -> AppResult {
if !args.global {
let config_path = session.env.cwd.join(PROTO_CONFIG_NAME);
let config_dir = session
.env
.get_config_dir(map_pin_type(args.global, args.from));
let config_path = config_dir.join(PROTO_CONFIG_NAME);

if !config_path.exists() {
return Err(ProtoCliError::MissingToolsConfigInCwd { path: config_path }.into());
}
if !config_path.exists() {
return Err(ProtoCliError::MissingToolsConfigInCwd { path: config_path }.into());
}

let config_path = ProtoConfig::update(
session
.env
.get_config_dir_from_flags(args.global, args.user),
|config| {
if let Some(plugins) = &mut config.plugins {
plugins.remove(&args.id);
}
},
)?;
let config_path = ProtoConfig::update(config_dir, |config| {
if let Some(plugins) = &mut config.plugins {
plugins.remove(&args.id);
}
})?;

println!(
"Removed plugin {} from config {}",
Expand Down
8 changes: 5 additions & 3 deletions crates/cli/src/commands/unalias.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::helpers::{map_pin_type, PinOption};
use crate::session::ProtoSession;
use clap::Args;
use proto_core::{Id, ProtoConfig};
Expand All @@ -20,8 +21,8 @@ pub struct UnaliasArgs {
)]
global: bool,

#[arg(long, group = "pin", help = "Remove from the user ~/.prototools")]
user: bool,
#[arg(long, group = "pin", help = "Location of .prototools to remove from")]
from: Option<PinOption>,
}

#[tracing::instrument(skip_all)]
Expand All @@ -30,7 +31,8 @@ pub async fn unalias(session: ProtoSession, args: UnaliasArgs) -> AppResult {
let mut value = None;

let config_path = ProtoConfig::update(
tool.proto.get_config_dir_from_flags(args.global, args.user),
tool.proto
.get_config_dir(map_pin_type(args.global, args.from)),
|config| {
if let Some(tool_configs) = &mut config.tools {
if let Some(tool_config) = tool_configs.get_mut(&tool.id) {
Expand Down
8 changes: 5 additions & 3 deletions crates/cli/src/commands/unpin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::helpers::{map_pin_type, PinOption};
use crate::session::ProtoSession;
use clap::Args;
use proto_core::{Id, ProtoConfig};
Expand All @@ -17,8 +18,8 @@ pub struct UnpinArgs {
)]
pub global: bool,

#[arg(long, group = "pin", help = "Unpin from the user ~/.prototools")]
pub user: bool,
#[arg(long, group = "pin", help = "Location of .prototools to unpin from")]
pub from: Option<PinOption>,
}

#[tracing::instrument(skip_all)]
Expand All @@ -27,7 +28,8 @@ pub async fn unpin(session: ProtoSession, args: UnpinArgs) -> AppResult {
let mut value = None;

let config_path = ProtoConfig::update(
tool.proto.get_config_dir_from_flags(args.global, args.user),
tool.proto
.get_config_dir(map_pin_type(args.global, args.from)),
|config| {
if let Some(versions) = &mut config.versions {
value = versions.remove(&tool.id);
Expand Down
29 changes: 29 additions & 0 deletions crates/cli/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
use clap::ValueEnum;
use dialoguer::{
console::{style, Style},
theme::ColorfulTheme,
};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use miette::IntoDiagnostic;
use proto_core::PinType;
use starbase_styles::color::{self, Color};
use starbase_utils::env::bool_var;
use std::{io::IsTerminal, time::Duration};
use tracing::debug;

#[derive(Clone, Copy, Debug, Default, ValueEnum)]
pub enum PinOption {
#[value(alias = "store")]
Global,
#[default]
#[value(alias = "cwd")]
Local,
#[value(alias = "home")]
User,
}

pub fn map_pin_type(global: bool, option: Option<PinOption>) -> PinType {
if let Some(option) = option {
return match option {
PinOption::Global => PinType::Global,
PinOption::Local => PinType::Local,
PinOption::User => PinType::User,
};
}

if global {
PinType::Global
} else {
PinType::Local
}
}

pub fn create_theme() -> ColorfulTheme {
ColorfulTheme {
defaults_style: Style::new().for_stderr().color256(Color::Pink as u8),
Expand Down
5 changes: 3 additions & 2 deletions crates/cli/tests/alias_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ mod alias_user {
.arg("node")
.arg("example")
.arg("19.0.0")
.arg("--user");
.arg("--to")
.arg("user");
})
.debug();
.success();

assert!(config_file.exists());

Expand Down
6 changes: 5 additions & 1 deletion crates/cli/tests/pin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ mod pin_user {

sandbox
.run_bin(|cmd| {
cmd.arg("pin").arg("node").arg("19.0.0").arg("--user");
cmd.arg("pin")
.arg("node")
.arg("19.0.0")
.arg("--to")
.arg("home");
})
.success();

Expand Down
Loading

0 comments on commit a84c955

Please sign in to comment.