Skip to content

Commit eec4989

Browse files
committed
Ban use of print macros in non-test code
With f472bce (Ignore EPIPE in CLI (#746), 2024-07-17) we added the `_nopipe` variants of the `(e)print(ln)!` macros to avoid panicking when piping to head(1). However, we do not systematically enforce their use, which will inevitably lead inconsistent usage within the project. Add `clippy` lints to ban use of the print macros in all non-test code. Either the `_nopipe` variants should be used when we don't care if the input is read, or `write!` when the information is crucial, such as an interactive session.
1 parent f472bce commit eec4989

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

cli/src/cmd_auth.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Copyright 2024 Oxide Computer Company
66

77
use std::fs::File;
8+
use std::io::{self, Write};
89

910
use anyhow::{anyhow, bail, Result};
1011
use async_trait::async_trait;
@@ -235,12 +236,16 @@ impl CmdAuthLogin {
235236
};
236237

237238
if opened {
238-
println!("Opened this URL in your browser:\n {}", uri);
239+
writeln!(io::stdout(), "Opened this URL in your browser:\n {}", uri)?;
239240
} else {
240-
println!("Open this URL in your browser:\n {}", uri);
241+
writeln!(io::stdout(), "Open this URL in your browser:\n {}", uri)?;
241242
}
242243

243-
println!("\nEnter the code: {}\n", details.user_code().secret());
244+
writeln!(
245+
io::stdout(),
246+
"\nEnter the code: {}\n",
247+
details.user_code().secret()
248+
)?;
244249

245250
let token = auth_client
246251
.exchange_device_access_token(&details)
@@ -348,13 +353,17 @@ impl CmdAuthLogin {
348353
silo_name,
349354
} = &user;
350355

351-
println!("Login successful");
352-
println!(" silo: {} ({})", **silo_name, silo_id);
353-
println!(" user: {} ({})", display_name, id);
356+
writeln!(io::stdout(), "Login successful")?;
357+
writeln!(io::stdout(), " silo: {} ({})", **silo_name, silo_id)?;
358+
writeln!(io::stdout(), " user: {} ({})", display_name, id)?;
354359
if ctx.config_file().basics.default_profile.is_none() {
355-
println!("Profile '{}' set as the default", profile_name);
360+
writeln!(
361+
io::stdout(),
362+
"Profile '{}' set as the default",
363+
profile_name
364+
)?;
356365
} else {
357-
println!("Use --profile '{}'", profile_name);
366+
writeln!(io::stdout(), "Use --profile '{}'", profile_name)?;
358367
}
359368

360369
Ok(())
@@ -386,7 +395,7 @@ impl CmdAuthLogout {
386395
if self.all {
387396
// Clear the entire file for users who want to reset their known hosts.
388397
let _ = File::create(credentials_path)?;
389-
println!("Removed all authentication information");
398+
writeln!(io::stdout(), "Removed all authentication information")?;
390399
} else {
391400
let profile = ctx
392401
.client_config()
@@ -408,10 +417,11 @@ impl CmdAuthLogout {
408417
}
409418
std::fs::write(credentials_path, credentials.to_string())
410419
.expect("unable to write credentials.toml");
411-
println!(
420+
writeln!(
421+
io::stdout(),
412422
"Removed authentication information for profile \"{}\"",
413423
profile_name,
414-
);
424+
)?;
415425
}
416426

417427
Ok(())

cli/src/cmd_instance.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use oxide::types::{
1414

1515
use oxide::ClientInstancesExt;
1616
use oxide::{Client, ClientImagesExt};
17+
use std::io::{self, Write};
1718
use std::path::PathBuf;
1819

1920
/// Connect to or retrieve data from the instance's serial console.
@@ -196,7 +197,7 @@ impl CmdInstanceSerialHistory {
196197
let data = req.send().await.map_err(|e| e.into_untyped())?.into_inner();
197198

198199
if self.json {
199-
println!("{}", serde_json::to_string(&data)?);
200+
writeln!(io::stdout(), "{}", serde_json::to_string(&data)?)?;
200201
} else {
201202
let mut tty = thouart::Console::new_stdio(None).await?;
202203
tty.write_stdout(&data.data).await?;

cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Copyright 2024 Oxide Computer Company
66

77
#![forbid(unsafe_code)]
8+
#![cfg_attr(not(test), deny(clippy::print_stdout, clippy::print_stderr))]
89

910
use std::io;
1011
use std::net::IpAddr;

0 commit comments

Comments
 (0)