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

Add run_command abstraction to azure_identity #2175

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 0 additions & 38 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions sdk/identity/azure_identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ async-trait.workspace = true
openssl = { workspace = true, optional = true }
pin-project.workspace = true
typespec_client_core = { workspace = true, features = ["derive"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
async-process.workspace = true
tokio = { workspace = true, optional = true }

[target.'cfg(unix)'.dependencies]
tz-rs = { workspace = true, optional = true }
Expand All @@ -46,6 +44,7 @@ default = ["reqwest", "old_azure_cli"]
reqwest = ["azure_core/reqwest"]
reqwest_rustls = ["azure_core/reqwest_rustls"]
client_certificate = ["openssl"]
tokio_process = ["tokio"]

# If you are using and Azure CLI version older than 2.54.0 from November 2023,
# upgrade your Azure CLI version or enable this feature.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use crate::credentials::cache::TokenCache;
use async_process::Command;
use crate::{credentials::cache::TokenCache, run_command::run_command};
use azure_core::{
credentials::{AccessToken, Secret, TokenCredential},
error::{Error, ErrorKind, ResultExt},
Expand Down Expand Up @@ -193,7 +192,7 @@ impl AzureCliCredential {
args.join(" "),
);

match Command::new(program).args(args).output().await {
match run_command(program, args).await {
Ok(az_output) if az_output.status.success() => {
let output = str::from_utf8(&az_output.stdout)?;

Expand Down
2 changes: 2 additions & 0 deletions sdk/identity/azure_identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#![doc = include_str!("../README.md")]

mod run_command;

mod authorization_code_flow;
mod credentials;
mod env;
Expand Down
38 changes: 38 additions & 0 deletions sdk/identity/azure_identity/src/run_command/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::io::Error;
use std::{ffi::OsStr, process::Output};

#[cfg(feature = "tokio_process")]
pub(crate) async fn run_command<S, I, A>(program: S, args: I) -> Result<Output, Error>
where
S: AsRef<OsStr>,
I: IntoIterator<Item = A>,
A: AsRef<OsStr>,
{
tokio::process::Command::new(program)
.args(args)
.output()
.await
}

#[cfg(not(feature = "tokio_process"))]
pub(crate) async fn run_command<S, I, A>(program: S, args: I) -> Result<Output, Error>
where
S: AsRef<OsStr>,
I: IntoIterator<Item = A>,
A: AsRef<OsStr>,
{
use futures::channel::oneshot;
use std::io::ErrorKind;

let (tx, rx) = oneshot::channel();
let mut cmd = std::process::Command::new(program);
cmd.args(args);
std::thread::spawn(move || {
let output = cmd.output();
tx.send(output)
});
let output = rx
.await
.map_err(|err| Error::new(ErrorKind::Other, err))??;
Ok(output)
}
Loading