Skip to content

Commit

Permalink
Update API.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 29, 2023
1 parent 37e79ea commit 1ddd96a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
6 changes: 1 addition & 5 deletions crates/cli/src/commands/add_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use clap::Args;
use proto_core::{Id, PluginLocator, ToolsConfig, UserConfig};
use starbase::system;
use starbase_styles::color;
use std::env;
use std::path::PathBuf;
use tracing::info;

#[derive(Args, Clone, Debug)]
Expand Down Expand Up @@ -39,9 +37,7 @@ pub async fn add_plugin(args: ArgsRef<AddPluginArgs>) {
return Ok(());
}

let local_path = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

let mut config = ToolsConfig::load_from(local_path)?;
let mut config = ToolsConfig::load()?;
config.plugins.insert(args.id.clone(), args.plugin.clone());
config.save()?;

Expand Down
17 changes: 14 additions & 3 deletions crates/cli/src/commands/outdated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct OutdatedArgs {
help = "Check for latest version available ignoring requirements and ranges"
)]
latest: bool,

#[arg(long, help = "Update the versions in the local configuration")]
update: bool,
}

#[derive(Serialize)]
Expand All @@ -31,7 +34,7 @@ pub struct OutdatedItem {

#[system]
pub async fn outdated(args: ArgsRef<OutdatedArgs>) {
let tools_config = ToolsConfig::load_upwards()?;
let mut tools_config = ToolsConfig::load()?;
let initial_version = UnresolvedVersionSpec::default(); // latest

if tools_config.tools.is_empty() {
Expand All @@ -44,6 +47,7 @@ pub async fn outdated(args: ArgsRef<OutdatedArgs>) {
}

let mut items = HashMap::new();
let mut tool_versions = HashMap::new();

for (tool_id, config_version) in &tools_config.tools {
let mut tool = load_tool(tool_id).await?;
Expand Down Expand Up @@ -87,6 +91,10 @@ pub async fn outdated(args: ArgsRef<OutdatedArgs>) {
comments.push(color::success("update available!"));
}

if args.update {
tool_versions.insert(tool.id.clone(), newer_version.to_unresolved_spec());
}

if args.json {
items.insert(
tool.id,
Expand All @@ -107,9 +115,12 @@ pub async fn outdated(args: ArgsRef<OutdatedArgs>) {
}
}

if args.update {
tools_config.tools.extend(tool_versions);
tools_config.save()?;
}

if args.json {
println!("{}", json::to_string_pretty(&items).into_diagnostic()?);

return Ok(());
}
}
7 changes: 1 addition & 6 deletions crates/cli/src/commands/pin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::env;
use std::path::PathBuf;

use clap::Args;
use proto_core::{load_tool, Id, Tool, ToolsConfig, UnresolvedVersionSpec};
use starbase::{system, SystemResult};
Expand Down Expand Up @@ -33,9 +30,7 @@ pub fn internal_pin(tool: &mut Tool, args: &PinArgs) -> SystemResult {
"Wrote the global version",
);
} else {
let local_path = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

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

Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/add_plugin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod add_plugin {

assert!(config_file.exists());

let manifest = ToolsConfig::load(config_file).unwrap();
let manifest = ToolsConfig::load_from(sandbox.path()).unwrap();

assert_eq!(
manifest.plugins,
Expand Down
22 changes: 11 additions & 11 deletions crates/core/src/tools_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,25 @@ impl ToolsConfig {
}
}

pub fn load_from<P: AsRef<Path>>(dir: P) -> miette::Result<Self> {
Self::load(dir.as_ref().join(TOOLS_CONFIG_NAME))
#[tracing::instrument(skip_all)]
pub fn load() -> miette::Result<Self> {
let working_dir = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

Self::load_from(working_dir)
}

#[tracing::instrument(skip_all)]
pub fn load<P: AsRef<Path>>(path: P) -> miette::Result<Self> {
let path = path.as_ref();
pub fn load_from<P: AsRef<Path>>(dir: P) -> miette::Result<Self> {
let path = dir.as_ref().join(TOOLS_CONFIG_NAME);

let mut config: ToolsConfig = if path.exists() {
debug!(file = ?path, "Loading {}", TOOLS_CONFIG_NAME);

toml::from_str(&fs::read_file_with_lock(path)?).into_diagnostic()?
toml::from_str(&fs::read_file_with_lock(&path)?).into_diagnostic()?
} else {
ToolsConfig::default()
};

config.path = path.to_owned();
config.path = path.clone();

// Update plugin file paths to be absolute
for locator in config.plugins.values_mut() {
Expand Down Expand Up @@ -88,10 +90,8 @@ impl ToolsConfig {
let mut config = ToolsConfig::default();

while let Some(dir) = current_dir {
let path = dir.join(TOOLS_CONFIG_NAME);

if path.exists() {
let mut parent_config = Self::load(&path)?;
if dir.join(TOOLS_CONFIG_NAME).exists() {
let mut parent_config = Self::load_from(dir)?;
parent_config.merge(config);

config = parent_config;
Expand Down

0 comments on commit 1ddd96a

Please sign in to comment.