Skip to content

Commit 69c695b

Browse files
author
Brord van Wierst
authored
Rust: Added env check to examples (iotaledger#1676)
* Added check * changed to expect * allow single for loop * fix logger test * changed to unwrap_or
1 parent 38e4f97 commit 69c695b

File tree

119 files changed

+440
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+440
-39
lines changed

sdk/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ required-features = ["wallet", "participation"]
847847
[[example]]
848848
name = "logger"
849849
path = "examples/wallet/logger.rs"
850-
required-features = ["wallet"]
850+
required-features = ["wallet", "storage"]
851851

852852
[[example]]
853853
name = "recover_accounts"

sdk/examples/client/01_generate_addresses.rs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ async fn main() -> Result<()> {
1919
// This example uses secrets in environment variables for simplicity which should not be done in production.
2020
dotenvy::dotenv().ok();
2121

22+
for var in ["NODE_URL", "MNEMONIC"] {
23+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
24+
}
25+
2226
// Create a node client.
2327
let client = Client::builder()
2428
.with_node(&std::env::var("NODE_URL").unwrap())?

sdk/examples/client/02_address_balance.rs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ async fn main() -> Result<()> {
2222
// This example uses secrets in environment variables for simplicity which should not be done in production.
2323
dotenvy::dotenv().ok();
2424

25+
for var in ["NODE_URL", "MNEMONIC"] {
26+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
27+
}
28+
2529
// Create a node client.
2630
let client = Client::builder()
2731
.with_node(&std::env::var("NODE_URL").unwrap())?

sdk/examples/client/block/00_block_no_payload.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ async fn main() -> Result<()> {
1515
// This example uses secrets in environment variables for simplicity which should not be done in production.
1616
dotenvy::dotenv().ok();
1717

18+
for var in ["NODE_URL", "EXPLORER_URL"] {
19+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
20+
}
21+
1822
let node_url = std::env::var("NODE_URL").unwrap();
1923

2024
// Create a node client.

sdk/examples/client/block/01_block_confirmation_time.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ async fn main() -> Result<()> {
1515
// This example uses secrets in environment variables for simplicity which should not be done in production.
1616
dotenvy::dotenv().ok();
1717

18+
for var in ["NODE_URL", "EXPLORER_URL"] {
19+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
20+
}
21+
1822
let node_url = std::env::var("NODE_URL").unwrap();
1923

2024
// Create a node client.

sdk/examples/client/block/02_block_custom_parents.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ async fn main() -> Result<()> {
1515
// This example uses secrets in environment variables for simplicity which should not be done in production.
1616
dotenvy::dotenv().ok();
1717

18+
for var in ["NODE_URL", "EXPLORER_URL"] {
19+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
20+
}
21+
1822
let node_url = std::env::var("NODE_URL").unwrap();
1923

2024
// Create a node client.

sdk/examples/client/block/03_block_custom_payload.rs

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ async fn main() -> Result<()> {
1818
// This example uses secrets in environment variables for simplicity which should not be done in production.
1919
dotenvy::dotenv().ok();
2020

21+
for var in ["NODE_URL", "EXPLORER_URL"] {
22+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
23+
}
24+
2125
let node_url = std::env::var("NODE_URL").unwrap();
2226

2327
// Create a node client.

sdk/examples/client/block/04_block_tagged_data.rs

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ async fn main() -> Result<()> {
1818
// This example uses secrets in environment variables for simplicity which should not be done in production.
1919
dotenvy::dotenv().ok();
2020

21+
for var in ["NODE_URL", "EXPLORER_URL"] {
22+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
23+
}
24+
2125
let node_url = std::env::var("NODE_URL").unwrap();
2226

2327
let tag = std::env::args().nth(1).unwrap_or_else(|| "Hello".to_string());

sdk/examples/client/block/custom_inputs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ async fn main() -> Result<()> {
2121
// This example uses secrets in environment variables for simplicity which should not be done in production.
2222
dotenvy::dotenv().ok();
2323

24+
for var in ["NODE_URL", "EXPLORER_URL", "FAUCET_URL", "MNEMONIC"] {
25+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
26+
}
27+
2428
let node_url = std::env::var("NODE_URL").unwrap();
2529
let faucet_url = std::env::var("FAUCET_URL").unwrap();
2630

sdk/examples/client/block/output.rs

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ async fn main() -> Result<()> {
1818
// This example uses secrets in environment variables for simplicity which should not be done in production.
1919
dotenvy::dotenv().ok();
2020

21+
for var in ["NODE_URL", "EXPLORER_URL", "FAUCET_URL", "MNEMONIC"] {
22+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
23+
}
24+
2125
let node_url = std::env::var("NODE_URL").unwrap();
2226
let faucet_url = std::env::var("FAUCET_URL").unwrap();
2327

sdk/examples/client/block/transaction.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ async fn main() -> Result<()> {
1515
// This example uses secrets in environment variables for simplicity which should not be done in production.
1616
dotenvy::dotenv().ok();
1717

18+
for var in ["NODE_URL", "EXPLORER_URL", "FAUCET_URL", "MNEMONIC"] {
19+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
20+
}
21+
1822
let node_url = std::env::var("NODE_URL").unwrap();
1923
let faucet_url = std::env::var("FAUCET_URL").unwrap();
2024

sdk/examples/client/custom_remainder_address.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ use iota_sdk::client::{
1616

1717
#[tokio::main]
1818
async fn main() -> Result<()> {
19+
// This example uses secrets in environment variables for simplicity which should not be done in production.
20+
dotenvy::dotenv().ok();
21+
22+
for var in ["NODE_URL", "MNEMONIC", "FAUCET_URL", "EXPLORER_URL"] {
23+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
24+
}
25+
1926
let amount = std::env::args()
2027
.nth(1)
2128
.map(|s| s.parse::<u64>().unwrap())
2229
.unwrap_or(9_000_000);
2330

24-
// This example uses secrets in environment variables for simplicity which should not be done in production.
25-
dotenvy::dotenv().ok();
26-
2731
// Create a client instance.
2832
let client = Client::builder()
2933
.with_node(&std::env::var("NODE_URL").unwrap())?

sdk/examples/client/get_block.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ async fn main() -> Result<()> {
1515
// This example uses secrets in environment variables for simplicity which should not be done in production.
1616
dotenvy::dotenv().ok();
1717

18+
#[allow(clippy::single_element_loop)]
19+
for var in ["NODE_URL"] {
20+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
21+
}
22+
1823
// Create a node client.
1924
let client = Client::builder()
2025
.with_node(&std::env::var("NODE_URL").unwrap())?

sdk/examples/client/high_level/consolidation.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ async fn main() -> Result<()> {
1515
// This example uses secrets in environment variables for simplicity which should not be done in production.
1616
dotenvy::dotenv().ok();
1717

18+
for var in ["NODE_URL", "EXPLORER_URL", "MNEMONIC"] {
19+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
20+
}
21+
1822
let address_range_start = std::env::args().nth(1).map(|s| s.parse::<u32>().unwrap()).unwrap_or(0);
1923
let address_range_len = std::env::args().nth(2).map(|s| s.parse::<u32>().unwrap()).unwrap_or(10);
2024

sdk/examples/client/high_level/inputs_from_transaction_id.rs

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ async fn main() -> Result<()> {
2020
// This example uses secrets in environment variables for simplicity which should not be done in production.
2121
dotenvy::dotenv().ok();
2222

23+
#[allow(clippy::single_element_loop)]
24+
for var in ["NODE_URL"] {
25+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
26+
}
27+
2328
let node_url = std::env::var("NODE_URL").unwrap();
2429

2530
// Create a node client.

sdk/examples/client/high_level/search_address.rs

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ async fn main() -> Result<()> {
2020
// This example uses secrets in environment variables for simplicity which should not be done in production.
2121
dotenvy::dotenv().ok();
2222

23+
for var in ["NODE_URL", "MNEMONIC"] {
24+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
25+
}
26+
2327
let node_url = std::env::var("NODE_URL").unwrap();
2428

2529
// Create a node client.

sdk/examples/client/ledger_nano_transaction.rs

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ async fn main() -> Result<()> {
3030
// This example uses secrets in environment variables for simplicity which should not be done in production.
3131
dotenvy::dotenv().ok();
3232

33+
for var in ["NODE_URL", "EXPLORER_URL"] {
34+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
35+
}
36+
3337
// Create a client instance
3438
let client = Client::builder()
3539
.with_node(&std::env::var("NODE_URL").unwrap())?

sdk/examples/client/logger.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ async fn main() -> Result<()> {
1515
// This example uses secrets in environment variables for simplicity which should not be done in production.
1616
dotenvy::dotenv().ok();
1717

18+
#[allow(clippy::single_element_loop)]
19+
for var in ["NODE_URL"] {
20+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
21+
}
22+
1823
// Generates a client.log file with logs for debugging.
1924
// We exclude logs from h2, hyper and rustls to reduce the noise.
2025
let logger_output_config = fern_logger::LoggerOutputConfigBuilder::new()

sdk/examples/client/node_api_core/01_get_routes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ use iota_sdk::client::{Client, Result};
1414
async fn main() -> Result<()> {
1515
// If not provided we use the default node from the `.env` file.
1616
dotenvy::dotenv().ok();
17-
1817
// Take the node URL from command line argument or use one from env as default.
1918
let node_url = std::env::args()
2019
.nth(1)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
20+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2221

2322
// Create a node client.
2423
let client = Client::builder()

sdk/examples/client/node_api_core/03_get_tips.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
// Take the node URL from command line argument or use one from env as default.
1919
let node_url = std::env::args()
2020
.nth(1)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
21+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2222

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

sdk/examples/client/node_api_core/04_post_block.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ async fn main() -> Result<()> {
1515
// If not provided we use the default node from the `.env` file.
1616
dotenvy::dotenv().ok();
1717

18+
#[allow(clippy::single_element_loop)]
19+
for var in ["EXPLORER_URL"] {
20+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
21+
}
22+
1823
// Take the node URL from command line argument or use one from env as default.
1924
let node_url = std::env::args()
2025
.nth(1)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
26+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2227

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

sdk/examples/client/node_api_core/05_post_block_raw.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ async fn main() -> Result<()> {
1515
// If not provided we use the default node from the `.env` file.
1616
dotenvy::dotenv().ok();
1717

18+
#[allow(clippy::single_element_loop)]
19+
for var in ["EXPLORER_URL"] {
20+
std::env::var(var).unwrap_or_else(|_| panic!(".env variable '{var}' is undefined, see .env.example"));
21+
}
22+
1823
// Take the node URL from command line argument or use one from env as default.
1924
let node_url = std::env::args()
2025
.nth(1)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
26+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2227

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

sdk/examples/client/node_api_core/06_get_block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
// Take the node URL from command line argument or use one from env as default.
1919
let node_url = std::env::args()
2020
.nth(2)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
21+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2222

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

sdk/examples/client/node_api_core/07_get_block_raw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
// Take the node URL from command line argument or use one from env as default.
1919
let node_url = std::env::args()
2020
.nth(2)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
21+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2222

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

sdk/examples/client/node_api_core/08_get_block_metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
// Take the node URL from command line argument or use one from env as default.
1919
let node_url = std::env::args()
2020
.nth(2)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
21+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2222

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

sdk/examples/client/node_api_core/09_get_output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> Result<()> {
2323
// Take the node URL from command line argument or use one from env as default.
2424
let node_url = std::env::args()
2525
.nth(2)
26-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
26+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2727

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

sdk/examples/client/node_api_core/10_get_output_raw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> Result<()> {
2323
// Take the node URL from command line argument or use one from env as default.
2424
let node_url = std::env::args()
2525
.nth(2)
26-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
26+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2727

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

sdk/examples/client/node_api_core/11_get_output_metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> Result<()> {
2323
// Take the node URL from command line argument or use one from env as default.
2424
let node_url = std::env::args()
2525
.nth(2)
26-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
26+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2727

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

sdk/examples/client/node_api_core/12_get_receipts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
// Take the node URL from command line argument or use one from env as default.
1919
let node_url = std::env::args()
2020
.nth(1)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
21+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2222

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

sdk/examples/client/node_api_core/13_get_receipts_migrated_at.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<()> {
1919
// Take the node URL from command line argument or use one from env as default.
2020
let node_url = std::env::args()
2121
.nth(2)
22-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
22+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2323

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

sdk/examples/client/node_api_core/14_get_treasury.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
// Take the node URL from command line argument or use one from env as default.
1919
let node_url = std::env::args()
2020
.nth(1)
21-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
21+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2222

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

sdk/examples/client/node_api_core/17_get_milestone_by_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn main() -> Result<()> {
2222
// Take the node URL from command line argument or use one from env as default.
2323
let node_url = std::env::args()
2424
.nth(2)
25-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
25+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2626

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

sdk/examples/client/node_api_core/18_get_milestone_by_id_raw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn main() -> Result<()> {
2222
// Take the node URL from command line argument or use one from env as default.
2323
let node_url = std::env::args()
2424
.nth(2)
25-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
25+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2626

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

sdk/examples/client/node_api_core/19_get_utxo_changes_by_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn main() -> Result<()> {
2222
// Take the node URL from command line argument or use one from env as default.
2323
let node_url = std::env::args()
2424
.nth(2)
25-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
25+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2626

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

sdk/examples/client/node_api_core/20_get_milestone_by_index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<()> {
1919
// Take the node URL from command line argument or use one from env as default.
2020
let node_url = std::env::args()
2121
.nth(2)
22-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
22+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2323

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

sdk/examples/client/node_api_core/21_get_milestone_by_index_raw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<()> {
1919
// Take the node URL from command line argument or use one from env as default.
2020
let node_url = std::env::args()
2121
.nth(2)
22-
.unwrap_or_else(|| std::env::var("NODE_URL").unwrap());
22+
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set"));
2323

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

0 commit comments

Comments
 (0)