Skip to content

Commit

Permalink
provisioning: Implement find_disk
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <[email protected]>
  • Loading branch information
ikeycode committed Feb 3, 2025
1 parent 7d7e11f commit 094061a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions crates/provisioning/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ mod find_disk;
pub enum Command {
CreatePartition,
CreatePartitionTable(Box<create_partition_table::Command>),
FindDisk,
FindDisk(Box<find_disk::Command>),
}

/// Command execution function
type CommandExec = for<'a> fn(Context<'a>) -> Result<Command, crate::Error>;

/// Map of command names to functions
static COMMANDS: phf::Map<&'static str, CommandExec> = phf::phf_map! {
//"find-disk" => find_disk::parse,
"find-disk" => find_disk::parse,
//"create-partition" => create_partition::parse,
"create-partition-table" => create_partition_table::parse,
};
Expand Down
39 changes: 36 additions & 3 deletions crates/provisioning/src/commands/find_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,43 @@
//
// SPDX-License-Identifier: MPL-2.0

use super::Command;
use itertools::Itertools;

use crate::Context;

#[derive(Debug)]
pub struct Command {
pub name: String,
}

/// Generate a command to find a disk
pub(crate) fn parse(_context: Context<'_>) -> Result<Command, crate::Error> {
unimplemented!("Command not implemented");
pub(crate) fn parse(context: Context<'_>) -> Result<super::Command, crate::Error> {
let arguments = context
.node
.entries()
.iter()
.filter(|e| e.is_empty() || e.name().is_none())
.collect_vec();

let name = match arguments.len() {
0 => {
return Err(crate::InvalidArguments {
at: context.node.span(),
advice: Some("find-disk <name> - provide a name for the storage device".into()),
}
.into())
}
1 => arguments[0].value().as_string().ok_or(crate::InvalidType {
at: arguments[0].span(),
})?,
_ => {
return Err(crate::InvalidArguments {
at: context.node.span(),
advice: Some("find-disk <name> - only one positional argument supported".into()),
}
.into())
}
};

Ok(super::Command::FindDisk(Box::new(Command { name: name.to_owned() })))
}
16 changes: 16 additions & 0 deletions crates/provisioning/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub enum Error {
#[error("unknown variant")]
UnknownVariant,

#[diagnostic(transparent)]
#[error(transparent)]
InvalidArguments(#[from] InvalidArguments),

#[diagnostic(transparent)]
#[error(transparent)]
InvalidType(#[from] InvalidType),
Expand Down Expand Up @@ -97,3 +101,15 @@ pub struct UnsupportedValue {
#[help]
pub advice: Option<String>,
}

/// Error for invalid arguments
#[derive(Debug, Diagnostic, Error)]
#[error("invalid arguments")]
#[diagnostic(severity(error))]
pub struct InvalidArguments {
#[label]
pub at: SourceSpan,

#[help]
pub advice: Option<String>,
}

0 comments on commit 094061a

Please sign in to comment.