Skip to content

Commit

Permalink
[fud2] Add list subcommand (#1937)
Browse files Browse the repository at this point in the history
This new command just prints a list of all the registered states and
operations, in case you forget a name or whatever.
  • Loading branch information
sampsyo authored Feb 29, 2024
1 parent 897914c commit 064206b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fud2/fud-core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ pub struct GetResource {
output: Option<Utf8PathBuf>,
}

/// list the available states and ops
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "list")]
pub struct ListCommand {}

/// supported subcommands
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
Expand All @@ -73,6 +78,9 @@ pub enum Subcommand {

/// extract a resource file
GetResource(GetResource),

/// list the available states and ops
List(ListCommand),
}

#[derive(FromArgs)]
Expand Down Expand Up @@ -246,6 +254,10 @@ pub fn cli(driver: &Driver) -> anyhow::Result<()> {
Some(Subcommand::GetResource(cmd)) => {
return get_resource(driver, cmd);
}
Some(Subcommand::List(_)) => {
driver.print_info();
return Ok(());
}
None => {}
}

Expand Down
32 changes: 32 additions & 0 deletions fud2/fud-core/src/exec/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ impl Driver {
}
}

/// Concot a plan to carry out the requested build.
///
/// This works by searching for a path through the available operations from the input state
/// to the output state. If no such path exists in the operation graph, we return None.
pub fn plan(&self, req: Request) -> Option<Plan> {
// Find a path through the states.
let path =
Expand Down Expand Up @@ -164,6 +168,9 @@ impl Driver {
})
}

/// Infer the state of a file based on its extension.
///
/// Multiple states can use the same extension. The first state registered "wins."
pub fn guess_state(&self, path: &Utf8Path) -> Option<StateRef> {
let ext = path.extension()?;
self.states
Expand All @@ -172,13 +179,15 @@ impl Driver {
.map(|(state, _)| state)
}

/// Look up a state by its name.
pub fn get_state(&self, name: &str) -> Option<StateRef> {
self.states
.iter()
.find(|(_, state_data)| state_data.name == name)
.map(|(state, _)| state)
}

/// Look an operation by its name.
pub fn get_op(&self, name: &str) -> Option<OpRef> {
self.ops
.iter()
Expand All @@ -190,6 +199,29 @@ impl Driver {
pub fn default_workdir(&self) -> Utf8PathBuf {
format!(".{}", &self.name).into()
}

/// Print a list of registered states and operations to stdout.
pub fn print_info(&self) {
println!("States:");
for (_, state) in self.states.iter() {
print!(" {}:", state.name);
for ext in &state.extensions {
print!(" .{}", ext);
}
println!();
}

println!();
println!("Operations:");
for (_, op) in self.ops.iter() {
println!(
" {}: {} -> {}",
op.name,
self.states[op.input].name,
self.states[op.output].name
);
}
}
}

pub struct DriverBuilder {
Expand Down

0 comments on commit 064206b

Please sign in to comment.