Skip to content

Commit 9fda9d0

Browse files
author
Ludo Galabru
committed
feat: ability to generate config
1 parent b283fa9 commit 9fda9d0

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

components/chainhook-cli/src/cli/mod.rs

+53-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use crate::block::DigestingCommand;
22
use crate::config::Config;
3+
use crate::config::generator::generate_config;
34
use crate::node::Node;
45
use crate::scan::bitcoin::scan_bitcoin_chain_with_predicate;
56
use crate::scan::stacks::scan_stacks_chain_with_predicate;
67

78
use chainhook_event_observer::chainhooks::types::ChainhookFullSpecification;
89
use chainhook_event_observer::indexer::ordinals::db::{
9-
build_bitcoin_traversal_local_storage, find_inscription_with_ordinal_number,
10+
build_bitcoin_traversal_local_storage,
1011
find_inscriptions_at_wached_outpoint, initialize_ordinal_state_storage,
11-
open_readonly_ordinals_db_conn, open_readwrite_ordinals_db_conn,
12+
open_readonly_ordinals_db_conn,
1213
retrieve_satoshi_point_using_local_storage,
1314
};
14-
use chainhook_event_observer::indexer::ordinals::ord::height::Height;
1515
use chainhook_event_observer::observer::BitcoinConfig;
1616
use chainhook_event_observer::utils::Context;
1717
use chainhook_types::{BlockIdentifier, TransactionIdentifier};
@@ -35,6 +35,9 @@ enum Command {
3535
/// Manage predicates
3636
#[clap(subcommand)]
3737
Predicates(PredicatesCommand),
38+
/// Manage config
39+
#[clap(subcommand)]
40+
Config(ConfigCommand),
3841
/// Start chainhook-cli
3942
#[clap(subcommand)]
4043
Node(NodeCommand),
@@ -54,6 +57,39 @@ enum PredicatesCommand {
5457
Scan(ScanPredicate),
5558
}
5659

60+
#[derive(Subcommand, PartialEq, Clone, Debug)]
61+
#[clap(bin_name = "config", aliases = &["config"])]
62+
enum ConfigCommand {
63+
/// Generate new predicate
64+
#[clap(name = "new", bin_name = "new", aliases = &["generate"])]
65+
New(NewConfig),
66+
}
67+
68+
#[derive(Parser, PartialEq, Clone, Debug)]
69+
struct NewConfig {
70+
/// Target Devnet network
71+
#[clap(
72+
long = "devnet",
73+
conflicts_with = "testnet",
74+
conflicts_with = "mainnet"
75+
)]
76+
pub devnet: bool,
77+
/// Target Testnet network
78+
#[clap(
79+
long = "testnet",
80+
conflicts_with = "devnet",
81+
conflicts_with = "mainnet"
82+
)]
83+
pub testnet: bool,
84+
/// Target Mainnet network
85+
#[clap(
86+
long = "mainnet",
87+
conflicts_with = "testnet",
88+
conflicts_with = "devnet"
89+
)]
90+
pub mainnet: bool,
91+
}
92+
5793
#[derive(Parser, PartialEq, Clone, Debug)]
5894
struct NewPredicate {
5995
/// Predicate's name
@@ -269,6 +305,20 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
269305
return node.run().await;
270306
}
271307
},
308+
Command::Config(subcmd) => match subcmd {
309+
ConfigCommand::New(cmd) => {
310+
use std::fs::File;
311+
use std::io::Write;
312+
let config_content = generate_config();
313+
let mut file_path = PathBuf::new();
314+
file_path.push("Chainhook.toml");
315+
let mut file = File::create(&file_path)
316+
.map_err(|e| format!("unable to open file {}\n{}", file_path.display(), e))?;
317+
file.write_all(config_content.as_bytes())
318+
.map_err(|e| format!("unable to write file {}\n{}", file_path.display(), e))?;
319+
println!("Created file Chainhook.toml");
320+
}
321+
}
272322
Command::Predicates(subcmd) => match subcmd {
273323
PredicatesCommand::New(_cmd) => {
274324
// let manifest = clarinet_files::get_manifest_location(None);

components/chainhook-cli/src/config/generator.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
pub fn generate_config() -> String {
22
let conf = format!(
3-
r#"
4-
[storage]
3+
r#"[storage]
54
driver = "redis"
65
redis_uri = "redis://localhost:6379/"
6+
cache_path = "chainhook_cache"
77
88
[chainhooks]
99
max_stacks_registrations = 500
1010
max_bitcoin_registrations = 500
1111
1212
[network]
13-
mode = "devnet"
14-
bitcoin_node_rpc_url = "http://0.0.0.0:18443"
13+
mode = "mainnet"
14+
bitcoin_node_rpc_url = "http://localhost:8332"
1515
bitcoin_node_rpc_username = "devnet"
1616
bitcoin_node_rpc_password = "devnet"
17-
stacks_node_rpc_url = "http://0.0.0.0:20443"
17+
stacks_node_rpc_url = "http://localhost:20443"
1818
"#
1919
);
2020
return conf;

0 commit comments

Comments
 (0)