From e90cd1a2d05419ecd3abf99569a216dff26c78c6 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Fri, 22 Dec 2023 11:18:41 -0800 Subject: [PATCH] new: Add `debug env` command. (#359) --- CHANGELOG.md | 1 + Cargo.lock | 4 +- Cargo.toml | 2 +- crates/cli/src/app.rs | 3 + crates/cli/src/commands/debug/env.rs | 84 ++++++++++++++++++++++++ crates/cli/src/commands/debug/mod.rs | 2 + crates/cli/src/main.rs | 1 + crates/core/templates/unix/sh.tpl | 9 --- crates/core/templates/windows/cmd.tpl | 11 ---- crates/core/templates/windows/macros.tpl | 3 - crates/core/templates/windows/ps1.tpl | 20 ------ crates/core/templates/windows/sh.tpl | 4 -- 12 files changed, 94 insertions(+), 50 deletions(-) create mode 100644 crates/cli/src/commands/debug/env.rs delete mode 100644 crates/core/templates/unix/sh.tpl delete mode 100644 crates/core/templates/windows/cmd.tpl delete mode 100644 crates/core/templates/windows/macros.tpl delete mode 100644 crates/core/templates/windows/ps1.tpl delete mode 100644 crates/core/templates/windows/sh.tpl diff --git a/CHANGELOG.md b/CHANGELOG.md index ec49d16bb..365221066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ #### 🚀 Updates +- Added a `proto debug env` command, for debugging basic env/store information. - Updated version resolve errors to include the tool that failed. #### ⚙️ Internal diff --git a/Cargo.lock b/Cargo.lock index 24d326e31..43b04b05b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,9 +125,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.75" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +checksum = "59d2a3357dde987206219e78ecfbbb6e8dad06cbb65292758d3270e6254f7355" [[package]] name = "arbitrary" diff --git a/Cargo.toml b/Cargo.toml index 3a3c91f4e..5c6e36fc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*"] default-members = ["crates/cli"] [workspace.dependencies] -anyhow = "1.0.75" +anyhow = "1.0.76" cached = "0.46.1" clap = "4.4.11" clap_complete = "4.4.4" diff --git a/crates/cli/src/app.rs b/crates/cli/src/app.rs index 628feeabb..cddee17a8 100644 --- a/crates/cli/src/app.rs +++ b/crates/cli/src/app.rs @@ -232,6 +232,9 @@ pub enum DebugCommands { about = "Debug all loaded .prototools config's for the current directory." )] Config(DebugConfigArgs), + + #[command(name = "env", about = "Debug the current proto environment and store.")] + Env, } #[derive(Clone, Debug, Subcommand)] diff --git a/crates/cli/src/commands/debug/env.rs b/crates/cli/src/commands/debug/env.rs new file mode 100644 index 000000000..81c377c88 --- /dev/null +++ b/crates/cli/src/commands/debug/env.rs @@ -0,0 +1,84 @@ +use crate::helpers::ProtoResource; +use crate::printer::Printer; +use proto_pdk_api::{HostArch, HostOS}; +use starbase::system; +use starbase_styles::color; +use std::env; + +#[system] +pub async fn env(proto: ResourceRef) { + let manager = proto.env.load_config_manager()?; + let mut printer = Printer::new(); + + // STORE + + printer.named_section("Store", |p| { + p.entry("Root", color::path(&proto.env.root)); + p.entry("Bins", color::path(&proto.env.bin_dir)); + p.entry("Shims", color::path(&proto.env.shims_dir)); + p.entry("Plugins", color::path(&proto.env.plugins_dir)); + p.entry("Tools", color::path(&proto.env.tools_dir)); + p.entry("Temp", color::path(&proto.env.temp_dir)); + p.entry_map( + "Virtual", + proto + .env + .get_virtual_paths() + .iter() + .map(|(h, g)| (color::file(g.to_string_lossy()), color::path(h))), + None, + ); + p.entry_list( + "Configs", + manager.files.iter().filter_map(|f| { + if f.exists { + Some(color::path(&f.path)) + } else { + None + } + }), + None, + ); + + Ok(()) + })?; + + // ENV + + printer.named_section("Environment", |p| { + p.entry( + "Proto version", + color::muted_light(env!("CARGO_PKG_VERSION")), + ); + p.entry( + "Operating system", + color::muted_light(HostOS::from_env().to_string()), + ); + p.entry( + "Architecture", + color::muted_light(HostArch::from_env().to_string()), + ); + p.entry_map( + "Variables", + env::vars().filter_map(|(k, v)| { + if k.starts_with("PROTO_") { + Some(( + color::id(k), + if v.contains('/') || v.contains('\\') { + color::path(v) + } else { + color::muted_light(v) + }, + )) + } else { + None + } + }), + None, + ); + + Ok(()) + })?; + + printer.flush(); +} diff --git a/crates/cli/src/commands/debug/mod.rs b/crates/cli/src/commands/debug/mod.rs index aa4926f43..0c5980310 100644 --- a/crates/cli/src/commands/debug/mod.rs +++ b/crates/cli/src/commands/debug/mod.rs @@ -1,3 +1,5 @@ mod config; +mod env; pub use config::*; +pub use env::*; diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index c49e0896f..968d87505 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -67,6 +67,7 @@ async fn main() -> MainResult { 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), + DebugCommands::Env => app.execute(commands::debug::env), }, Commands::Install(args) => app.execute_with_args(commands::install, args), Commands::InstallGlobal(args) => app.execute_with_args(commands::install_global, args), diff --git a/crates/core/templates/unix/sh.tpl b/crates/core/templates/unix/sh.tpl deleted file mode 100644 index 0f611a8d8..000000000 --- a/crates/core/templates/unix/sh.tpl +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -set -e - -if [ -n "$PROTO_DEBUG" ]; then - set -x - echo "Running with {{ bin }}.sh shim" -fi - -exec proto run {{ bin }} {% if alt_bin %}--alt "{{ alt_bin }}" {% endif %}-- {{ before_args }} "$@" {{ after_args }} diff --git a/crates/core/templates/windows/cmd.tpl b/crates/core/templates/windows/cmd.tpl deleted file mode 100644 index f40152a85..000000000 --- a/crates/core/templates/windows/cmd.tpl +++ /dev/null @@ -1,11 +0,0 @@ -{% import "macros.tpl" as macros %} - -@echo off -setlocal - -if defined PROTO_DEBUG ( - echo "Running with {{ bin }}.cmd shim" -) - -{# This hack removes the "Terminate Batch Job" message #} -endlocal & goto #_undefined_# 2>NUL || title %COMSPEC% & {{ macros::exec(args="%*") }} diff --git a/crates/core/templates/windows/macros.tpl b/crates/core/templates/windows/macros.tpl deleted file mode 100644 index 745e3ccf9..000000000 --- a/crates/core/templates/windows/macros.tpl +++ /dev/null @@ -1,3 +0,0 @@ -{% macro exec(args) -%} -proto.exe run {{ bin }} {% if alt_bin %}--alt "{{ alt_bin }}" {% endif %}-- {{ before_args }} {{ args }} {{ after_args }} -{%- endmacro input %} diff --git a/crates/core/templates/windows/ps1.tpl b/crates/core/templates/windows/ps1.tpl deleted file mode 100644 index ac861f688..000000000 --- a/crates/core/templates/windows/ps1.tpl +++ /dev/null @@ -1,20 +0,0 @@ -{% import "macros.tpl" as macros %} - -#!/usr/bin/env pwsh -$ErrorActionPreference = 'Stop' - -if (Test-Path env:PROTO_DEBUG) { - $DebugPreference = 'Continue' - Write-Output "Running with {{ bin }}.ps1 shim" -} - -$ret = 0 - -if ($MyInvocation.ExpectingInput) { - $input | & {{ macros::exec(args="$args") }} -} else { - & {{ macros::exec(args="$args") }} -} - -$ret = $LASTEXITCODE -exit $ret diff --git a/crates/core/templates/windows/sh.tpl b/crates/core/templates/windows/sh.tpl deleted file mode 100644 index d9e055be8..000000000 --- a/crates/core/templates/windows/sh.tpl +++ /dev/null @@ -1,4 +0,0 @@ -{% import "macros.tpl" as macros %} - -#!/bin/sh -{{ macros::exec(args='"$@"') }}