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

fix(io): replace std::fs in async fns with tokio::fs #458

Closed
wants to merge 5 commits 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features --features reqwest_request,${{ matrix.feature }}
args: --no-default-features --features reqwest_request,tokio_fs,${{ matrix.feature }}

build_all_features:
runs-on: ${{ matrix.os }}
Expand Down
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ version = "0.16.0"
[package.metadata.docs.rs]
all-features = true


[features]
default = ["reqwest_request", "services-all"]
default = ["reqwest_request", "services-all", "tokio_fs"]

native-tls = ["reqwest?/default-tls"]
rustls = ["reqwest?/rustls-tls"]

tokio_fs = ["tokio/fs"]
blocking_io = []

# requests that reqwest supports
reqwest_blocking_request = ["reqwest?/blocking"]
reqwest_request = ["dep:reqwest"]
Expand Down Expand Up @@ -58,6 +62,10 @@ services-huaweicloud = ["dep:serde", "dep:serde_json", "dep:once_cell"]
services-oracle = ["dep:reqwest", "dep:rsa", "dep:toml", "dep:serde"]
services-tencent = ["dep:reqwest", "dep:serde", "dep:serde_json"]

[target.'cfg(target_arch = "wasm32")'.features]
default = ["reqwest_request", "services-all"]


[[bench]]
harness = false
name = "aws"
Expand Down Expand Up @@ -88,11 +96,11 @@ toml = { version = "0.8.9", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
home = "0.5"
tokio = { version = "1", features = ["fs"], optional = true }
tokio = { version = "1", features = ["fs"], optional=true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
tokio = { version = "1", optional = true }
tokio = { version = "1", optional=true }

[dev-dependencies]
aws-credential-types = "1.1.8"
Expand Down
8 changes: 3 additions & 5 deletions src/aliyun/credential.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs;
use std::sync::Arc;
use std::sync::Mutex;

Expand Down Expand Up @@ -133,8 +132,7 @@ impl Loader {
}
_ => return Ok(None),
};

let token = fs::read_to_string(token_file)?;
let token = crate::io::read_file_to_string(token_file).await?;
let role_session_name = &self.config.role_session_name;

// Construct request to Aliyun STS Service.
Expand Down Expand Up @@ -296,7 +294,7 @@ mod tests {
.expect("current_dir must exist")
.to_string_lossy()
);
fs::write(&file_path, token)?;
std::fs::write(&file_path, token)?;

temp_env::with_vars(
vec![
Expand Down Expand Up @@ -395,7 +393,7 @@ mod tests {
.expect("current_dir must exist")
.to_string_lossy()
);
fs::write(&file_path, token)?;
std::fs::write(&file_path, token)?;

temp_env::with_vars(
vec![
Expand Down
7 changes: 3 additions & 4 deletions src/aws/credential.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt::Debug;
use std::fmt::Write;
use std::fs;
use std::sync::Arc;
use std::sync::Mutex;

Expand Down Expand Up @@ -182,7 +181,7 @@ impl DefaultLoader {
_ => return Ok(None),
};

let token = fs::read_to_string(token_file)?;
let token = crate::io::read_file_to_string(token_file).await?;
let role_session_name = &self.config.role_session_name;

let endpoint = self.sts_endpoint()?;
Expand Down Expand Up @@ -769,7 +768,7 @@ mod tests {
.expect("current_dir must exist")
.to_string_lossy()
);
fs::write(&file_path, github_token)?;
std::fs::write(&file_path, github_token)?;

temp_env::with_vars(
vec![
Expand Down Expand Up @@ -849,7 +848,7 @@ mod tests {
.expect("current_dir must exist")
.to_string_lossy()
);
fs::write(&file_path, github_token)?;
std::fs::write(&file_path, github_token)?;

temp_env::with_vars(
vec![
Expand Down
4 changes: 2 additions & 2 deletions src/azure/storage/workload_identity_credential.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, str};
use std::str;

use http::HeaderValue;
use http::Method;
Expand Down Expand Up @@ -27,7 +27,7 @@ pub async fn get_workload_identity_token(config: &Config) -> anyhow::Result<Opti
_ => return Ok(None),
};

let token = fs::read_to_string(token_file)?;
let token = crate::io::read_file_to_string(token_file).await?;
let url = Url::parse(authority_host)?.join(&format!("/{tenant_id}/oauth2/v2.0/token"))?;
let scopes: &[&str] = &[STORAGE_TOKEN_SCOPE];
let encoded_body: String = form_urlencoded::Serializer::new(String::new())
Expand Down
9 changes: 9 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub async fn read_file_to_string(path: &str) -> std::io::Result<String> {

Check warning on line 1 in src/io.rs

View workflow job for this annotation

GitHub Actions / build_single_feature (services-huaweicloud)

function `read_file_to_string` is never used
#[cfg(any(target_arch = "wasm32", feature = "blocking_io"))]
return std::fs::read_to_string(path);
#[cfg(all(
not(target_arch = "wasm32"),
all(feature = "tokio_fs", not(feature = "blocking_io"))
))]
return tokio::fs::read_to_string(path).await;
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ pub use tencent::*;
mod ctx;
mod dirs;
mod hash;
mod io;
mod request;
mod time;
5 changes: 2 additions & 3 deletions src/tencent/credential.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs;
use std::sync::Arc;
use std::sync::Mutex;

Expand Down Expand Up @@ -138,7 +137,7 @@ impl CredentialLoader {
}
};

let token = fs::read_to_string(token_file)?;
let token = crate::io::read_file_to_string(token_file).await?;
let role_session_name = &self.config.role_session_name;

// Construct request to Tencent Cloud STS Service.
Expand Down Expand Up @@ -315,7 +314,7 @@ mod tests {
.expect("current_dir must exist")
.to_string_lossy()
);
fs::write(&file_path, github_token)?;
std::fs::write(&file_path, github_token)?;

temp_env::with_vars(
vec![
Expand Down
Loading