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

Ban use of print macros in non-test code #749

Merged
merged 2 commits into from
Aug 8, 2024
Merged
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
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 @@ -237,12 +238,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 @@ -357,13 +362,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 @@ -396,7 +405,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 Down Expand Up @@ -424,10 +433,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
4 changes: 2 additions & 2 deletions cli/src/cmd_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright 2024 Oxide Computer Company

use crate::context::Context;
use crate::RunnableCmd;
use crate::{println_nopipe, RunnableCmd};

use super::cmd_version::built_info;
use anyhow::Result;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl RunnableCmd for CmdDocs {
app.build();
let json_doc = to_json(&app);
let pretty_json = serde_json::to_string_pretty(&json_doc)?;
println!("{}", pretty_json);
println_nopipe!("{}", pretty_json);
Ok(())
}
}
7 changes: 5 additions & 2 deletions cli/src/cmd_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ use oxide::types::{

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

use crate::println_nopipe;

/// Connect to or retrieve data from the instance's serial console.
#[derive(Parser, Debug, Clone)]
#[command(verbatim_doc_comment)]
Expand Down Expand Up @@ -196,7 +199,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 Expand Up @@ -283,7 +286,7 @@ impl crate::AuthenticatedCmd for CmdInstanceFromImage {
.send()
.await?;

println!("instance {} created", instance.id);
println_nopipe!("instance {} created", instance.id);

Ok(())
}
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
Loading