Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
enhancement: pull remote ABI subcommand (#775)
Browse files Browse the repository at this point in the history
* initial structure and ops logic

* cleanup

* cargo fix

* cargo fix

* Revert "cleanup"

This reverts commit 069519e.

* cargo fmt

* undo cargo fix

* use optional contract_name arg, cleanup

* undo cargo fix change

* undo cargo fic

* swap fuels in indexers

* fix defaults

* update docs

* integrate ra0x3 feedback

* update doc

* deekerno feedback
  • Loading branch information
0xmovses authored May 11, 2023
1 parent a0b7b62 commit ef7a380
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 4 deletions.
21 changes: 21 additions & 0 deletions docs/src/reference-guide/plugins/forc-index/pull-abi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# `forc index pull-abi`

Pull an abi file from a raw GitHub URL.

```bash
forc index pull-abi --raw-url https://raw.githubusercontent.com/rvmelkonian/sway-escrow/main/out/debug/escrow-contract-abi.json
```

```text
USAGE:
forc-index pull-abi [OPTIONS] --raw-url <RAW_URL>
OPTIONS:
--contract-name <CONTRACT_NAME> Name of contract.
-h, --help Print help information
-p, --path <PATH> Path at which to write the ABI.
--url <URL> URL of the ABI file.
-v, --verbose Enable verbose output.
--with-abi <WITH_ABI> Only pull the ABI for the given contract.
--with-contract <WITH_CONTRACT> Pull the full contract code including the abi.
```
Empty file.
8 changes: 5 additions & 3 deletions plugins/forc-index/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pub(crate) use crate::commands::{
auth::Command as AuthCommand, build::Command as BuildCommand,
check::Command as CheckCommand, deploy::Command as DeployCommand,
init::Command as InitCommand, kill::Command as KillCommand,
new::Command as NewCommand, remove::Command as RemoveCommand,
revert::Command as RevertCommand, start::Command as StartCommand,
welcome::Command as WelcomeCommand,
new::Command as NewCommand, pull_abi::Command as PullAbiCommand,
remove::Command as RemoveCommand, revert::Command as RevertCommand,
start::Command as StartCommand, welcome::Command as WelcomeCommand,
};
use clap::{Parser, Subcommand};
use forc_postgres::{
Expand Down Expand Up @@ -34,6 +34,7 @@ pub enum ForcIndex {
Build(BuildCommand),
Auth(AuthCommand),
Postgres(ForcPostgresOpt),
PullAbi(PullAbiCommand),
Kill(KillCommand),
//Welcome(WelcomeCommand),
}
Expand All @@ -54,6 +55,7 @@ pub async fn run_cli() -> Result<(), anyhow::Error> {
ForcIndex::Remove(command) => crate::commands::remove::exec(command),
ForcIndex::Revert(command) => crate::commands::revert::exec(command).await,
ForcIndex::Build(command) => crate::commands::build::exec(command),
ForcIndex::PullAbi(command) => crate::commands::pull_abi::exec(command).await,
//ForcIndex::Welcome(command) => crate::commands::welcome::exec(command).await,
ForcIndex::Auth(command) => crate::commands::auth::exec(command),
ForcIndex::Postgres(opt) => match opt.command {
Expand Down
2 changes: 1 addition & 1 deletion plugins/forc-index/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Command {
#[clap(long, help = "Name of indexer.")]
pub name: Option<String>,

/// Path at which to create indexer
/// Path at which to create indexer.
#[clap(
short,
long,
Expand Down
1 change: 1 addition & 0 deletions plugins/forc-index/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod deploy;
pub mod init;
pub mod kill;
pub mod new;
pub mod pull_abi;
pub mod remove;
pub mod revert;
pub mod start;
Expand Down
41 changes: 41 additions & 0 deletions plugins/forc-index/src/commands/pull_abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::ops::forc_index_pull_abi;
use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

#[derive(Debug, Parser)]
pub struct Command {
/// URL of the ABI file.
#[clap(long, help = "URL of the ABI file.")]
pub url: String,

/// Only pull the ABI for the given contract.
#[clap(long, help = "Only pull the ABI for the given contract.")]
pub with_abi: Option<bool>,

/// Pull the full contract code including the abi.
#[clap(long, help = "Pull the full contract code including the abi.")]
pub with_contract: Option<bool>,

/// Name of contract.
#[clap(long, help = "Name of contract.")]
pub contract_name: Option<String>,

/// Path at which to write the ABI.
#[clap(
short,
long,
parse(from_os_str),
help = "Path at which to write the ABI."
)]
pub path: Option<PathBuf>,

/// Enable verbose output.
#[clap(short, long, help = "Enable verbose output.")]
pub verbose: bool,
}

pub async fn exec(command: Command) -> Result<(), anyhow::Error> {
forc_index_pull_abi::init(command).await?;
Ok(())
}
55 changes: 55 additions & 0 deletions plugins/forc-index/src/ops/forc_index_pull_abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::cli::PullAbiCommand;
use anyhow::anyhow;
use reqwest::Url;
use std::{fs::File, io::prelude::*};
use tracing::{error, info};

pub async fn init(command: PullAbiCommand) -> anyhow::Result<()> {
let PullAbiCommand {
url,
with_abi,
with_contract,
contract_name,
path,
verbose,
..
} = command;

let url = Url::parse(&url)?;
let client = reqwest::Client::new();
let response = client.get(url.clone()).send().await?;
let content = response.text().await?;

let file_name = match contract_name {
Some(name) => format!("{}-abi.json", name),
None => url
.path_segments()
.ok_or(anyhow!("Invalid URL path"))?
.last()
.ok_or(anyhow!("Invalid URL path"))?
.to_owned(),
};

if with_contract.unwrap_or(false) && with_abi.unwrap_or(false) {
error!("Cannot use both --with-contract and --with-abi, please choose one");
}

if with_contract.unwrap_or(false) {
unimplemented!();
}

if with_abi.unwrap_or(true) {
let output_dir = path.unwrap_or(std::env::current_dir()?);
let file_path = output_dir.join(file_name);
let mut file = File::create(&file_path)?;
file.write_all(content.as_bytes())?;

if verbose {
info!("ABI file saved to: {:?}", file_path);
}

info!("✅ ABI file saved");
}

Ok(())
}
1 change: 1 addition & 0 deletions plugins/forc-index/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod forc_index_deploy;
pub mod forc_index_init;
pub mod forc_index_kill;
pub mod forc_index_new;
pub mod forc_index_pull_abi;
pub mod forc_index_remove;
pub mod forc_index_revert;
pub mod forc_index_start;
Expand Down

0 comments on commit ef7a380

Please sign in to comment.