Skip to content

Commit

Permalink
Adds --no-src and --src options to spk ls and spk search. (#1167)
Browse files Browse the repository at this point in the history
These options exclude, or include respectively, the /src builds when
considering packages, versions, and builds for listings or searches.

The --host option enables --no-src by default. This can be overridden
by using --src.

Signed-off-by: David Gilligan-Cook <[email protected]>
  • Loading branch information
dcookspi authored Jan 14, 2025
1 parent 754a98b commit 2eef8f1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
38 changes: 37 additions & 1 deletion crates/spk-cli/group2/src/cmd_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,22 @@ pub struct Ls<Output: Default = Console> {

/// Enable filtering to only show items that have a build that
/// matches the current host's host options. This option can be
/// configured as the default in spk's config file.
/// configured as the default in spk's config file. Enables
/// --no-src by default.
#[clap(long)]
host: bool,

/// Disable showing items that have any matching build and only
/// show items with a non-src build that matches the current
/// host's host options. Using --host will enable this by default.
#[clap(long, conflicts_with = "src")]
no_src: bool,

/// Enable filtering to show items that have any build, including
/// src ones, that match the current host's host options.
#[clap(long)]
src: bool,

/// Given a name, list versions. Given a name/version list builds.
///
/// If nothing is provided, list all available packages.
Expand Down Expand Up @@ -106,6 +118,11 @@ impl<T: Output> Run for Ls<T> {
// Set the default filter to the all current host's host
// options (--host). --no-host will disable this.
let filter_by = if !self.no_host && self.host {
// Using --host enables --no-src by default. But using
// --src overrides that.
if !self.src {
self.no_src = true;
}
get_host_options_filters()
} else {
None
Expand Down Expand Up @@ -183,6 +200,10 @@ impl<T: Output> Run for Ls<T> {
let mut any_deprecated = false;
let mut any_not_deprecated = false;
while let Some(build) = builds.pop() {
if self.no_src && build.is_source() {
// Filter out source builds
continue;
}
match repo.read_package(&build).await {
Ok(spec) => {
if !spec.matches_all_filters(&filter_by) {
Expand Down Expand Up @@ -244,6 +265,11 @@ impl<T: Output> Run for Ls<T> {
let pkg = parse_ident(package)?;
for (_, repo) in repos {
for build in repo.list_package_builds(pkg.as_version_ident()).await? {
if self.no_src && build.is_source() {
// Filter out source builds
continue;
}

// Doing this here slows the listing down, but
// the spec file is the only place that holds
// the deprecation status.
Expand Down Expand Up @@ -364,6 +390,11 @@ impl<T: Output> Ls<T> {
}
}

if self.no_src && build.is_source() {
// Filter out source builds
continue;
}

// Doing this here slows the listing down, but
// the spec file is the only place that holds
// the deprecation status.
Expand Down Expand Up @@ -432,6 +463,11 @@ impl<T: Output> Ls<T> {
for pkg in versions {
let mut found_a_match = false;
for build in repo.list_package_builds(pkg.as_version_ident()).await? {
if self.no_src && build.is_source() {
// Filter out source builds
continue;
}

let spec = match repo.read_package(&build).await {
Ok(spec) => spec,
Err(err) => {
Expand Down
29 changes: 28 additions & 1 deletion crates/spk-cli/group4/src/cmd_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,22 @@ pub struct Search {

/// Enable filtering to only show items that have a build that
/// matches the current host's host options. This option can be
/// configured as the default in spk's config file.
/// configured as the default in spk's config file. Enables
/// --no-src by default.
#[clap(long)]
host: bool,

/// Disable showing items that have any matching build and only
/// show items with a non-src build that matches the current
/// host's host options. Using --host will enable this by default.
#[clap(long, conflicts_with = "src")]
no_src: bool,

/// Enable filtering to show items that have any build, including
/// src ones, that match the current host's host options.
#[clap(long)]
src: bool,

/// The text/substring to search for in package names
term: String,
}
Expand All @@ -56,6 +68,11 @@ impl Run for Search {
// Set the default filter to the all current host's host
// options (--host). --no-host will disable this.
let filter_by = if !self.no_host && self.host {
// Using --host enables --no-src by default. But using
// --src overrides that.
if !self.src {
self.no_src = true;
}
get_host_options_filters()
} else {
None
Expand Down Expand Up @@ -98,6 +115,11 @@ impl Run for Search {
// there's one that matches the filters
let mut has_a_build_that_matches_the_filter = false;
for build in builds {
if self.no_src && build.is_source() {
// Filter out source builds
continue;
}

if let Ok(spec) = repo.read_package(&build).await {
if spec.matches_all_filters(&filter_by) {
has_a_build_that_matches_the_filter = true;
Expand All @@ -119,6 +141,11 @@ impl Run for Search {
// that only exists as embedded builds.
let mut all_builds_deprecated = true;
for build in builds {
if self.no_src && build.is_source() {
// Filter out source builds
continue;
}

if let Ok(spec) = repo.read_package(&build).await {
if !spec.is_deprecated() {
if !spec.matches_all_filters(&filter_by) {
Expand Down

0 comments on commit 2eef8f1

Please sign in to comment.