Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Factor out code for calling arbitrary bundled binaries #2611

Merged
merged 3 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## DFX

### refactor: Factor out code for calling arbitrary bundled binaries

The function for calling sns can now call any bundled binary.

### fix: Only kill main process on `dfx stop`
Removes misleading panics when running `dfx stop`.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Library for calling the `sns` command line tool.
//! Library for calling bundled command line tools.
use anyhow::{anyhow, Context};
use fn_error_context::context;
use std::ffi::OsStr;
Expand All @@ -8,23 +8,27 @@ use std::process::{self, Command};
use crate::lib::error::DfxResult;
use crate::Environment;

/// Calls the sns command line tool from the SNS codebase in the ic repo.
/// Calls a bundled command line tool.
///
/// # Returns
/// - On success, returns stdout as a string.
/// - On error, returns an error message including stdout and stderr.
#[context("Failed to call sns CLI.")]
pub fn call_sns_cli<S, I>(env: &dyn Environment, args: I) -> DfxResult<String>
pub fn call_bundled<S, I>(env: &dyn Environment, command: &str, args: I) -> DfxResult<String>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let cli_name = "sns";
let sns_cli = env
let binary = env
.get_cache()
.get_binary_command_path(cli_name)
.with_context(|| format!("Could not find bundled binary '{cli_name}'."))?;
let mut command = Command::new(&sns_cli);
.get_binary_command_path(command)
.with_context(|| format!("Could not find bundled binary '{command}'."))?;
let mut command = Command::new(&binary);
command.args(args);
// The sns command line tool itself calls dfx; it should call this dfx.
// The sns command line tool should not rely on commands not packaged with dfx.
command.env("PATH", sns_cli.parent().unwrap_or_else(|| Path::new(".")));
// The same applies to other bundled binaries.
command.env("PATH", binary.parent().unwrap_or_else(|| Path::new(".")));
command
.stdin(process::Stdio::null())
.output()
Expand All @@ -39,7 +43,7 @@ where
.map(OsStr::to_string_lossy)
.collect();
Err(anyhow!(
"SNS cli call failed:\n{:?} {}\nStdout:\n{}\n\nStderr:\n{}",
"Call failed:\n{:?} {}\nStdout:\n{}\n\nStderr:\n{}",
command.get_program(),
args.join(" "),
String::from_utf8_lossy(&output.stdout),
Expand Down
1 change: 1 addition & 0 deletions src/dfx/src/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod bitcoin;
pub mod builders;
pub mod call_bundled;
pub mod canister_http;
pub mod canister_info;
pub mod config;
Expand Down
4 changes: 2 additions & 2 deletions src/dfx/src/lib/sns/create_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use fn_error_context::context;
use std::ffi::OsString;
use std::path::Path;

use crate::lib::call_bundled::call_bundled;
use crate::lib::error::DfxResult;
use crate::lib::sns::sns_cli::call_sns_cli;
use crate::Environment;

/// Ceates an SNS configuration template.
Expand All @@ -16,6 +16,6 @@ pub fn create_config(env: &dyn Environment, path: &Path) -> DfxResult {
OsString::from(path),
OsString::from("new"),
];
call_sns_cli(env, &args)?;
call_bundled(env, "sns", &args)?;
Ok(())
}
5 changes: 3 additions & 2 deletions src/dfx/src/lib/sns/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use fn_error_context::context;
use std::ffi::OsString;
use std::path::Path;

use crate::lib::call_bundled::call_bundled;
use crate::lib::error::DfxResult;
use crate::lib::sns::sns_cli::call_sns_cli;
use crate::Environment;

/// Creates an SNS. This requires funds but no proposal.
Expand All @@ -15,5 +15,6 @@ pub fn deploy_sns(env: &dyn Environment, path: &Path) -> DfxResult<String> {
OsString::from("--init-config-file"),
OsString::from(path),
];
call_sns_cli(env, &args).map(|stdout| format!("Deployed SNS: {}\n{}", path.display(), stdout))
call_bundled(env, "sns", &args)
.map(|stdout| format!("Deployed SNS: {}\n{}", path.display(), stdout))
}
1 change: 0 additions & 1 deletion src/dfx/src/lib/sns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![warn(clippy::missing_docs_in_private_items)]
pub mod create_config;
pub mod deploy;
pub mod sns_cli;
pub mod validate_config;

/// The default location of an SNS configuration file.
Expand Down
4 changes: 2 additions & 2 deletions src/dfx/src/lib/sns/validate_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use fn_error_context::context;
use std::ffi::OsString;
use std::path::Path;

use crate::lib::call_bundled::call_bundled;
use crate::lib::error::DfxResult;
use crate::lib::sns::sns_cli::call_sns_cli;
use crate::Environment;

/// Checks whether an SNS configuration file is valid.
Expand All @@ -16,5 +16,5 @@ pub fn validate_config(env: &dyn Environment, path: &Path) -> DfxResult<String>
OsString::from(path),
OsString::from("validate"),
];
call_sns_cli(env, &args).map(|_| format!("SNS config file is valid: {}", path.display()))
call_bundled(env, "sns", &args).map(|_| format!("SNS config file is valid: {}", path.display()))
}