From 22ea685a33a2fbfc100838c59260ec113ffaa27f Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 13 Dec 2023 17:12:10 -0800 Subject: [PATCH] internal: Merge shim code into cli crate. (#335) --- Cargo.lock | 18 +++------- Cargo.toml | 1 + crates/cli-shim/Cargo.toml | 21 ------------ crates/cli/Cargo.toml | 12 +++++++ .../RESULTS.md => cli/SHIM_RESULTS.md} | 0 .../src/main.rs => cli/src/main_shim.rs} | 34 ++++++++----------- crates/pdk-api/Cargo.toml | 2 +- crates/pdk/Cargo.toml | 2 +- 8 files changed, 35 insertions(+), 55 deletions(-) delete mode 100644 crates/cli-shim/Cargo.toml rename crates/{cli-shim/RESULTS.md => cli/SHIM_RESULTS.md} (100%) rename crates/{cli-shim/src/main.rs => cli/src/main_shim.rs} (85%) diff --git a/Cargo.lock b/Cargo.lock index b62893ca0..fe3f44b50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2445,10 +2445,12 @@ dependencies = [ name = "proto_cli" version = "0.25.1" dependencies = [ + "anyhow", "chrono", "clap 4.4.11", "clap_complete", "convert_case", + "ctrlc", "dialoguer", "dirs 5.0.1", "futures", @@ -2463,6 +2465,9 @@ dependencies = [ "rustls-webpki", "semver", "serde", + "serde_json", + "shared_child", + "sigpipe", "starbase", "starbase_archive", "starbase_sandbox", @@ -2475,19 +2480,6 @@ dependencies = [ "winreg 0.52.0", ] -[[package]] -name = "proto_cli_shim" -version = "0.25.0" -dependencies = [ - "ctrlc", - "dirs 5.0.1", - "serde", - "serde_json", - "shared_child", - "sigpipe", - "starbase", -] - [[package]] name = "proto_core" version = "0.25.1" diff --git a/Cargo.toml b/Cargo.toml index 0e90c0cdd..e9126de80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = ["crates/*"] default-members = ["crates/cli"] [workspace.dependencies] +anyhow = "1.0.75" cached = "0.46.1" clap = "4.4.11" clap_complete = "4.4.4" diff --git a/crates/cli-shim/Cargo.toml b/crates/cli-shim/Cargo.toml deleted file mode 100644 index a2a7a6a99..000000000 --- a/crates/cli-shim/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "proto_cli_shim" -version = "0.25.0" -edition = "2021" -publish = false - -[[bin]] -name = "proto-shim" -path = "src/main.rs" - -[dependencies] -ctrlc = "3.4.1" -dirs = { workspace = true } -serde = { workspace = true } -serde_json = { workspace = true } -shared_child = "1.0.0" -sigpipe = "0.1.3" -starbase = { workspace = true } - -[package.metadata.dist] -dist = true diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 8883dc13f..ad7dc0d35 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -15,6 +15,7 @@ keywords = [ ] categories = ["development-tools"] readme = "../../README.md" +default-run = "proto" [package.metadata.release] pre-release-replacements = [ @@ -26,10 +27,15 @@ pre-release-replacements = [ name = "proto" path = "src/main.rs" +[[bin]] +name = "proto-shim" +path = "src/main_shim.rs" + [dependencies] proto_core = { version = "0.25.1", path = "../core" } proto_pdk_api = { version = "0.11.2", path = "../pdk-api" } system_env = { version = "0.1.8", path = "../system-env" } +anyhow = { workspace = true } chrono = "0.4.31" clap = { workspace = true, features = ["derive", "env"] } clap_complete = { workspace = true } @@ -51,6 +57,12 @@ thiserror = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } +# For the shim binary +ctrlc = "3.4.1" +serde_json = { workspace = true } +shared_child = "1.0.0" +sigpipe = "0.1.3" + # Force latest rustls rustls = "0.21.9" rustls-pemfile = "1.0.4" diff --git a/crates/cli-shim/RESULTS.md b/crates/cli/SHIM_RESULTS.md similarity index 100% rename from crates/cli-shim/RESULTS.md rename to crates/cli/SHIM_RESULTS.md diff --git a/crates/cli-shim/src/main.rs b/crates/cli/src/main_shim.rs similarity index 85% rename from crates/cli-shim/src/main.rs rename to crates/cli/src/main_shim.rs index a16e8c090..68f5a967c 100644 --- a/crates/cli-shim/src/main.rs +++ b/crates/cli/src/main_shim.rs @@ -1,6 +1,10 @@ +// NOTE: We want to keep the shim binary as lean as possible, +// so these imports use std as much as possible, and should +// not pull in large libraries (tracing is already enough)! + +use anyhow::{anyhow, Result}; use serde::Deserialize; use shared_child::SharedChild; -use starbase::diagnostics::{self, miette, IntoDiagnostic, Result}; use starbase::tracing::{self, trace, TracingOptions}; use std::collections::{HashMap, VecDeque}; use std::io::{IsTerminal, Read, Write}; @@ -24,12 +28,8 @@ fn get_proto_home() -> Result { return Ok(root.into()); } - let home_dir = dirs::home_dir().ok_or_else(|| { - miette!( - code = "proto::unknown_home_dir", - "Unable to determine user home directory." - ) - })?; + let home_dir = + dirs::home_dir().ok_or_else(|| anyhow!("Unable to determine user home directory."))?; Ok(home_dir.join(".proto")) } @@ -40,8 +40,8 @@ fn create_command(mut args: VecDeque, shim_name: &str) -> Result = serde_json::from_slice(&file).into_diagnostic()?; + let file = fs::read(shims_path)?; + let mut shims: HashMap = serde_json::from_slice(&file)?; if shims.contains_key(shim_name) { shim = shims.remove(shim_name).unwrap(); @@ -84,7 +84,6 @@ fn create_command(mut args: VecDeque, shim_name: &str) -> Result Result<()> { sigpipe::reset(); - diagnostics::setup_miette(); // Setup tracing and pass log level down let log_level = env::var("PROTO_LOG").unwrap_or_else(|_| "info".into()); @@ -109,8 +108,7 @@ pub fn main() -> Result<()> { trace!(args = ?args, shim = ?exe_path, "Running {} shim", shim_name); if shim_name.is_empty() || shim_name.contains("proto-shim") { - return Err(miette!( - code = "proto::invalid_shim", + return Err(anyhow!( "Invalid shim name detected. Unable to execute the appropriate proto tool.\nPlease refer to the documentation or ask for support on Discord." )); } @@ -123,7 +121,7 @@ pub fn main() -> Result<()> { // Only read piped data when stdin is not a TTY, // otherwise the process will hang indefinitely waiting for EOF if !stdin.is_terminal() { - stdin.read_to_string(&mut buffer).into_diagnostic()?; + stdin.read_to_string(&mut buffer)?; } buffer @@ -141,7 +139,7 @@ pub fn main() -> Result<()> { // Spawn a shareable child process trace!("Spawning proto child process"); - let shared_child = SharedChild::spawn(&mut command).into_diagnostic()?; + let shared_child = SharedChild::spawn(&mut command)?; let child = Arc::new(shared_child); let child_clone = Arc::clone(&child); @@ -149,20 +147,18 @@ pub fn main() -> Result<()> { ctrlc::set_handler(move || { trace!("Received CTRL+C, killing child process"); let _ = child_clone.kill(); - }) - .into_diagnostic()?; + })?; // If we have piped data, pass it through if has_piped_stdin { if let Some(mut stdin) = child.take_stdin() { trace!(input, "Received piped input, passing through"); - - stdin.write_all(input.as_bytes()).into_diagnostic()?; + stdin.write_all(input.as_bytes())?; } } // Wait for the process to finish or be killed - let status = child.wait().into_diagnostic()?; + let status = child.wait()?; let code = status.code().unwrap_or(0); trace!(code, "Received exit code"); diff --git a/crates/pdk-api/Cargo.toml b/crates/pdk-api/Cargo.toml index 6f0199519..30114dce5 100644 --- a/crates/pdk-api/Cargo.toml +++ b/crates/pdk-api/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/moonrepo/proto" system_env = { version = "0.1.8", path = "../system-env" } version_spec = { version = "0.1.7", path = "../version-spec" } warpgate_api = { version = "0.1.6", path = "../warpgate-api" } -anyhow = "1.0.75" +anyhow = { workspace = true } semver = { workspace = true, features = ["serde"] } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/pdk/Cargo.toml b/crates/pdk/Cargo.toml index f4cdf2881..bc4091c9a 100644 --- a/crates/pdk/Cargo.toml +++ b/crates/pdk/Cargo.toml @@ -9,6 +9,6 @@ repository = "https://github.com/moonrepo/proto" [dependencies] proto_pdk_api = { version = "0.11.2", path = "../pdk-api" } -anyhow = "1.0.75" +anyhow = { workspace = true } extism-pdk = { workspace = true } serde = { workspace = true }