Skip to content

Commit

Permalink
breaking: Add proto pin command. (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 29, 2023
1 parent 2028dc0 commit 358141a
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 187 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@

## Unreleased

#### 💥 Breaking

- Removed `proto global`, use `proto pin --global` instead.
- Removed `proto local`, use `proto pin` instead.

#### 🚀 Updates

- Added a `proto pin` command, which is a merge of the old `proto global` and `proto local` commands.
- Updated `proto install` to auto-clean stale plugins after a successful installation.

## 0.18.5
Expand Down
23 changes: 8 additions & 15 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::commands::{
AddPluginArgs, AliasArgs, BinArgs, CleanArgs, CompletionsArgs, GlobalArgs, InstallArgs,
InstallGlobalArgs, ListArgs, ListGlobalArgs, ListRemoteArgs, LocalArgs, PluginsArgs,
RemovePluginArgs, RunArgs, SetupArgs, ToolsArgs, UnaliasArgs, UninstallArgs,
UninstallGlobalArgs,
AddPluginArgs, AliasArgs, BinArgs, CleanArgs, CompletionsArgs, InstallArgs, InstallGlobalArgs,
ListArgs, ListGlobalArgs, ListRemoteArgs, PinArgs, PluginsArgs, RemovePluginArgs, RunArgs,
SetupArgs, ToolsArgs, UnaliasArgs, UninstallArgs, UninstallGlobalArgs,
};
use clap::builder::styling::{Color, Style, Styles};
use clap::{Parser, Subcommand, ValueEnum};
Expand Down Expand Up @@ -132,13 +131,6 @@ pub enum Commands {
)]
InstallGlobal(InstallGlobalArgs),

#[command(
name = "global",
about = "Set the global default version of a tool.",
long_about = "Set the global default version of a tool. This will pin the version in the ~/.proto/tools installation directory."
)]
Global(GlobalArgs),

#[command(
alias = "ls",
name = "list",
Expand All @@ -164,11 +156,12 @@ pub enum Commands {
ListRemote(ListRemoteArgs),

#[command(
name = "local",
about = "Set the local version of a tool.",
long_about = "Set the local version of a tool. This will create a .prototools file (if it does not exist)\nin the current working directory with the appropriate tool and version."
alias = "p",
name = "pin",
about = "Pin a default global or local version of a tool.",
long_about = "Pin a default version of a tool globally to ~/.proto/tools, or locally to .prototools (in the current working directory)."
)]
Local(LocalArgs),
Pin(PinArgs),

#[command(name = "plugins", about = "List all active and configured plugins.")]
Plugins(PluginsArgs),
Expand Down
34 changes: 0 additions & 34 deletions crates/cli/src/commands/global.rs

This file was deleted.

37 changes: 0 additions & 37 deletions crates/cli/src/commands/local.rs

This file was deleted.

6 changes: 2 additions & 4 deletions crates/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ mod alias;
mod bin;
mod clean;
mod completions;
mod global;
mod install;
mod install_all;
mod install_global;
mod list;
mod list_global;
mod list_remote;
mod local;
mod pin;
mod plugins;
mod remove_plugin;
mod run;
Expand All @@ -26,14 +25,13 @@ pub use alias::*;
pub use bin::*;
pub use clean::*;
pub use completions::*;
pub use global::*;
pub use install::*;
pub use install_all::*;
pub use install_global::*;
pub use list::*;
pub use list_global::*;
pub use list_remote::*;
pub use local::*;
pub use pin::*;
pub use plugins::*;
pub use remove_plugin::*;
pub use run::*;
Expand Down
63 changes: 63 additions & 0 deletions crates/cli/src/commands/pin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::env;
use std::path::PathBuf;

use clap::Args;
use proto_core::{load_tool, Id, ToolsConfig, UnresolvedVersionSpec};
use starbase::system;
use starbase_styles::color;
use tracing::{debug, info};

#[derive(Args, Clone, Debug)]
pub struct PinArgs {
#[arg(required = true, help = "ID of tool")]
id: Id,

#[arg(required = true, help = "Version or alias of tool")]
spec: UnresolvedVersionSpec,

#[arg(
long,
help = "Add to the global user config instead of local .prototools"
)]
global: bool,
}

#[system]
pub async fn pin(args: ArgsRef<PinArgs>) -> SystemResult {
let mut tool = load_tool(&args.id).await?;

if args.global {
tool.manifest.default_version = Some(args.spec.clone());
tool.manifest.save()?;

debug!(
version = args.spec.to_string(),
manifest = ?tool.manifest.path,
"Wrote the global version",
);

info!(
"Set the global {} version to {}",
tool.get_name(),
color::hash(args.spec.to_string())
);
} else {
let local_path = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

let mut config = ToolsConfig::load_from(local_path)?;
config.tools.insert(args.id.clone(), args.spec.clone());
config.save()?;

debug!(
version = args.spec.to_string(),
config = ?config.path,
"Wrote the local version",
);

info!(
"Set the local {} version to {}",
tool.get_name(),
color::hash(args.spec.to_string())
);
}
}
3 changes: 1 addition & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ async fn main() -> MainResult {
Commands::Completions(args) => app.execute_with_args(commands::completions, args),
Commands::Install(args) => app.execute_with_args(commands::install, args),
Commands::InstallGlobal(args) => app.execute_with_args(commands::install_global, args),
Commands::Global(args) => app.execute_with_args(commands::global, args),
Commands::List(args) => app.execute_with_args(commands::list, args),
Commands::ListGlobal(args) => app.execute_with_args(commands::list_global, args),
Commands::ListRemote(args) => app.execute_with_args(commands::list_remote, args),
Commands::Local(args) => app.execute_with_args(commands::local, args),
Commands::Pin(args) => app.execute_with_args(commands::pin, args),
Commands::Plugins(args) => app.execute_with_args(commands::plugins, args),
Commands::RemovePlugin(args) => app.execute_with_args(commands::remove_plugin, args),
Commands::Run(args) => app.execute_with_args(commands::run, args),
Expand Down
76 changes: 0 additions & 76 deletions crates/cli/tests/global_test.rs

This file was deleted.

Loading

0 comments on commit 358141a

Please sign in to comment.