Skip to content

Commit

Permalink
Updated to add --requests-file and make required REQUESTS arguments o…
Browse files Browse the repository at this point in the history
…ptional.

Signed-off-by: David Gilligan-Cook <[email protected]>
  • Loading branch information
dcookspi committed Sep 24, 2024
1 parent c7f609f commit e56c9f2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/spk-cli/cmd-env/src/cmd_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Env {
pub formatter_settings: flags::DecisionFormatterSettings,

/// The requests to resolve and run
#[clap(name = "REQUESTS|REQUESTS_YAML_FILE")]
#[clap(name = "REQUESTS")]
pub requested: Vec<String>,

/// An optional command to run in the resolved environment.
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-cli/cmd-explain/src/cmd_explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Explain {
pub formatter_settings: flags::DecisionFormatterSettings,

/// The requests to resolve
#[clap(name = "REQUESTS|REQUESTS_YAML_FILE", required = true)]
#[clap(name = "REQUESTS")]
pub requested: Vec<String>,

// The following arguments were previously provided by the `runtime` field.
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-cli/cmd-install/src/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Install {
pub formatter_settings: flags::DecisionFormatterSettings,

/// The packages to install
#[clap(name = "PKG", required = true)]
#[clap(name = "PKG")]
pub packages: Vec<String>,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/spk-cli/cmd-render/src/cmd_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Render {
pub formatter_settings: flags::DecisionFormatterSettings,

/// The packages to resolve and render
#[clap(name = "PKG", required = true)]
#[clap(name = "PKG")]
packages: Vec<String>,

/// The empty directory to render into
Expand Down
60 changes: 35 additions & 25 deletions crates/spk-cli/common/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ pub struct Requests {
/// Allow pre-releases for all command line package requests
#[clap(long)]
pub pre: bool,

/// Specify package requests from a yaml file
#[clap(long, value_hint = ValueHint::FilePath)]
pub requests_file: Vec<std::path::PathBuf>,
}

impl Requests {
Expand Down Expand Up @@ -419,43 +423,49 @@ impl Requests {
let mut out = Vec::<Request>::new();
let options = options.get_options()?;

for r in requests.into_iter() {
let r: &str = r.as_ref();

// Is it a filepath to a package requests yaml file?
if r.ends_with(".yaml") {
//let filename: std::path::PathBuf = r.into();
let reader = std::fs::File::open(r)
.into_diagnostic()
.wrap_err_with(|| format!("Failed to open: {r}"))?;
let requests_from_file: Vec<String> = serde_yaml::from_reader(reader)
.into_diagnostic()
.wrap_err_with(|| {
format!("Failed to parse as list of package requests: {r}")
})?;
for req in requests_from_file {
let new_requests = self.parse_non_file_request(&req, &options, repos).await?;
out.extend(new_requests);
}
continue;
// First, any --requests-file filepaths that specified. These
// can contain a list of arbitrary package requests, including
// 'file@stage' requests.
for filename in &self.requests_file {
let reader = std::fs::File::open(filename)
.into_diagnostic()
.wrap_err_with(|| format!("Failed to open: {filename:?}"))?;
let requests_from_file: Vec<String> = serde_yaml::from_reader(reader)
.into_diagnostic()
.wrap_err_with(|| {
format!("Failed to parse as list of package requests: {filename:?}")
})?;
for req in requests_from_file {
let new_requests = self.parse_cli_request(&req, &options, repos).await?;
out.extend(new_requests);
}
}

// It's not a filepath to a package requests yaml file
let reqs = self.parse_non_file_request(r, &options, repos).await?;
// From the positional REQUESTS arg
for r in requests.into_iter() {
let r: &str = r.as_ref();
let reqs = self.parse_cli_request(r, &options, repos).await?;
out.extend(reqs);
}

Ok(out)
if out.is_empty() {
Err(Error::String(
"Needs at least one request: Missing required argument <REQUESTS> ... ".to_string(),
)
.into())
} else {
Ok(out)
}
}

async fn parse_non_file_request(
async fn parse_cli_request(
&self,
request: &str,
options: &OptionMap,
repos: &[Arc<storage::RepositoryHandle>],
) -> Result<Vec<Request>> {
// Parses a string that isn't a path to a request file into
// one or more requests.
// Parses a command line request into one or more requests.
// 'file@stage' strings can expand into more than one request.
let mut out = Vec::<Request>::new();

// If this a request with a '@' stage-specifier, it may result
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-cli/group1/src/cmd_bake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct Bake {
pub formatter_settings: flags::DecisionFormatterSettings,

/// The requests to resolve and bake
#[clap(name = "REQUESTS|REQUESTS_YAML_FILE")]
#[clap(name = "REQUESTS")]
pub requested: Vec<String>,
}

Expand Down

0 comments on commit e56c9f2

Please sign in to comment.