Skip to content

Accept local package-lock.json as target #1116

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
38 changes: 34 additions & 4 deletions hipcheck/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::{
shell::{color_choice::ColorChoice, verbosity::Verbosity},
source,
target::{
pm, LocalGitRepo, MavenPackage, Package, PackageHost, Sbom, SbomStandard, SingleTargetSeed,
SingleTargetSeedKind, TargetSeed, TargetSeedKind, TargetType, ToTargetSeed,
ToTargetSeedKind, VcsUrl,
pm, LocalGitRepo, MavenPackage, MultiTargetSeed, MultiTargetSeedKind, Package, PackageHost,
Sbom, SbomStandard, SingleTargetSeed, SingleTargetSeedKind, TargetSeed, TargetSeedKind,
TargetType, ToTargetSeed, ToTargetSeedKind, VcsUrl,
},
};
use clap::{Parser as _, ValueEnum};
Expand Down Expand Up @@ -651,7 +651,13 @@ impl ToTargetSeed for CheckArgs {

Ok(TargetSeed::Single(seed))
}
TargetSeedKind::Multi(_multi_target_seed_kind) => todo!(),
TargetSeedKind::Multi(multi_target_seed_kind) => {
let seed = MultiTargetSeed {
kind: multi_target_seed_kind,
specifier: command.get_specifier().to_string(),
};
Ok(TargetSeed::Multi(seed))
}
}
}
}
Expand All @@ -673,6 +679,9 @@ pub enum CheckCommand {
/// Analyze packages specified in an SBOM document
#[command(hide = true)]
Sbom(CheckSbomArgs),
/// Analyze npm dependencies specified in an package-lock.json document
#[command(hide = true)]
PackageLockJson(CheckPackageLockJsonArgs),
}

impl CheckCommand {
Expand All @@ -684,6 +693,7 @@ impl CheckCommand {
Pypi(args) => &args.package,
Repo(args) => &args.source,
Sbom(args) => &args.path,
PackageLockJson(args) => &args.path,
}
}
}
Expand All @@ -696,6 +706,7 @@ impl ToTargetSeedKind for CheckCommand {
CheckCommand::Pypi(args) => args.to_target_seed_kind(),
CheckCommand::Repo(args) => args.to_target_seed_kind(),
CheckCommand::Sbom(args) => args.to_target_seed_kind(),
CheckCommand::PackageLockJson(args) => args.to_target_seed_kind(),
}
}
}
Expand Down Expand Up @@ -887,6 +898,24 @@ impl ToTargetSeedKind for CheckSbomArgs {
}
}

#[derive(Debug, Clone, clap::Args)]
pub struct CheckPackageLockJsonArgs {
/// package-lock.json to analyze
pub path: String,
}

impl ToTargetSeedKind for CheckPackageLockJsonArgs {
fn to_target_seed_kind(&self) -> Result<TargetSeedKind> {
let path = PathBuf::from(&self.path);
if path.exists() && self.path.ends_with("package-lock.json") {
return Ok(TargetSeedKind::Multi(MultiTargetSeedKind::PackageLockJson(
path,
)));
}
Err(hc_error!("The provided package-lock.json does not exist"))
}
}

#[derive(Debug, Clone, clap::Args)]
pub struct SchemaArgs {
#[clap(subcommand)]
Expand Down Expand Up @@ -1616,6 +1645,7 @@ mod tests {
CheckCommand::Pypi(args) => args.package,
CheckCommand::Repo(args) => args.source,
CheckCommand::Sbom(args) => args.path,
CheckCommand::PackageLockJson(args) => args.path,
}
}

Expand Down
5 changes: 5 additions & 0 deletions hipcheck/src/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub enum TargetType {
Repo,
Request,
Sbom,
#[serde(rename = "package-lock-json")]
PackageLockJson,
}

impl TargetType {
Expand Down Expand Up @@ -96,6 +98,9 @@ impl TargetType {
|| tgt.ends_with(".cdx.xml")
{
Some((Sbom, tgt.to_string()))
// Otherwise check if it is a package-lock.json for a local npm project
} else if tgt.ends_with("package-lock.json") {
Some((PackageLockJson, tgt.to_string()))
// If is path to a file/dir that exists, treat as a local Repo
} else if PathBuf::from(tgt).exists() {
Some((Repo, tgt.to_string()))
Expand Down
16 changes: 15 additions & 1 deletion hipcheck/src/target/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,25 @@ impl Display for SingleTargetSeedKind {
}
}

impl Display for MultiTargetSeedKind {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
use MultiTargetSeedKind::*;
match self {
GoMod(path) => {
write!(f, "go.mod file at {}", path.display())
}
PackageLockJson(path) => {
write!(f, "package-lock.json file at {}", path.display())
}
}
}
}

impl Display for TargetSeed {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
TargetSeed::Single(x) => x.kind.fmt(f),
TargetSeed::Multi(_x) => unimplemented!(),
TargetSeed::Multi(x) => x.kind.fmt(f),
}
}
}