Skip to content

Commit

Permalink
Polish.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 30, 2023
1 parent 4700d00 commit 6e352fc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 40 deletions.
8 changes: 4 additions & 4 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub enum Commands {
alias = "ap",
name = "add-plugin",
about = "Add a plugin.",
long_about = "Add a plugin to the local .prototools config, or global ~/.proto/config.toml config.",
long_about = "Add a plugin to the local .prototools config, or global ~/.proto/.prototools config.",
hide = true
)]
AddPlugin(AddToolArgs),
Expand Down Expand Up @@ -188,7 +188,7 @@ pub enum Commands {
alias = "rp",
name = "remove-plugin",
about = "Remove a plugin.",
long_about = "Remove a plugin from the local .prototools config, or global ~/.proto/config.toml config.",
long_about = "Remove a plugin from the local .prototools config, or global ~/.proto/.prototools config.",
hide = true
)]
RemovePlugin(RemoveToolArgs),
Expand Down Expand Up @@ -255,7 +255,7 @@ pub enum ToolCommands {
#[command(
name = "add",
about = "Add a tool plugin.",
long_about = "Add a plugin to the local .prototools config, or global ~/.proto/config.toml config."
long_about = "Add a plugin to the local .prototools config, or global ~/.proto/.prototools config."
)]
Add(AddToolArgs),

Expand All @@ -278,7 +278,7 @@ pub enum ToolCommands {
#[command(
name = "remove",
about = "Remove a tool plugin.",
long_about = "Remove a plugin from the local .prototools config, or global ~/.proto/config.toml config."
long_about = "Remove a plugin from the local .prototools config, or global ~/.proto/.prototools config."
)]
Remove(RemoveToolArgs),
}
13 changes: 7 additions & 6 deletions crates/core/src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::helpers::{get_home_dir, get_proto_home, is_offline};
use crate::proto_config::{ProtoConfig, ProtoConfigManager, PROTO_CONFIG_ROOT_NAME};
use crate::proto_config::{ProtoConfig, ProtoConfigManager};
use crate::{ProtoConfigFile, PROTO_CONFIG_NAME};
use once_cell::sync::OnceCell;
use std::collections::BTreeMap;
use std::env;
Expand Down Expand Up @@ -96,11 +97,11 @@ impl ProtoEnvironment {

let mut manager = ProtoConfigManager::load(&self.cwd, end_dir)?;

// Always load the proto home/root config
manager.files.insert(
PathBuf::from(PROTO_CONFIG_ROOT_NAME),
ProtoConfigManager::load_from(&self.root)?,
);
// Always load the proto home/root config last
manager.files.push(ProtoConfigFile {
path: self.root.join(PROTO_CONFIG_NAME),
config: ProtoConfigManager::load_from(&self.root)?,
});

Ok(manager)
})
Expand Down
24 changes: 16 additions & 8 deletions crates/core/src/proto_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use version_spec::*;
use warpgate::{HttpOptions, Id, PluginLocator};

pub const PROTO_CONFIG_NAME: &str = ".prototools";
pub const PROTO_CONFIG_ROOT_NAME: &str = "/";
pub const SCHEMA_PLUGIN_KEY: &str = "internal-schema";

derive_enum!(
Expand Down Expand Up @@ -159,14 +158,20 @@ impl ProtoConfig {
}
}

#[derive(Debug)]
pub struct ProtoConfigFile {
pub path: PathBuf,
pub config: PartialProtoConfig,
}

#[derive(Debug)]
pub struct ProtoConfigManager {
// Paths are sorted from smallest to largest components,
// so we need to traverse in reverse order. Furthermore,
// the special config at `~/.proto/.prototools` is mapped
// "/" to give it the lowest precedence. We also don't
// expect users to put configs in the actual root...
pub files: BTreeMap<PathBuf, PartialProtoConfig>,
pub files: Vec<ProtoConfigFile>,

merged_config: Arc<OnceCell<ProtoConfig>>,
}
Expand All @@ -176,12 +181,13 @@ impl ProtoConfigManager {
trace!("Traversing upwards and loading {} files", PROTO_CONFIG_NAME);

let mut current_dir = Some(start_dir);
let mut files = BTreeMap::new();
let mut files = vec![];

while let Some(dir) = current_dir {
if !files.contains_key(dir) {
files.insert(dir.to_path_buf(), Self::load_from(dir)?);
}
files.push(ProtoConfigFile {
path: dir.join(PROTO_CONFIG_NAME),
config: Self::load_from(dir)?,
});

if end_dir.is_some_and(|end| end == dir) {
break;
Expand Down Expand Up @@ -254,7 +260,9 @@ impl ProtoConfigManager {

pub fn update(dir: &Path, op: impl FnOnce(&mut PartialProtoConfig)) -> miette::Result<PathBuf> {
let mut config = Self::load_from(dir)?;

op(&mut config);

Self::save_to(dir, config)
}

Expand All @@ -263,8 +271,8 @@ impl ProtoConfigManager {
let mut partial = PartialProtoConfig::default();
let context = &();

for file in self.files.values() {
partial.merge(context, file.to_owned())?;
for file in self.files.iter().rev() {
partial.merge(context, file.config.to_owned())?;
}

let mut config = ProtoConfig::from_partial(partial.finalize(context)?);
Expand Down
14 changes: 5 additions & 9 deletions crates/core/src/tool_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ pub fn locate_tool(
debug!(tool = id.as_str(), "Finding a configured plugin");

// Check config files for plugins
for (file, config) in configs.files.iter().rev() {
if let Some(plugins) = &config.plugins {
for file in &configs.files {
if let Some(plugins) = &file.config.plugins {
if let Some(maybe_locator) = plugins.get(id) {
debug!(file = ?file, plugin = maybe_locator.to_string(), "Found a plugin");
debug!(file = ?file.path, plugin = maybe_locator.to_string(), "Found a plugin");

locator = Some(maybe_locator.to_owned());
break;
Expand Down Expand Up @@ -147,13 +147,9 @@ pub async fn load_tool_from_locator(
}

pub async fn load_tool_with_proto(id: &Id, proto: &ProtoEnvironment) -> miette::Result<Tool> {
let locator = locate_tool(id, &proto, false)?;

load_tool_from_locator(id, proto, locator).await
load_tool_from_locator(id, proto, locate_tool(id, proto, false)?).await
}

pub async fn load_tool(id: &Id) -> miette::Result<Tool> {
let proto = ProtoEnvironment::new()?;

load_tool_with_proto(id, &proto).await
load_tool_with_proto(id, &ProtoEnvironment::new()?).await
}
18 changes: 9 additions & 9 deletions crates/core/src/version_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ pub async fn detect_version_first_available(
tool: &Tool,
config_manager: &ProtoConfigManager,
) -> miette::Result<Option<UnresolvedVersionSpec>> {
for (file, local_config) in config_manager.files.iter().rev() {
if let Some(versions) = &local_config.versions {
for file in &config_manager.files {
if let Some(versions) = &file.config.versions {
if let Some(version) = versions.get(&tool.id) {
debug!(
tool = tool.id.as_str(),
version = version.to_string(),
file = ?file,
file = ?file.path,
"Detected version from {} file", PROTO_CONFIG_NAME
);

return Ok(Some(version.to_owned()));
}
}

let dir = file.parent().unwrap();
let dir = file.path.parent().unwrap();

if let Some(version) = tool.detect_version_from(dir).await? {
debug!(
Expand All @@ -45,13 +45,13 @@ pub async fn detect_version_prefer_prototools(
config_manager: &ProtoConfigManager,
) -> miette::Result<Option<UnresolvedVersionSpec>> {
// Check config files first
for (file, local_config) in config_manager.files.iter().rev() {
if let Some(versions) = &local_config.versions {
for file in &config_manager.files {
if let Some(versions) = &file.config.versions {
if let Some(version) = versions.get(&tool.id) {
debug!(
tool = tool.id.as_str(),
version = version.to_string(),
file = ?file,
file = ?file.path,
"Detected version from {} file", PROTO_CONFIG_NAME
);

Expand All @@ -61,8 +61,8 @@ pub async fn detect_version_prefer_prototools(
}

// Then check the ecosystem
for (file, _) in config_manager.files.iter().rev() {
let dir = file.parent().unwrap();
for file in &config_manager.files {
let dir = file.path.parent().unwrap();

if let Some(version) = tool.detect_version_from(dir).await? {
debug!(
Expand Down
9 changes: 5 additions & 4 deletions crates/pdk/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,11 @@ pub fn get_proto_environment() -> anyhow::Result<HostEnvironment> {
#[derive(Deserialize)]
pub struct UserConfigSettings {}

/// Return the loaded proto user configuration (`~/.proto/config.toml`). Does not include plugins!
/// Return the loaded proto user configuration (`~/.proto/.prototools`). Does not include plugins!
pub fn get_proto_user_config() -> anyhow::Result<UserConfigSettings> {
let config = config::get("proto_user_config").expect("Missing proto user configuration!");
let config: UserConfigSettings = json::from_str(&config)?;
if let Some(config) = config::get("proto_user_config") {
return Ok(json::from_str(&config)?);
}

Ok(config)
Ok(UserConfigSettings {})
}

0 comments on commit 6e352fc

Please sign in to comment.