Skip to content
Open
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
18 changes: 15 additions & 3 deletions crates/socket-patch-cli/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,13 @@ pub async fn run(args: ApplyArgs) -> i32 {
match &vex_result {
Some(Ok(summary)) => {
env.vex = Some(VexSummary {
path: args.vex.vex.as_ref().unwrap().display().to_string(),
path: args
.vex
.vex
.as_ref()
.expect("vex_result is Some only when --vex was given")
.display()
.to_string(),
statements: summary.statements,
format: "openvex-0.2.0".to_string(),
});
Expand Down Expand Up @@ -829,7 +835,11 @@ pub async fn run(args: ApplyArgs) -> i32 {
println!(
"Wrote OpenVEX document with {} statement(s) to {}",
summary.statements,
args.vex.vex.as_ref().unwrap().display(),
args.vex
.vex
.as_ref()
.expect("vex_result is Some only when --vex was given")
.display(),
);
}
}
Expand Down Expand Up @@ -970,7 +980,9 @@ async fn apply_patches_inner(

// Resolve patch sources (read `.socket/` directly, or stage an overlay
// tempdir + download the gap). Shared with `vendor` via fetch_stage.
let socket_dir = manifest_path.parent().unwrap();
let socket_dir = manifest_path
.parent()
.expect("manifest path names a file, so it has a parent");
// Partition manifest PURLs by ecosystem up front. The source probes,
// the offline guard, and the download planner in `fetch_stage` must only
// consider patches this run can actually apply — the `--ecosystems`
Expand Down
28 changes: 20 additions & 8 deletions crates/socket-patch-cli/src/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ fn merge_metadata(record: &mut serde_json::Value, meta: serde_json::Value) {

/// Print a `serde_json::Value` as pretty JSON to stdout.
fn print_json(v: &serde_json::Value) {
println!("{}", serde_json::to_string_pretty(v).unwrap());
println!(
"{}",
serde_json::to_string_pretty(v).expect("serializing an in-memory JSON value cannot fail")
);
}

/// Truncate `s` to at most `limit` displayed characters, appending an
Expand Down Expand Up @@ -496,10 +499,11 @@ impl fmt::Display for IdentifierType {
}

fn detect_identifier_type(identifier: &str) -> Option<IdentifierType> {
let uuid_re =
Regex::new(r"(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$").unwrap();
let cve_re = Regex::new(r"(?i)^CVE-\d{4}-\d+$").unwrap();
let ghsa_re = Regex::new(r"(?i)^GHSA-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}$").unwrap();
let uuid_re = Regex::new(r"(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
.expect("hardcoded UUID regex must compile");
let cve_re = Regex::new(r"(?i)^CVE-\d{4}-\d+$").expect("hardcoded CVE regex must compile");
let ghsa_re = Regex::new(r"(?i)^GHSA-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}$")
.expect("hardcoded GHSA regex must compile");

if uuid_re.is_match(identifier) {
Some(IdentifierType::Uuid)
Expand Down Expand Up @@ -635,7 +639,7 @@ pub(crate) fn select_patches(
"purl": purl,
"options": options_json,
}))
.unwrap()
.expect("serializing an in-memory JSON value cannot fail")
);
return Err(1);
}
Expand Down Expand Up @@ -1840,7 +1844,11 @@ pub async fn run(args: GetArgs) -> i32 {
let (code, result_json) = download_and_apply_patches(&selected, &params).await;

if args.common.json {
println!("{}", serde_json::to_string_pretty(&result_json).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result_json)
.expect("serializing an in-memory JSON value cannot fail")
);
}

code
Expand Down Expand Up @@ -2083,7 +2091,11 @@ async fn save_and_apply_patch(args: &GetArgs, patch: &PatchResponse) -> i32 {
if !warnings.is_empty() {
result_json["warnings"] = serde_json::json!(warnings);
}
println!("{}", serde_json::to_string_pretty(&result_json).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result_json)
.expect("serializing an in-memory JSON value cannot fail")
);
}

exit_code
Expand Down
4 changes: 3 additions & 1 deletion crates/socket-patch-cli/src/commands/repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ async fn repair_inner(
.await
.map_err(|e| e.to_string())?;

let socket_dir = manifest_path.parent().unwrap();
let socket_dir = manifest_path
.parent()
.expect("manifest path names a file, so it has a parent");
let blobs_path = socket_dir.join("blobs");
let diffs_path = socket_dir.join("diffs");
let packages_path = socket_dir.join("packages");
Expand Down
16 changes: 10 additions & 6 deletions crates/socket-patch-cli/src/commands/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pub async fn run(args: RollbackArgs) -> i32 {
"status": "error",
"error": msg,
}))
.unwrap()
.expect("serializing an in-memory JSON value cannot fail")
);
} else {
eprintln!("Error: {msg}");
Expand All @@ -274,7 +274,7 @@ pub async fn run(args: RollbackArgs) -> i32 {
"error": "Manifest not found",
"path": manifest_path.display().to_string(),
}))
.unwrap()
.expect("serializing an in-memory JSON value cannot fail")
);
} else {
// Errors print even under --silent ("errors only", never
Expand Down Expand Up @@ -331,7 +331,7 @@ pub async fn run(args: RollbackArgs) -> i32 {
"vendored": vendored,
"results": results.iter().map(result_to_json).collect::<Vec<_>>(),
}))
.unwrap()
.expect("serializing an in-memory JSON value cannot fail")
);
} else if !args.common.silent && !results.is_empty() {
let rolled_back: Vec<_> = results
Expand Down Expand Up @@ -458,7 +458,7 @@ pub async fn run(args: RollbackArgs) -> i32 {
"vendored": [],
"results": [],
}))
.unwrap()
.expect("serializing an in-memory JSON value cannot fail")
);
} else {
// Errors print even under --silent ("errors only", never
Expand All @@ -479,7 +479,9 @@ async fn rollback_patches_inner(
.map_err(|e| e.to_string())?
.ok_or_else(|| "Invalid manifest".to_string())?;

let socket_dir = manifest_path.parent().unwrap();
let socket_dir = manifest_path
.parent()
.expect("manifest path names a file, so it has a parent");
let mut blobs_path = socket_dir.join("blobs");
// `--dry-run` must not mutate `.socket/` ("Preview, no mutations"):
// don't create the blobs dir; a throwaway stage replaces it below.
Expand All @@ -495,7 +497,9 @@ async fn rollback_patches_inner(
if args.identifier.is_some() {
return Err(format!(
"No patch found matching identifier: {}",
args.identifier.as_deref().unwrap()
args.identifier
.as_deref()
.expect("is_some checked by the enclosing if")
));
}
if !args.common.silent && !args.common.json {
Expand Down
20 changes: 16 additions & 4 deletions crates/socket-patch-cli/src/commands/scan/hosted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ fn emit_json_error(scan_result: Option<serde_json::Value>, message: &str) {
if !result.get("redirect").is_some_and(|r| r.is_object()) {
result["redirect"] = serde_json::json!({ "mode": "hosted" });
}
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
}

/// Build the hosted `--json` success envelope: the classic scan object
Expand Down Expand Up @@ -670,7 +674,7 @@ pub(super) async fn run_redirect(
let mut result = build_redirect_json_envelope(scan_result.take(), redirect);
if let Some(statements) = vex_statements {
result["vex"] = serde_json::json!({
"path": args.vex.vex.as_ref().unwrap().display().to_string(),
"path": args.vex.vex.as_ref().expect("vex_statements is Some only when --vex was given").display().to_string(),
"statements": statements,
"format": "openvex-0.2.0",
"verified": false,
Expand All @@ -679,7 +683,11 @@ pub(super) async fn run_redirect(
result["status"] = serde_json::json!("error");
result["error"] = serde_json::json!({ "code": code, "message": message });
}
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
} else {
if !args.common.silent {
let verb = if args.common.dry_run {
Expand Down Expand Up @@ -729,7 +737,11 @@ pub(super) async fn run_redirect(
install time; run `socket-patch vex` after installing to verify against \
the installed tree).",
statements,
args.vex.vex.as_ref().unwrap().display(),
args.vex
.vex
.as_ref()
.expect("vex_statements is Some only when --vex was given")
.display(),
);
} else if args.vex.vex.is_some() && args.common.dry_run {
eprintln!("Skipping VEX generation (--dry-run).");
Expand Down
43 changes: 35 additions & 8 deletions crates/socket-patch-cli/src/commands/scan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ async fn embed_vex_into_json(
match generate_vex_from_manifest_path(common, &params, manifest_path).await {
Ok(summary) => {
result["vex"] = serde_json::json!({
"path": vex_args.vex.as_ref().unwrap().display().to_string(),
"path": vex_args.vex.as_ref().expect("--vex is Some: guarded by the early return above").display().to_string(),
"statements": summary.statements,
"format": "openvex-0.2.0",
});
Expand Down Expand Up @@ -337,7 +337,11 @@ async fn embed_vex_human(
println!(
"Wrote OpenVEX document with {} statement(s) to {}",
summary.statements,
vex_args.vex.as_ref().unwrap().display(),
vex_args
.vex
.as_ref()
.expect("--vex is Some: guarded by the early return above")
.display(),
);
}
0
Expand Down Expand Up @@ -407,7 +411,11 @@ async fn discover_selected(
fn emit_discovery_error_json(result: &mut serde_json::Value, message: &str) {
result["status"] = serde_json::json!("error");
result["error"] = serde_json::json!(message);
println!("{}", serde_json::to_string_pretty(result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(result)
.expect("serializing an in-memory JSON value cannot fail")
);
}

/// The `DownloadParams` every scan-driven download shares. Only the output
Expand Down Expand Up @@ -680,7 +688,11 @@ pub async fn run(mut args: ScanArgs) -> i32 {
"packages": [],
"updates": [],
});
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
} else {
eprintln!("Error: {err}");
}
Expand All @@ -707,7 +719,10 @@ pub async fn run(mut args: ScanArgs) -> i32 {
// `--vex` side-effect reads the manifest at several terminal returns,
// including the early "no packages" exit before the GC block.
let manifest_path = args.common.resolved_manifest_path();
let socket_dir = manifest_path.parent().unwrap().to_path_buf();
let socket_dir = manifest_path
.parent()
.expect("manifest path names a file, so it has a parent")
.to_path_buf();

let overrides = args.common.api_client_overrides();
let (mut api_client, mut use_public_proxy) =
Expand Down Expand Up @@ -860,7 +875,11 @@ pub async fn run(mut args: ScanArgs) -> i32 {
}
let code =
embed_vex_into_json(&args.common, &args.vex, &manifest_path, 0, &mut result).await;
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
return code;
} else if args.common.silent {
// Errors only: the empty-scan hint is informational.
Expand Down Expand Up @@ -1021,7 +1040,11 @@ pub async fn run(mut args: ScanArgs) -> i32 {
"packages": [],
"updates": [],
});
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
} else {
eprintln!("Error: all {total_batches} API batch queries failed: {err}");
}
Expand Down Expand Up @@ -1350,7 +1373,11 @@ pub async fn run(mut args: ScanArgs) -> i32 {
&mut result,
)
.await;
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
return final_code;
}

Expand Down
18 changes: 15 additions & 3 deletions crates/socket-patch-cli/src/commands/scan/vendor_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ async fn run_vendor_json_path(
if args.vex.vex.is_some() {
result["vex"] = serde_json::json!({ "skipped": true, "reason": "dry_run" });
}
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
return 0;
}

Expand Down Expand Up @@ -396,7 +400,11 @@ async fn run_vendor_json_path(
"code": code,
"message": message,
});
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
return 1;
}
};
Expand All @@ -406,7 +414,11 @@ async fn run_vendor_json_path(

let final_code =
embed_vex_into_json(&args.common, &args.vex, manifest_path, vendor_code, result).await;
println!("{}", serde_json::to_string_pretty(&result).unwrap());
println!(
"{}",
serde_json::to_string_pretty(&result)
.expect("serializing an in-memory JSON value cannot fail")
);
final_code
}

Expand Down
Loading