Skip to content

Commit

Permalink
Rust: Added env check to examples (#1676)
Browse files Browse the repository at this point in the history
* Added check

* changed to expect

* allow single for loop

* fix logger test

* changed to unwrap_or
  • Loading branch information
Brord van Wierst authored Nov 29, 2023
1 parent 38e4f97 commit 69c695b
Show file tree
Hide file tree
Showing 119 changed files with 440 additions and 39 deletions.
2 changes: 1 addition & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ required-features = ["wallet", "participation"]
[[example]]
name = "logger"
path = "examples/wallet/logger.rs"
required-features = ["wallet"]
required-features = ["wallet", "storage"]

[[example]]
name = "recover_accounts"
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/01_generate_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "MNEMONIC"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

// Create a node client.
let client = Client::builder()
.with_node(&std::env::var("NODE_URL").unwrap())?
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/02_address_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "MNEMONIC"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

// Create a node client.
let client = Client::builder()
.with_node(&std::env::var("NODE_URL").unwrap())?
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/00_block_no_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();

// Create a node client.
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/01_block_confirmation_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();

// Create a node client.
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/02_block_custom_parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();

// Create a node client.
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/03_block_custom_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();

// Create a node client.
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/04_block_tagged_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();

let tag = std::env::args().nth(1).unwrap_or_else(|| "Hello".to_string());
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/custom_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL", "FAUCET_URL", "MNEMONIC"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();
let faucet_url = std::env::var("FAUCET_URL").unwrap();

Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL", "FAUCET_URL", "MNEMONIC"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();
let faucet_url = std::env::var("FAUCET_URL").unwrap();

Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/block/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL", "FAUCET_URL", "MNEMONIC"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();
let faucet_url = std::env::var("FAUCET_URL").unwrap();

Expand Down
10 changes: 7 additions & 3 deletions sdk/examples/client/custom_remainder_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ use iota_sdk::client::{

#[tokio::main]
async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "MNEMONIC", "FAUCET_URL", "EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let amount = std::env::args()
.nth(1)
.map(|s| s.parse::<u64>().unwrap())
.unwrap_or(9_000_000);

// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

// Create a client instance.
let client = Client::builder()
.with_node(&std::env::var("NODE_URL").unwrap())?
Expand Down
5 changes: 5 additions & 0 deletions sdk/examples/client/get_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

#[allow(clippy::single_element_loop)]
for var in ["NODE_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

// Create a node client.
let client = Client::builder()
.with_node(&std::env::var("NODE_URL").unwrap())?
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/high_level/consolidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL", "MNEMONIC"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let address_range_start = std::env::args().nth(1).map(|s| s.parse::<u32>().unwrap()).unwrap_or(0);
let address_range_len = std::env::args().nth(2).map(|s| s.parse::<u32>().unwrap()).unwrap_or(10);

Expand Down
5 changes: 5 additions & 0 deletions sdk/examples/client/high_level/inputs_from_transaction_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

#[allow(clippy::single_element_loop)]
for var in ["NODE_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();

// Create a node client.
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/high_level/search_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "MNEMONIC"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

let node_url = std::env::var("NODE_URL").unwrap();

// Create a node client.
Expand Down
4 changes: 4 additions & 0 deletions sdk/examples/client/ledger_nano_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

for var in ["NODE_URL", "EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

// Create a client instance
let client = Client::builder()
.with_node(&std::env::var("NODE_URL").unwrap())?
Expand Down
5 changes: 5 additions & 0 deletions sdk/examples/client/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();

#[allow(clippy::single_element_loop)]
for var in ["NODE_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

// Generates a client.log file with logs for debugging.
// We exclude logs from h2, hyper and rustls to reduce the noise.
let logger_output_config = fern_logger::LoggerOutputConfigBuilder::new()
Expand Down
3 changes: 1 addition & 2 deletions sdk/examples/client/node_api_core/01_get_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ use iota_sdk::client::{Client, Result};
async fn main() -> Result<()> {
// If not provided we use the default node from the `.env` file.
dotenvy::dotenv().ok();

// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder()
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/03_get_tips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
7 changes: 6 additions & 1 deletion sdk/examples/client/node_api_core/04_post_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ async fn main() -> Result<()> {
// If not provided we use the default node from the `.env` file.
dotenvy::dotenv().ok();

#[allow(clippy::single_element_loop)]
for var in ["EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
7 changes: 6 additions & 1 deletion sdk/examples/client/node_api_core/05_post_block_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ async fn main() -> Result<()> {
// If not provided we use the default node from the `.env` file.
dotenvy::dotenv().ok();

#[allow(clippy::single_element_loop)]
for var in ["EXPLORER_URL"] {
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
}

// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/06_get_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/07_get_block_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/08_get_block_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/09_get_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/10_get_output_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/12_get_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/14_get_treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(1)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args()
.nth(2)
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));

// Create a node client.
let client = Client::builder().with_node(&node_url)?.finish().await?;
Expand Down
Loading

0 comments on commit 69c695b

Please sign in to comment.