Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples to cli --help #5388

Merged
merged 56 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
21ab78f
Add examples to cli --help
crodas Dec 13, 2023
a9b1628
Introducing an automatic way to test examples
crodas Dec 15, 2023
9551af6
WIP
crodas Dec 15, 2023
e47e1fd
Add support for longer commands
crodas Dec 16, 2023
783804f
Taking a different approach
crodas Dec 16, 2023
cca3655
Added more comments
crodas Dec 16, 2023
d4cb380
Merge remote-tracking branch 'origin/master' into plugins-examples
crodas Dec 18, 2023
03a9b91
Update macro
crodas Dec 19, 2023
cb49afe
Update forc-plugins/forc-crypto/src/args.rs
crodas Dec 19, 2023
e381462
Update forc-plugins/forc-crypto/src/keygen/new_key.rs
crodas Dec 19, 2023
47b629b
Update forc-plugins/forc-doc/src/cli.rs
crodas Dec 19, 2023
df1a1ff
Merge remote-tracking branch 'origin/master' into plugins-examples
crodas Dec 20, 2023
afb4d6a
Add examples for forc-fmt
crodas Dec 28, 2023
171115d
Merge remote-tracking branch 'origin/master' into plugins-examples
crodas Dec 28, 2023
a399d30
Add ability to test stdout/stderr
crodas Dec 29, 2023
6091e03
Add example for forc-submit
crodas Dec 30, 2023
8702941
Add more tests for forc-submit
crodas Dec 30, 2023
f6b6738
Merge remote-tracking branch 'origin/master' into plugins-examples
crodas Dec 30, 2023
3f6cc14
Merge branch 'master' into plugins-examples
crodas Jan 3, 2024
6674d34
Merge branch 'master' into plugins-examples
crodas Jan 3, 2024
525e96e
Merge branch 'master' into plugins-examples
crodas Jan 3, 2024
d1946b9
Update forc-plugins/forc-crypto/src/address.rs
crodas Jan 3, 2024
763ccda
Update forc-plugins/forc-client/src/cmd/submit.rs
crodas Jan 3, 2024
d5d32d8
Update forc-plugins/forc-crypto/src/address.rs
crodas Jan 3, 2024
0411a7d
Update forc-plugins/forc-crypto/src/keygen/new_key.rs
crodas Jan 3, 2024
6414e01
Update forc-plugins/forc-client/src/cmd/deploy.rs
crodas Jan 3, 2024
d92b589
Fixed test
crodas Jan 3, 2024
5cffff4
Use consistent SHA256 naming for algorithm
crodas Jan 3, 2024
f930e27
Remove `author` and add `after_help` as well
crodas Jan 3, 2024
0592490
Remove `extern` and define tests in their own `cli_examples` namespace
crodas Jan 4, 2024
2435eb6
Add more examples
crodas Jan 4, 2024
211e8bf
Remove after_long_help
crodas Jan 4, 2024
17e2581
Clippy
crodas Jan 4, 2024
f048c77
Merge branch 'master' into plugins-examples
crodas Jan 4, 2024
a5256a0
WIP
crodas Jan 4, 2024
e5aa0ca
Merge branch 'master' into plugins-examples
crodas Jan 5, 2024
8f2bc87
Merge remote-tracking branch 'origin/plugins-examples' into plugins-e…
crodas Jan 5, 2024
72884aa
chore: bump forc-wallet to 0.4.1
kayagokalp Jan 4, 2024
6ec795c
chore: use sdk v0.54.0
kayagokalp Jan 4, 2024
b0e545f
fix: use 0.4.2 and subscriptions feature
kayagokalp Jan 4, 2024
0a525c6
Bump ci
crodas Jan 5, 2024
fb0fa1a
chore: also update sdk harness test
kayagokalp Jan 7, 2024
a2b48ad
use fuel-core-client 0.22
kayagokalp Jan 7, 2024
478802c
Merge remote-tracking branch 'origin/kayagokalp/wallet-bump-to-0.4.1'…
crodas Jan 8, 2024
25ce578
Update tests that expects to fail
crodas Jan 8, 2024
d3cc93f
Merge branch 'master' into plugins-examples
crodas Jan 8, 2024
c26b37a
Merge remote-tracking branch 'origin/master' into plugins-examples
crodas Jan 8, 2024
f4fd9d9
Merge branch 'master' into plugins-examples
crodas Jan 9, 2024
0a884ce
Update cargo.lock
crodas Jan 9, 2024
e1f74aa
Move macro to forc-utils
crodas Jan 9, 2024
8d506c0
Fix clippy issue
crodas Jan 9, 2024
94dfb0c
Add examples for forc-test
crodas Jan 9, 2024
9e1355b
Add more tests
crodas Jan 9, 2024
d18d260
Add more tests for forc build and forc test
crodas Jan 9, 2024
b87d0fe
Add tests for forc check
crodas Jan 9, 2024
93fea83
Merge branch 'master' into plugins-examples
sdankel Jan 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions forc-plugins/forc-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ anyhow = "1.0.75"
async-trait = "0.1.58"
atty = "0.2.14"
clap = { version = "3", features = ["derive", "env"] }
forc = { version = "0.48.1", path = "../../forc" }
forc-tracing = { version = "0.48.1", path = "../../forc-tracing" }
fuel-core-types = { workspace = true }
fuel-crypto = { workspace = true, features = ["random"] }
Expand Down
7 changes: 6 additions & 1 deletion forc-plugins/forc-crypto/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ use fuels_core::types::bech32::Bech32Address;
use serde_json::json;
use std::str::{from_utf8, FromStr};

forc::cli_examples! {
[ Convert any valid address to all other supported formats => crypto "address" "fuel12e0xwx34nfp7jrzvn9mp5qkac3yvp7h8fx37ghl7klf82vv2wkys6wd523" ]
}

#[derive(Debug, clap::Args)]
#[clap(
author,
version,
about = "Converts any valid address to all supported formats"
about = "Converts any valid address to all supported formats",
after_long_help = help(),
)]
pub struct Args {
/// The address to convert. It can be either a valid address in any format
Expand Down
14 changes: 13 additions & 1 deletion forc-plugins/forc-crypto/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@ use std::{
path::Path,
};

forc::cli_examples! {
[ Hashes data passing as an argument with SHA256 => crypto "sha256" "test" ]
[ Hashes data passing as an argument with Keccak256 => crypto "keccak256" "test" ]
[ Hashes a file path as an argument with Sha256 => crypto "sha256" "src/args.rs" ]
[ Hashes a file path as an argument with Keccak256 => crypto "keccak256" "src/args.rs" ]
}

#[derive(Debug, Clone, clap::Args)]
#[clap(author, version, about = "Hashes the argument or file with this hash")]
#[clap(
author,
version,
about = "Hashes the argument or file with this hash",
after_long_help = help(),
)]
pub struct HashArgs {
/// This argument is optional, it can be either:
///
Expand Down
10 changes: 9 additions & 1 deletion forc-plugins/forc-crypto/src/keygen/new_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ use libp2p_identity::{secp256k1, Keypair, PeerId};
use serde_json::json;
use std::ops::Deref;

const ABOUT: &str = "Creates a new key for use with fuel-core";

forc::cli_examples! {
[ Creates a new key for block production by default => crypto "new-key" ]
[ Creates a new key for peering => crypto "new-key" "-k" "peering" ]
[ Creates a new key for block production => crypto "new-key" "-k" "peering" ]
}

/// Generate a random new secret & public key in the format expected by fuel-core
#[derive(Debug, clap::Args)]
#[clap(author, version, about = "Creates a new key for use with fuel-core")]
#[clap(author, version, about = ABOUT, after_long_help = help())]
pub struct Arg {
/// Key type to generate. It can either be `block-production` or `peering`.
#[clap(
Expand Down
14 changes: 13 additions & 1 deletion forc-plugins/forc-crypto/src/keygen/parse_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ use libp2p_identity::{secp256k1, Keypair, PeerId};
use serde_json::json;
use std::{ops::Deref, str::FromStr};

const ABOUT: &str = "Parses a private key to view the associated public key";

#[cfg(test)]
#[allow(non_upper_case_globals)]
const PrivateKey: &str = "f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28";

forc::cli_examples! {
[ Parses the secret of a block production => crypto "parse-secret" PrivateKey ]
[ Parses the secret of a peering => crypto "parse-secret" "-k" "peering" PrivateKey ]
}

/// Parse a secret key to view the associated public key
#[derive(Debug, clap::Args)]
#[clap(
author,
version,
about = "Parses a private key to view the associated public key"
about = ABOUT,
after_long_help = help(),
)]
pub struct Arg {
/// A private key in hex format
Expand Down
18 changes: 17 additions & 1 deletion forc-plugins/forc-crypto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,26 @@ mod keccak256;
mod keygen;
mod sha256;

const ABOUT: &str = "Forc plugin for hashing arbitrary data.";

fn after_long_help() -> &'static str {
Box::leak(
format!(
"EXAMPLES:\n{}{}{}{}",
args::examples(),
address::examples(),
keygen::new_key::examples(),
keygen::parse_secret::examples(),
)
.into_boxed_str(),
)
}

#[derive(Debug, Parser)]
#[clap(
name = "forc-crypto",
about = "Forc plugin for hashing arbitrary data.",
about = ABOUT,
after_long_help = after_long_help(),
version
)]
pub enum Command {
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ anyhow = "1.0.65"
clap = { version = "4.0.18", features = ["derive"] }
colored = "2.0.0"
comrak = "0.16"
forc = { version = "0.48.1", path = "../../forc" }
forc-pkg = { version = "0.48.1", path = "../../forc-pkg" }
forc-util = { version = "0.48.1", path = "../../forc-util" }
horrorshow = "0.8.4"
Expand Down
13 changes: 12 additions & 1 deletion forc-plugins/forc-doc/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
use clap::Parser;
use forc_pkg::source::IPFSNode;

const ABOUT: &str = "Forc plugin for building a Sway package's documentation";

forc::cli_examples! {
[ Build the docs for a project in the current path => doc ]
[ Build the docs for a project in the current path and open it in the browser => doc "--open" ]
[ Build the docs for a project located in another path => doc "--manifest-path" "../tests_project2" ]
[ Build the docs for the current project exportings private types => doc "--document-private-items" ]
[ Build the docs offline without downloading any dependency from the network => doc "--offline" ]
}

#[derive(Debug, Parser, Default)]
#[clap(
name = "forc-doc",
about = "Forc plugin for building a Sway package's documentation",
about = ABOUT,
after_long_help = help(),
version
)]
pub struct Command {
Expand Down
2 changes: 2 additions & 0 deletions forc-plugins/forc-doc/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions forc-plugins/forc-doc/tests/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "tests"

[dependencies]
11 changes: 11 additions & 0 deletions forc-plugins/forc-doc/tests/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract;

abi MyContract {
fn test_function() -> bool;
}

impl MyContract for Contract {
fn test_function() -> bool {
true
}
}
2 changes: 2 additions & 0 deletions forc-plugins/forc-doc/tests_project2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions forc-plugins/forc-doc/tests_project2/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "inner"

[dependencies]
11 changes: 11 additions & 0 deletions forc-plugins/forc-doc/tests_project2/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract;

abi MyContract {
fn test_function() -> bool;
}

impl MyContract for Contract {
fn test_function() -> bool {
true
}
}
2 changes: 2 additions & 0 deletions forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ forc-util = { version = "0.48.1", path = "../forc-util" }
fs_extra = "1.2"
fuel-asm = { workspace = true }
hex = "0.4.3"
paste = "1.0.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.73"
serial_test = "2.0.0"
sway-core = { version = "0.48.1", path = "../sway-core" }
sway-error = { version = "0.48.1", path = "../sway-error" }
sway-types = { version = "0.48.1", path = "../sway-types" }
Expand Down
45 changes: 45 additions & 0 deletions forc/src/cli/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#[macro_export]
macro_rules! cli_examples {
($( [ $($description:ident)* => $command:tt $($arg:expr)* ] )*) => {
#[cfg(test)]
use $crate::serial_test;
$(
$crate::paste::paste! {
#[cfg(test)]
#[test]
#[serial_test::serial]
fn [<$($description:lower _)*:snake example>] () {
let mut proc = std::process::Command::new("cargo");
proc.arg("run");
proc.arg("--bin");
proc.arg(format!("forc-{}", stringify!($command)));
proc.arg("--");
$(
proc.arg($arg);
)*

let path = std::path::Path::new("tests");
if path.is_dir() {
proc.current_dir(path);
}
let output = proc.output().expect(stringify!($command));
assert!(output.status.success(), "{}: {:?}", stringify!($($description)*), output);
}
}
)*

fn help() -> &'static str {
Box::leak(format!("EXAMPLES:\n{}", examples()).into_boxed_str())
}

pub fn examples() -> &'static str {
Box::leak( [
$(
$crate::paste::paste! {
format!("\t#{}\n\tforc {} {}\n\n", stringify!($($description)*), stringify!($command), stringify!($($arg)*) )
},
)*
].concat().into_boxed_str())
}
};
}
2 changes: 2 additions & 0 deletions forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub use test::Command as TestCommand;
use tracing::metadata::LevelFilter;
pub use update::Command as UpdateCommand;

#[macro_use]
pub mod help;
mod commands;
mod plugin;
pub mod shared;
Expand Down
3 changes: 3 additions & 0 deletions forc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pub mod test {
pub mod util {
pub use sway_utils::constants;
}

pub extern crate paste;
pub use serial_test;