Skip to content

Commit

Permalink
update forc plugins to emit file names only, with option of full pa…
Browse files Browse the repository at this point in the history
…ths (FuelLabs#1381)

* emit file names by default, with option of full path

* pub use PluginsCommmand

* cargo fmt

* better handling of file_name

* Use expect for print_plugin + add panic assumptions comment

* Run forc-documenter for forc plugins

Co-authored-by: bing <[email protected]>
  • Loading branch information
bing and bing authored Apr 24, 2022
1 parent fe4f296 commit a48a7e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
12 changes: 9 additions & 3 deletions docs/src/forc/commands/forc_plugins.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# forc-plugins
Find all forc plugins available via `PATH`.

Prints the absolute path to each discovered plugin on a new line.
Prints information about each discovered plugin.


## USAGE:
forc plugins
forc plugins [OPTIONS]


## OPTIONS:

`-h`, `--help`


Print help information
Print help information


`-p`, `--paths`


Prints the absolute path to each discovered plugin
39 changes: 37 additions & 2 deletions forc/src/cli/commands/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
use crate::cli::PluginsCommand;
use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

/// Find all forc plugins available via `PATH`.
///
/// Prints information about each discovered plugin.
#[derive(Debug, Parser)]
pub struct Command {
/// Prints the absolute path to each discovered plugin.
#[clap(long = "paths", short = 'p')]
print_full_path: bool,
}

pub(crate) fn exec(command: PluginsCommand) -> Result<()> {
let PluginsCommand { print_full_path } = command;

pub(crate) fn exec() -> Result<()> {
for path in crate::cli::plugin::find_all() {
println!("{}", path.display());
print_plugin(path, print_full_path);
}
Ok(())
}

/// # Panics
///
/// This function assumes that file names will never be empty since it is only used with
/// paths yielded from plugin::find_all(), as well as that the file names are in valid
/// unicode format since file names should be prefixed with `forc-`. Should one of these 2
/// assumptions fail, this function panics.
fn print_plugin(path: PathBuf, print_full_path: bool) {
if print_full_path {
println!("{}", path.display());
} else {
println!(
"{}",
path.file_name()
.expect("Failed to read file name")
.to_str()
.expect("Failed to print file name")
);
}
}
8 changes: 3 additions & 5 deletions forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use deploy::Command as DeployCommand;
pub use init::Command as InitCommand;
pub use json_abi::Command as JsonAbiCommand;
use parse_bytecode::Command as ParseBytecodeCommand;
pub use plugins::Command as PluginsCommand;
pub use run::Command as RunCommand;
use test::Command as TestCommand;
pub use update::Command as UpdateCommand;
Expand Down Expand Up @@ -44,10 +45,7 @@ enum Forc {
Test(TestCommand),
Update(UpdateCommand),
JsonAbi(JsonAbiCommand),
/// Find all forc plugins available via `PATH`.
///
/// Prints the absolute path to each discovered plugin on a new line.
Plugins,
Plugins(PluginsCommand),
/// This is a catch-all for unknown subcommands and their arguments.
///
/// When we receive an unknown subcommand, we check for a plugin exe named
Expand All @@ -70,7 +68,7 @@ pub async fn run_cli() -> Result<()> {
Forc::Deploy(command) => deploy::exec(command).await,
Forc::Init(command) => init::exec(command),
Forc::ParseBytecode(command) => parse_bytecode::exec(command),
Forc::Plugins => plugins::exec(),
Forc::Plugins(command) => plugins::exec(command),
Forc::Run(command) => run::exec(command).await,
Forc::Test(command) => test::exec(command),
Forc::Update(command) => update::exec(command).await,
Expand Down

0 comments on commit a48a7e9

Please sign in to comment.