Skip to content

Commit

Permalink
✨ feat(cli): add --name flag (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneshASP authored Dec 2, 2023
1 parent c4f9855 commit 306888a
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 11 deletions.
73 changes: 62 additions & 11 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
cmd.rpc_url = configuration.rpc_url;
}

// if the user has passed an output filename, override the default filename
let mut filename: String = "disassembled.asm".to_string();
let given_name = cmd.name.as_str();

if !given_name.is_empty() {
filename = format!("{}-{}", given_name, filename);
}

let assembly = disassemble(cmd.clone()).await?;

if cmd.output == "print" {
print_with_less(&assembly).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "disassembled.asm")
.await?;
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, &filename).await?;

write_file(&output_path, &assembly);
}
Expand All @@ -124,6 +131,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
cmd.rpc_url = configuration.rpc_url;
}

// if the user has passed an output filename, override the default filename
let mut abi_filename: String = "abi.json".to_string();
let mut decompiled_output_filename: String = "decompiled".to_string();

let given_name = cmd.name.as_str();

if !given_name.is_empty() {
abi_filename = format!("{}-{}", given_name, abi_filename);
decompiled_output_filename =
format!("{}-{}", given_name, decompiled_output_filename);
}

let result = decompile(cmd.clone()).await?;

if cmd.output == "print" {
Expand All @@ -144,7 +163,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// write the contract ABI
if let Some(abi) = result.abi {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "abi.json")
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, &abi_filename)
.await?;

write_file(
Expand Down Expand Up @@ -174,11 +193,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// write the contract source
if let Some(source) = &result.source {
let output_path = if cmd.include_solidity {
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "decompiled.sol")
.await?
build_output_path(
&cmd.output,
&cmd.target,
&cmd.rpc_url,
&format!("{}.sol", &decompiled_output_filename),
)
.await?
} else {
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "decompiled.yul")
.await?
build_output_path(
&cmd.output,
&cmd.target,
&cmd.rpc_url,
&format!("{}.yul", &decompiled_output_filename,),
)
.await?
};
write_file(&output_path, source);
}
Expand Down Expand Up @@ -208,14 +237,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
cmd.rpc_url = configuration.rpc_url;
}

// if the user has passed an output filename, override the default filename
let mut filename = "cfg.dot".to_string();
let given_name = cmd.name.as_str();

if !given_name.is_empty() {
filename = format!("{}-{}", given_name, filename);
}
let cfg = cfg(cmd.clone()).await?;
let stringified_dot = build_cfg(&cfg, &cmd);

if cmd.output == "print" {
print_with_less(&stringified_dot).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "cfg.dot").await?;
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, &filename).await?;
write_file(&output_path, &stringified_dot);
}
}
Expand All @@ -226,6 +262,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
cmd.rpc_url = configuration.rpc_url;
}

// if the user has passed an output filename, override the default filename
let mut filename = "dump.csv".to_string();
let given_name = cmd.name.as_str();

if !given_name.is_empty() {
filename = format!("{}-{}", given_name, filename);
}

// if the user has not specified a transpose api key, use the default
if cmd.transpose_api_key.as_str() == "" {
cmd.transpose_api_key = configuration.transpose_api_key;
Expand All @@ -249,7 +293,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
print_with_less(&lines.join("\n")).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "dump.csv").await?;
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, &filename).await?;

write_lines_to_file(&output_path, lines);
}
Expand All @@ -261,6 +305,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
cmd.rpc_url = configuration.rpc_url;
}

// if the user has passed an output filename, override the default filename
let mut filename = "snapshot.csv".to_string();
let given_name = cmd.name.as_str();

if !given_name.is_empty() {
filename = format!("{}-{}", given_name, filename);
}

let snapshot_result = snapshot(cmd.clone()).await?;
let csv_lines = generate_csv(
&snapshot_result.snapshots,
Expand All @@ -272,8 +324,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
print_with_less(&csv_lines.join("\n")).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "snapshot.csv")
.await?;
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, &filename).await?;

write_lines_to_file(&output_path, csv_lines);
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub struct CFGArgs {
/// The output directory to write the output to or 'print' to print to the console
#[clap(long = "output", short = 'o', default_value = "output", hide_default_value = true)]
pub output: String,

/// The name for the output file
#[clap(long, short, default_value = "", hide_default_value = true)]
pub name: String,
}

impl CFGArgsBuilder {
Expand All @@ -64,6 +68,7 @@ impl CFGArgsBuilder {
default: Some(true),
color_edges: Some(false),
output: Some(String::new()),
name: Some(String::new()),
}
}
}
Expand Down Expand Up @@ -145,6 +150,7 @@ pub async fn cfg(args: CFGArgs) -> Result<Graph<String, String>, Box<dyn std::er
verbose: args.verbose.clone(),
rpc_url: args.rpc_url.clone(),
decimal_counter: false,
name: String::from(""),
output: String::from(""),
})
.await?;
Expand Down
6 changes: 6 additions & 0 deletions core/src/decompile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub struct DecompilerArgs {
/// The output directory to write the output to or 'print' to print to the console
#[clap(long = "output", short = 'o', default_value = "output", hide_default_value = true)]
pub output: String,

/// The name for the output file
#[clap(long, short, default_value = "", hide_default_value = true)]
pub name: String,
}

impl DecompilerArgsBuilder {
Expand All @@ -89,6 +93,7 @@ impl DecompilerArgsBuilder {
include_solidity: Some(false),
include_yul: Some(false),
output: Some(String::new()),
name: Some(String::new()),
}
}
}
Expand Down Expand Up @@ -185,6 +190,7 @@ pub async fn decompile(
verbose: args.verbose.clone(),
rpc_url: args.rpc_url.clone(),
decimal_counter: false,
name: String::from(""),
output: String::from(""),
})
.await?;
Expand Down
5 changes: 5 additions & 0 deletions core/src/disassemble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub struct DisassemblerArgs {
#[clap(long = "decimal-counter", short = 'd')]
pub decimal_counter: bool,

/// Name of the output file.
#[clap(long, short, default_value = "", hide_default_value = true)]
pub name: String,

/// The output directory to write the output to or 'print' to print to the console
#[clap(long = "output", short = 'o', default_value = "output", hide_default_value = true)]
pub output: String,
Expand All @@ -45,6 +49,7 @@ impl DisassemblerArgsBuilder {
verbose: Some(clap_verbosity_flag::Verbosity::new(0, 1)),
rpc_url: Some(String::new()),
decimal_counter: Some(false),
name: Some(String::new()),
output: Some(String::new()),
}
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/dump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ pub struct DumpArgs {
/// The chain of the target. Valid chains are ethereum, polygon, goerli, canto, and arbitrum.
#[clap(long, default_value = "ethereum", hide_default_value = true)]
pub chain: String,

/// The name for the output file
#[clap(long, short, default_value = "", hide_default_value = true)]
pub name: String,
}

impl DumpArgsBuilder {
Expand All @@ -82,6 +86,7 @@ impl DumpArgsBuilder {
to_block: Some(9999999999),
no_tui: Some(true),
chain: Some(String::from("ethereum")),
name: Some(String::new()),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/src/dump/structures/dump_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl DumpState {
to_block: 9999999999,
no_tui: false,
chain: String::from("ethereum"),
name: String::from(""),
},
scroll_index: 0,
selection_size: 1,
Expand Down
6 changes: 6 additions & 0 deletions core/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ pub struct SnapshotArgs {
#[clap(long)]
pub no_tui: bool,

/// Name for the output snapshot file.
#[clap(long, short, default_value = "", hide_default_value = true)]
pub name: String,

/// The output directory to write the output to, or 'print' to print to the console.
#[clap(long = "output", short = 'o', default_value = "output", hide_default_value = true)]
pub output: String,
Expand All @@ -85,6 +89,7 @@ impl SnapshotArgsBuilder {
default: Some(true),
skip_resolving: Some(false),
no_tui: Some(true),
name: Some(String::new()),
output: Some(String::new()),
}
}
Expand Down Expand Up @@ -174,6 +179,7 @@ pub async fn snapshot(args: SnapshotArgs) -> Result<SnapshotResult, Box<dyn std:
verbose: args.verbose.clone(),
rpc_url: args.rpc_url,
decimal_counter: false,
name: args.name,
output: String::new(),
})
.await?;
Expand Down
4 changes: 4 additions & 0 deletions core/tests/test_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod benchmark {
default: true,
color_edges: false,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::cfg::cfg(args).await;
}
Expand All @@ -32,6 +33,7 @@ mod benchmark {
default: true,
color_edges: false,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::cfg::cfg(args).await;
}
Expand All @@ -55,6 +57,7 @@ mod integration_tests {
default: true,
color_edges: false,
output: String::from(""),
name: String::from(""),
})
.await
.unwrap();
Expand All @@ -78,6 +81,7 @@ mod integration_tests {
default: true,
color_edges: false,
output: String::from(""),
name: String::from(""),
})
.await
.unwrap();
Expand Down
10 changes: 10 additions & 0 deletions core/tests/test_decompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod benchmark {
include_solidity: true,
include_yul: false,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::decompile::decompile(args).await;
}
Expand All @@ -36,6 +37,7 @@ mod benchmark {
include_solidity: true,
include_yul: false,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::decompile::decompile(args).await;
}
Expand All @@ -55,6 +57,7 @@ mod benchmark {
include_solidity: false,
include_yul: true,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::decompile::decompile(args).await;
}
Expand All @@ -74,6 +77,7 @@ mod benchmark {
include_solidity: false,
include_yul: true,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::decompile::decompile(args).await;
}
Expand All @@ -93,6 +97,7 @@ mod benchmark {
include_solidity: false,
include_yul: false,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::decompile::decompile(args).await;
}
Expand All @@ -112,6 +117,7 @@ mod benchmark {
include_solidity: false,
include_yul: false,
output: String::from(""),
name: String::from(""),
};
let _ = heimdall_core::decompile::decompile(args).await;
}
Expand All @@ -137,6 +143,7 @@ mod integration_tests {
include_solidity: true,
include_yul: false,
output: String::from(""),
name: String::from(""),
})
.await
.unwrap();
Expand Down Expand Up @@ -165,6 +172,7 @@ mod integration_tests {
include_solidity: true,
include_yul: false,
output: String::from(""),
name: String::from(""),
})
.await
.unwrap();
Expand Down Expand Up @@ -200,6 +208,7 @@ mod integration_tests {
include_solidity: true,
include_yul: false,
output: String::from(""),
name: String::from(""),
})
.await
.unwrap();
Expand Down Expand Up @@ -306,6 +315,7 @@ mod integration_tests {
include_solidity: true,
include_yul: false,
output: String::from(""),
name: String::from(""),
})
.await
.unwrap();
Expand Down
Loading

0 comments on commit 306888a

Please sign in to comment.