Skip to content

Commit

Permalink
new: Add proto debug config command. (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Dec 11, 2023
1 parent 60dc560 commit c656100
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#### 🚀 Updates

- Added Linux arm64 gnu and musl support (`aarch64-unknown-linux-gnu` and `aarch64-unknown-linux-musl`).
- Added a `proto debug config` command, to debug all loaded configs and the final merged config.
- Added a `PROTO_BYPASS_VERSION_CHECK` environment variable, to bypass loading and checking of versions. Useful when internet is unreliable.

## 0.24.2

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ installers = []
# Path that installers should place binaries in
install-path = "~/.proto/bin"
# Publish jobs to run in CI
pr-run-mode = "upload"
pr-run-mode = "plan" # "upload"
# Skip checking whether the specified configuration files are up to date
allow-dirty = ["ci"]

Expand Down
16 changes: 16 additions & 0 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::commands::{
debug::DebugConfigArgs,
tool::{AddToolArgs, ListToolPluginsArgs, ListToolsArgs, RemoveToolArgs, ToolInfoArgs},
AliasArgs, BinArgs, CleanArgs, CompletionsArgs, InstallArgs, InstallGlobalArgs, ListArgs,
ListGlobalArgs, ListRemoteArgs, MigrateArgs, OutdatedArgs, PinArgs, RunArgs, SetupArgs,
Expand Down Expand Up @@ -117,6 +118,12 @@ pub enum Commands {
)]
Completions(CompletionsArgs),

#[command(name = "debug", about = "Debug the current proto environment.")]
Debug {
#[command(subcommand)]
command: DebugCommands,
},

#[command(
alias = "i",
name = "install",
Expand Down Expand Up @@ -250,6 +257,15 @@ pub enum Commands {
Use,
}

#[derive(Clone, Debug, Subcommand)]
pub enum DebugCommands {
#[command(
name = "config",
about = "Debug all loaded .prototools config's for the current directory."
)]
Config(DebugConfigArgs),
}

#[derive(Clone, Debug, Subcommand)]
pub enum ToolCommands {
#[command(
Expand Down
77 changes: 77 additions & 0 deletions crates/cli/src/commands/debug/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::helpers::ProtoResource;
use clap::Args;
use miette::IntoDiagnostic;
use proto_core::{ProtoConfig, ProtoConfigFile};
use serde::Serialize;
use starbase::system;
use starbase_styles::color::{self, OwoStyle};
use starbase_utils::{json, toml};

#[derive(Serialize)]
pub struct DebugConfigResult<'a> {
config: &'a ProtoConfig,
files: Vec<&'a ProtoConfigFile>,
}

#[derive(Args, Clone, Debug)]
pub struct DebugConfigArgs {
#[arg(long, help = "Print the list in JSON format")]
json: bool,
}

fn print_toml(value: impl Serialize) -> miette::Result<()> {
let contents = toml::to_string_pretty(&value).into_diagnostic()?;

let contents = contents
.lines()
.map(|line| {
let indented_line = format!(" {line}");

if line.starts_with('[') {
indented_line
} else {
color::muted_light(indented_line)
}
})
.collect::<Vec<_>>()
.join("\n");

println!("{}", contents);

Ok(())
}

#[system]
pub async fn config(args: ArgsRef<DebugConfigArgs>, proto: ResourceRef<ProtoResource>) {
let manager = proto.env.load_config_manager()?;
let config = manager.get_merged_config()?;

if args.json {
let result = DebugConfigResult {
config,
files: manager.files.iter().rev().collect::<Vec<_>>(),
};

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

return Ok(());
}

for file in manager.files.iter().rev() {
if !file.exists {
continue;
}

println!();
println!("{}", OwoStyle::new().bold().style(color::path(&file.path)));
print_toml(&file.config)?;
}

println!();
println!(
"{}",
OwoStyle::new().bold().style(color::id("Configuration"))
);
print_toml(config)?;
println!();
}
3 changes: 3 additions & 0 deletions crates/cli/src/commands/debug/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod config;

pub use config::*;
1 change: 1 addition & 0 deletions crates/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod alias;
mod bin;
mod clean;
mod completions;
pub mod debug;
mod install;
mod install_all;
mod install_global;
Expand Down
5 changes: 4 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod printer;
mod shell;
mod systems;

use app::{App as CLI, Commands, ToolCommands};
use app::{App as CLI, Commands, DebugCommands, ToolCommands};
use clap::Parser;
use starbase::{tracing::TracingOptions, App, MainResult};
use starbase_utils::string_vec;
Expand Down Expand Up @@ -58,6 +58,9 @@ async fn main() -> MainResult {
Commands::Bin(args) => app.execute_with_args(commands::bin, args),
Commands::Clean(args) => app.execute_with_args(commands::clean, args),
Commands::Completions(args) => app.execute_with_args(commands::completions, args),
Commands::Debug { command } => match command {
DebugCommands::Config(args) => app.execute_with_args(commands::debug::config, args),
},
Commands::Install(args) => app.execute_with_args(commands::install, args),
Commands::InstallGlobal(args) => app.execute_with_args(commands::install_global, args),
Commands::List(args) => app.execute_with_args(commands::list, args),
Expand Down
17 changes: 12 additions & 5 deletions crates/core/src/proto_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ derive_enum!(
);

#[derive(Clone, Config, Debug, Serialize)]
#[config(allow_unknown_fields, rename_all = "kebab-case")]
#[config(allow_unknown_fields)]
#[serde(rename_all = "kebab-case")]
pub struct ProtoToolConfig {
#[setting(merge = merge::merge_btreemap)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub aliases: BTreeMap<String, UnresolvedVersionSpec>,

// Custom configuration to pass to plugins
#[setting(flatten, merge = merge::merge_btreemap)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub config: BTreeMap<String, JsonValue>,
}

#[derive(Clone, Config, Debug, Serialize)]
#[config(rename_all = "kebab-case")]
#[serde(rename_all = "kebab-case")]
pub struct ProtoSettingsConfig {
#[setting(env = "PROTO_AUTO_CLEAN", parse_env = env::parse_bool)]
pub auto_clean: bool,
Expand Down Expand Up @@ -78,12 +81,15 @@ fn merge_tools(
}

#[derive(Clone, Config, Debug, Serialize)]
#[config(allow_unknown_fields, rename_all = "kebab-case")]
#[config(allow_unknown_fields)]
#[serde(rename_all = "kebab-case")]
pub struct ProtoConfig {
#[setting(nested, merge = merge_tools)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub tools: BTreeMap<Id, ProtoToolConfig>,

#[setting(merge = merge::merge_btreemap)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub plugins: BTreeMap<Id, PluginLocator>,

#[setting(nested)]
Expand All @@ -92,7 +98,8 @@ pub struct ProtoConfig {
#[setting(flatten, merge = merge::merge_btreemap)]
pub versions: BTreeMap<Id, UnresolvedVersionSpec>,

#[setting(flatten, merge = merge::merge_btreemap, skip_serializing)]
#[setting(flatten, merge = merge::merge_btreemap)]
#[serde(skip_serializing)]
pub unknown: BTreeMap<String, TomlValue>,
}

Expand Down Expand Up @@ -307,7 +314,7 @@ impl ProtoConfig {
}
}

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct ProtoConfigFile {
pub exists: bool,
pub global: bool,
Expand Down
16 changes: 9 additions & 7 deletions crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,16 @@ impl Tool {
.into());
}

versions = self.plugin.cache_func_with(
"load_versions",
LoadVersionsInput {
initial: initial_version.to_owned(),
},
)?;
if env::var("PROTO_BYPASS_VERSION_CHECK").is_err() {
versions = self.plugin.cache_func_with(
"load_versions",
LoadVersionsInput {
initial: initial_version.to_owned(),
},
)?;

json::write_file(cache_path, &versions, false)?;
json::write_file(cache_path, &versions, false)?;
}
}

// Cache the results and create a resolver
Expand Down

0 comments on commit c656100

Please sign in to comment.