Plugins

Plugins can be used to extend forc with new commands that go beyond the native commands mentioned in the previous chapter. While the Fuel ecosystem provides a few commonly useful plugins (forc-fmt, forc-lsp, forc-explore), anyone can write their own!

Let's install a starter plugin, forc-gm, and take a look at how it works underneath:

cargo install forc-gm

Check that we have installed forc-gm:

$ forc plugins
/Users/<USER>/.cargo/bin/forc-gm

Underneath, forc-gm is a simple CLI app, with clap as the only dependency:

//! A sample `forc` plugin that greets you!
//!
//! Once installed and available via `PATH`, can be executed via `forc gm`.

use clap::Parser;

#[derive(Debug, Parser)]
#[clap(
    name = "forc-gm",
    about = "Sample Forc plugin that greets you!",
    version
)]
struct App {
    #[clap(subcommand)]
    pub subcmd: Option<Subcommand>,
}

#[derive(Debug, Parser)]
enum Subcommand {
    /// Say 'gm' to Fuel!
    Fuel,
}

fn main() {
    let app = App::parse();

    match app.subcmd {
        Some(Subcommand::Fuel) => greet_fuel(),
        None => greet(),
    }
}

fn greet_fuel() {
    println!("gn from Fuel!");
}

fn greet() {
    println!("gn!");
}

You can say gm, or you can greet Fuel:

$ forc gm
gn!
$ forc gm fuel
gn from Fuel!

Writing your own plugin

We encourage anyone to write and publish their own forc plugin to enhance their development experience.

Your plugin must be named in the format forc-<MY_PLUGIN> and you may use the above template as a starting point. You can use clap and add more subcommands, options and configurations to suit your plugin's needs.