simplify: drop redundant CommandContext derivations and guards

- ssh/graph: remove `explicit_json_requested() &&` guard before
  `require_no_json_override()` (the call already no-ops without --json).
- pr close/merge/view: drop the outer `with_target` derivation that was
  used only for `printer()` and the output format — both match base_ctx,
  so the derivation was an unused disk-read + settings re-merge.
- command_context tests: collapse `synthetic_context` to delegate to
  `synthetic_context_with_settings`, removing duplicated struct literals.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bryan Helmkamp 2026-04-23 07:28:54 -04:00
parent 2b933597b2
commit 07d4aee67c
No known key found for this signature in database
6 changed files with 23 additions and 30 deletions

View file

@ -228,25 +228,6 @@ mod tests {
}
}
fn synthetic_context(process_local_json: bool, printer: Printer) -> CommandContext {
let cli_layer = cli_layer_with_json_and_verbose();
let (machine_settings, user_settings) =
merge_settings_layer(parse_settings_layer("_version = 1\n").unwrap(), &cli_layer)
.expect("settings should merge");
CommandContext {
printer,
process_local_json,
cwd: PathBuf::from("/tmp/workspace"),
base_config_path: PathBuf::from("/tmp/settings.toml"),
cli_layer,
machine_settings,
user_settings,
server_mode: ServerMode::None,
server: OnceCell::new(),
}
}
fn synthetic_context_with_settings(
process_local_json: bool,
printer: Printer,
@ -268,6 +249,21 @@ mod tests {
}
}
fn synthetic_context(process_local_json: bool, printer: Printer) -> CommandContext {
let cli_layer = cli_layer_with_json_and_verbose();
let (machine_settings, user_settings) =
merge_settings_layer(parse_settings_layer("_version = 1\n").unwrap(), &cli_layer)
.expect("settings should merge");
synthetic_context_with_settings(
process_local_json,
printer,
cli_layer,
machine_settings,
user_settings,
ServerMode::None,
)
}
#[test]
fn context_exposes_resolved_output_and_explicit_json_state() {
let ctx = synthetic_context(true, Printer::Default);

View file

@ -29,7 +29,7 @@ pub(crate) async fn run(
styles: &Styles,
base_ctx: &CommandContext,
) -> anyhow::Result<()> {
if base_ctx.explicit_json_requested() && args.output.is_none() {
if args.output.is_none() {
base_ctx.require_no_json_override()?;
}

View file

@ -7,8 +7,7 @@ use crate::command_context::CommandContext;
use crate::shared::print_json_pretty;
pub(super) async fn close_command(args: PrCloseArgs, base_ctx: &CommandContext) -> Result<()> {
let ctx = base_ctx.with_target(&args.server)?;
let printer = ctx.printer();
let printer = base_ctx.printer();
let (record, _run_id) = super::load_pr_record(&args.server, &args.run_id, base_ctx).await?;
let creds = super::load_github_credentials_required(base_ctx)?;
@ -24,7 +23,7 @@ pub(super) async fn close_command(args: PrCloseArgs, base_ctx: &CommandContext)
.map_err(|err| anyhow::anyhow!("{err}"))?;
info!(number = record.number, owner = %record.owner, repo = %record.repo, "Closed pull request");
if ctx.user_settings().cli.output.format == OutputFormat::Json {
if base_ctx.user_settings().cli.output.format == OutputFormat::Json {
print_json_pretty(&serde_json::json!({
"number": record.number,
"html_url": record.html_url,

View file

@ -7,8 +7,7 @@ use crate::command_context::CommandContext;
use crate::shared::print_json_pretty;
pub(super) async fn merge_command(args: PrMergeArgs, base_ctx: &CommandContext) -> Result<()> {
let ctx = base_ctx.with_target(&args.server)?;
let printer = ctx.printer();
let printer = base_ctx.printer();
let (record, _run_id) = super::load_pr_record(&args.server, &args.run_id, base_ctx).await?;
let creds = super::load_github_credentials_required(base_ctx)?;
@ -25,7 +24,7 @@ pub(super) async fn merge_command(args: PrMergeArgs, base_ctx: &CommandContext)
.map_err(|err| anyhow::anyhow!("{err}"))?;
info!(number = record.number, owner = %record.owner, repo = %record.repo, method = %args.method, "Merged pull request");
if ctx.user_settings().cli.output.format == OutputFormat::Json {
if base_ctx.user_settings().cli.output.format == OutputFormat::Json {
print_json_pretty(&serde_json::json!({
"number": record.number,
"html_url": record.html_url,

View file

@ -7,8 +7,7 @@ use crate::command_context::CommandContext;
use crate::shared::print_json_pretty;
pub(super) async fn view_command(args: PrViewArgs, base_ctx: &CommandContext) -> Result<()> {
let ctx = base_ctx.with_target(&args.server)?;
let printer = ctx.printer();
let printer = base_ctx.printer();
let (record, _run_id) = super::load_pr_record(&args.server, &args.run_id, base_ctx).await?;
let creds = super::load_github_credentials_required(base_ctx)?;
@ -25,7 +24,7 @@ pub(super) async fn view_command(args: PrViewArgs, base_ctx: &CommandContext) ->
info!(number = detail.number, owner = %record.owner, repo = %record.repo, "Viewing pull request");
if ctx.user_settings().cli.output.format == OutputFormat::Json {
if base_ctx.user_settings().cli.output.format == OutputFormat::Json {
print_json_pretty(&detail)?;
return Ok(());
}

View file

@ -7,7 +7,7 @@ use crate::command_context::CommandContext;
use crate::shared::print_json_pretty;
pub(crate) async fn run(args: SshArgs, base_ctx: &CommandContext) -> Result<()> {
if base_ctx.explicit_json_requested() && !args.print {
if !args.print {
base_ctx.require_no_json_override()?;
}