Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: Standardize configuration. #306

Merged
merged 34 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bbfb8ee
Add config.
milesj Nov 21, 2023
812a2d2
Cache user config.
milesj Nov 21, 2023
9a87848
Move instrument.
milesj Nov 22, 2023
9c5a6db
Update aliases.
milesj Nov 22, 2023
343d050
Update pin/unalias.
milesj Nov 28, 2023
9d6f2a8
Update detection.
milesj Nov 28, 2023
6804d6d
Update resolver.
milesj Nov 28, 2023
dd2bd21
Fixes.
milesj Nov 29, 2023
b5c5b06
Add new config.
milesj Nov 29, 2023
425871a
Start on new config.
milesj Nov 30, 2023
eae503d
Start on manager.
milesj Nov 30, 2023
6be0dae
Update loader.
milesj Nov 30, 2023
37488a1
Merge files.
milesj Nov 30, 2023
538a960
Update detector.
milesj Nov 30, 2023
68b7e6f
Update pin and install.
milesj Nov 30, 2023
5ad8cb3
Update add/remove.
milesj Nov 30, 2023
3e63d9e
Update alias/unalias.
milesj Nov 30, 2023
1c77c3e
Update outdated.
milesj Nov 30, 2023
a637829
Polish.
milesj Nov 30, 2023
e3f00bc
Update flags.
milesj Nov 30, 2023
634eac9
Updated based on rebase.
milesj Dec 1, 2023
338aa27
Use a resource.
milesj Dec 1, 2023
21d7084
Remove loader.
milesj Dec 1, 2023
16144bd
Update migrator.
milesj Dec 2, 2023
bfcb255
Update core tests.
milesj Dec 2, 2023
8b3a88a
Update alias tests.
milesj Dec 2, 2023
e7a284d
Update sandbox.
milesj Dec 4, 2023
c385b20
Update tool tests.
milesj Dec 4, 2023
752d8e0
Update more tests.
milesj Dec 4, 2023
56f8573
Update manifest logic.
milesj Dec 4, 2023
dda7656
Update more tests.
milesj Dec 4, 2023
51ca0cb
Polish.
milesj Dec 4, 2023
cdaca1d
Fix tests.
milesj Dec 4, 2023
9c28745
Fixes.
milesj Dec 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update pin/unalias.
milesj committed Dec 4, 2023
commit 343d050ae8323d461636b1f57f7ae02222cea87c
4 changes: 4 additions & 0 deletions crates/cli/src/commands/migrate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod v0_20;
mod v0_24;

use crate::error::ProtoCliError;
use clap::Args;
@@ -16,6 +17,9 @@ pub async fn migrate(args: ArgsRef<MigrateArgs>) {
"v0.20" => {
v0_20::migrate().await?;
}
"v0.24" => {
v0_24::migrate().await?;
}
unknown => {
return Err(ProtoCliError::UnknownMigration {
op: unknown.to_owned(),
2 changes: 2 additions & 0 deletions crates/cli/src/commands/migrate/v0_20.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use crate::helpers::load_configured_tools;
use crate::shell;
use proto_core::get_bin_dir;
54 changes: 54 additions & 0 deletions crates/cli/src/commands/migrate/v0_24.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![allow(deprecated)]

use crate::helpers::load_configured_tools;
use proto_core::UserConfig;
use starbase::SystemResult;
use starbase_styles::color;
use std::mem;
use tracing::{debug, info};

pub async fn migrate() -> SystemResult {
info!("Loading tools...");

let tools = load_configured_tools().await?;
let mut user_config = UserConfig::load()?;
let mut updated_user_config = false;

info!("Migrating configs...");

for tool in tools {
debug!("Checking {}", color::id(&tool.id));

let mut manifest = tool.manifest.clone();
let mut updated_manifest = false;

if !manifest.aliases.is_empty() {
debug!("Found aliases");

let entry = user_config.tools.entry(tool.id.clone()).or_default();
entry.aliases.extend(mem::take(&mut manifest.aliases));
updated_manifest = true;
}

if !tool.manifest.default_version.is_some() {
debug!("Found a default version");

let entry = user_config.tools.entry(tool.id.clone()).or_default();
entry.default_version = manifest.default_version.take();
updated_manifest = true;
}

if updated_manifest {
updated_user_config = true;
manifest.save()?;
}
}

if updated_user_config {
user_config.save()?;
}

info!("Migration complete!");

Ok(())
}
22 changes: 18 additions & 4 deletions crates/cli/src/commands/pin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::Args;
use proto_core::{load_tool, Id, Tool, ToolsConfig, UnresolvedVersionSpec};
use proto_core::{
load_tool, Id, Tool, ToolsConfig, UnresolvedVersionSpec, UserConfig, UserToolConfig,
};
use starbase::{system, SystemResult};
use starbase_styles::color;
use tracing::{debug, info};
@@ -21,12 +23,24 @@ pub struct PinArgs {

pub async fn internal_pin(tool: &mut Tool, args: &PinArgs, link: bool) -> SystemResult {
if args.global {
tool.manifest.default_version = Some(args.spec.clone());
tool.manifest.save()?;
let mut user_config = UserConfig::load()?;

user_config
.tools
.entry(tool.id.clone())
.and_modify(|cfg| {
cfg.default_version = Some(args.spec.clone());
})
.or_insert_with(|| UserToolConfig {
default_version: Some(args.spec.clone()),
..UserToolConfig::default()
});

user_config.save()?;

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

29 changes: 24 additions & 5 deletions crates/cli/src/commands/tool/list.rs
Original file line number Diff line number Diff line change
@@ -4,13 +4,20 @@ use crate::printer::Printer;
use chrono::{DateTime, NaiveDateTime};
use clap::Args;
use miette::IntoDiagnostic;
use proto_core::Id;
use proto_core::{Id, ToolManifest, UserConfig, UserToolConfig};
use serde::Serialize;
use starbase::system;
use starbase_styles::color;
use starbase_utils::json;
use std::collections::{HashMap, HashSet};
use tracing::info;

#[derive(Serialize)]
pub struct ToolItem {
manifest: ToolManifest,
user_config: Option<UserToolConfig>,
}

#[derive(Args, Clone, Debug)]
pub struct ListToolsArgs {
#[arg(help = "ID of tools to list")]
@@ -27,6 +34,7 @@ pub async fn list(args: ArgsRef<ListToolsArgs>) {
}

let tools = load_configured_tools_with_filters(HashSet::from_iter(&args.ids)).await?;
let mut user_config = UserConfig::load()?;

let mut tools = tools
.into_iter()
@@ -42,7 +50,17 @@ pub async fn list(args: ArgsRef<ListToolsArgs>) {
if args.json {
let items = tools
.into_iter()
.map(|t| (t.id, t.manifest))
.map(|t| {
let user_config = user_config.tools.remove(&t.id);

return (
t.id,
ToolItem {
manifest: t.manifest,
user_config,
},
);
})
.collect::<HashMap<_, _>>();

println!("{}", json::to_string_pretty(&items).into_diagnostic()?);
@@ -53,6 +71,8 @@ pub async fn list(args: ArgsRef<ListToolsArgs>) {
let mut printer = Printer::new();

for tool in tools {
let user_tool_config = user_config.tools.remove(&tool.id).unwrap_or_default();

printer.line();
printer.header(&tool.id, &tool.metadata.name);

@@ -61,7 +81,7 @@ pub async fn list(args: ArgsRef<ListToolsArgs>) {

p.entry_map(
"Aliases",
tool.manifest
user_tool_config
.aliases
.iter()
.map(|(k, v)| (color::hash(v.to_string()), color::label(k)))
@@ -92,8 +112,7 @@ pub async fn list(args: ArgsRef<ListToolsArgs>) {
}
}

if tool
.manifest
if user_tool_config
.default_version
.as_ref()
.is_some_and(|dv| *dv == version.to_unresolved_spec())
40 changes: 22 additions & 18 deletions crates/cli/src/commands/unalias.rs
Original file line number Diff line number Diff line change
@@ -18,23 +18,27 @@ pub async fn unalias(args: ArgsRef<UnaliasArgs>) {
let tool = load_tool(&args.id).await?;
let mut user_config = UserConfig::load()?;

let tool_config = user_config.tools.entry(args.id.clone()).or_default();
let value = tool_config.aliases.remove(&args.alias);

user_config.save()?;

if let Some(version) = value {
info!(
"Removed alias {} ({}) from {}",
color::id(&args.alias),
color::muted_light(version.to_string()),
tool.get_name(),
);
} else {
info!(
"Alias {} not found for {}",
color::id(&args.alias),
tool.get_name(),
);
if user_config.tools.contains_key(&args.id) {
let tool_config = user_config.tools.entry(args.id.clone()).or_default();
let value = tool_config.aliases.remove(&args.alias);

user_config.save()?;

if let Some(version) = value {
info!(
"Removed alias {} ({}) from {}",
color::id(&args.alias),
color::muted_light(version.to_string()),
tool.get_name(),
);

return Ok(());
}
}

info!(
"Alias {} not found for {}",
color::id(&args.alias),
tool.get_name(),
);
}
3 changes: 1 addition & 2 deletions crates/cli/tests/list_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utils;

use proto_core::{ToolManifest, UnresolvedVersionSpec, VersionSpec};
use proto_core::{ToolManifest, VersionSpec};
use utils::*;

mod list {
@@ -12,7 +12,6 @@ mod list {

let mut manifest =
ToolManifest::load(temp.path().join("tools/node/manifest.json")).unwrap();
manifest.default_version = Some(UnresolvedVersionSpec::parse("19.0.0").unwrap());
manifest
.installed_versions
.insert(VersionSpec::parse("19.0.0").unwrap());
Loading