Skip to content

Commit

Permalink
internal: Merge shim code into cli crate. (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Dec 14, 2023
1 parent 5bda7a5 commit 22ea685
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 55 deletions.
18 changes: 5 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 0 additions & 21 deletions crates/cli-shim/Cargo.toml

This file was deleted.

12 changes: 12 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ keywords = [
]
categories = ["development-tools"]
readme = "../../README.md"
default-run = "proto"

[package.metadata.release]
pre-release-replacements = [
Expand All @@ -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 }
Expand All @@ -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"
Expand Down
File renamed without changes.
34 changes: 15 additions & 19 deletions crates/cli-shim/src/main.rs → crates/cli/src/main_shim.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -24,12 +28,8 @@ fn get_proto_home() -> Result<PathBuf> {
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"))
}
Expand All @@ -40,8 +40,8 @@ fn create_command(mut args: VecDeque<String>, shim_name: &str) -> Result<Command

// Load the shims registry if it exists
if shims_path.exists() {
let file = fs::read(shims_path).into_diagnostic()?;
let mut shims: HashMap<String, Shim> = serde_json::from_slice(&file).into_diagnostic()?;
let file = fs::read(shims_path)?;
let mut shims: HashMap<String, Shim> = serde_json::from_slice(&file)?;

if shims.contains_key(shim_name) {
shim = shims.remove(shim_name).unwrap();
Expand Down Expand Up @@ -84,7 +84,6 @@ fn create_command(mut args: VecDeque<String>, shim_name: &str) -> Result<Command

pub fn main() -> 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());
Expand All @@ -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."
));
}
Expand All @@ -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
Expand All @@ -141,28 +139,26 @@ 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);

// Handle CTRL+C and kill the child
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");
Expand Down
2 changes: 1 addition & 1 deletion crates/pdk-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion crates/pdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 22ea685

Please sign in to comment.