Skip to content

Commit

Permalink
Ban use of print macros in non-test code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wfchandler committed Jul 18, 2024
1 parent f472bce commit eec4989
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
32 changes: 21 additions & 11 deletions cli/src/cmd_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Copyright 2024 Oxide Computer Company

use std::fs::File;
use std::io::{self, Write};

use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;
Expand Down Expand Up @@ -235,12 +236,16 @@ impl CmdAuthLogin {
};

if opened {
println!("Opened this URL in your browser:\n {}", uri);
writeln!(io::stdout(), "Opened this URL in your browser:\n {}", uri)?;
} else {
println!("Open this URL in your browser:\n {}", uri);
writeln!(io::stdout(), "Open this URL in your browser:\n {}", uri)?;
}

println!("\nEnter the code: {}\n", details.user_code().secret());
writeln!(
io::stdout(),
"\nEnter the code: {}\n",
details.user_code().secret()
)?;

let token = auth_client
.exchange_device_access_token(&details)
Expand Down Expand Up @@ -348,13 +353,17 @@ impl CmdAuthLogin {
silo_name,
} = &user;

println!("Login successful");
println!(" silo: {} ({})", **silo_name, silo_id);
println!(" user: {} ({})", display_name, id);
writeln!(io::stdout(), "Login successful")?;
writeln!(io::stdout(), " silo: {} ({})", **silo_name, silo_id)?;
writeln!(io::stdout(), " user: {} ({})", display_name, id)?;
if ctx.config_file().basics.default_profile.is_none() {
println!("Profile '{}' set as the default", profile_name);
writeln!(
io::stdout(),
"Profile '{}' set as the default",
profile_name
)?;
} else {
println!("Use --profile '{}'", profile_name);
writeln!(io::stdout(), "Use --profile '{}'", profile_name)?;
}

Ok(())
Expand Down Expand Up @@ -386,7 +395,7 @@ impl CmdAuthLogout {
if self.all {
// Clear the entire file for users who want to reset their known hosts.
let _ = File::create(credentials_path)?;
println!("Removed all authentication information");
writeln!(io::stdout(), "Removed all authentication information")?;
} else {
let profile = ctx
.client_config()
Expand All @@ -408,10 +417,11 @@ impl CmdAuthLogout {
}
std::fs::write(credentials_path, credentials.to_string())
.expect("unable to write credentials.toml");
println!(
writeln!(
io::stdout(),
"Removed authentication information for profile \"{}\"",
profile_name,
);
)?;
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion cli/src/cmd_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use oxide::types::{

use oxide::ClientInstancesExt;
use oxide::{Client, ClientImagesExt};
use std::io::{self, Write};
use std::path::PathBuf;

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

if self.json {
println!("{}", serde_json::to_string(&data)?);
writeln!(io::stdout(), "{}", serde_json::to_string(&data)?)?;
} else {
let mut tty = thouart::Console::new_stdio(None).await?;
tty.write_stdout(&data.data).await?;
Expand Down
1 change: 1 addition & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Copyright 2024 Oxide Computer Company

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

use std::io;
use std::net::IpAddr;
Expand Down

0 comments on commit eec4989

Please sign in to comment.