Skip to content

Commit

Permalink
feat: ability to generate config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Mar 23, 2023
1 parent b283fa9 commit 9fda9d0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
56 changes: 53 additions & 3 deletions components/chainhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::block::DigestingCommand;
use crate::config::Config;
use crate::config::generator::generate_config;
use crate::node::Node;
use crate::scan::bitcoin::scan_bitcoin_chain_with_predicate;
use crate::scan::stacks::scan_stacks_chain_with_predicate;

use chainhook_event_observer::chainhooks::types::ChainhookFullSpecification;
use chainhook_event_observer::indexer::ordinals::db::{
build_bitcoin_traversal_local_storage, find_inscription_with_ordinal_number,
build_bitcoin_traversal_local_storage,
find_inscriptions_at_wached_outpoint, initialize_ordinal_state_storage,
open_readonly_ordinals_db_conn, open_readwrite_ordinals_db_conn,
open_readonly_ordinals_db_conn,
retrieve_satoshi_point_using_local_storage,
};
use chainhook_event_observer::indexer::ordinals::ord::height::Height;
use chainhook_event_observer::observer::BitcoinConfig;
use chainhook_event_observer::utils::Context;
use chainhook_types::{BlockIdentifier, TransactionIdentifier};
Expand All @@ -35,6 +35,9 @@ enum Command {
/// Manage predicates
#[clap(subcommand)]
Predicates(PredicatesCommand),
/// Manage config
#[clap(subcommand)]
Config(ConfigCommand),
/// Start chainhook-cli
#[clap(subcommand)]
Node(NodeCommand),
Expand All @@ -54,6 +57,39 @@ enum PredicatesCommand {
Scan(ScanPredicate),
}

#[derive(Subcommand, PartialEq, Clone, Debug)]
#[clap(bin_name = "config", aliases = &["config"])]
enum ConfigCommand {
/// Generate new predicate
#[clap(name = "new", bin_name = "new", aliases = &["generate"])]
New(NewConfig),
}

#[derive(Parser, PartialEq, Clone, Debug)]
struct NewConfig {
/// Target Devnet network
#[clap(
long = "devnet",
conflicts_with = "testnet",
conflicts_with = "mainnet"
)]
pub devnet: bool,
/// Target Testnet network
#[clap(
long = "testnet",
conflicts_with = "devnet",
conflicts_with = "mainnet"
)]
pub testnet: bool,
/// Target Mainnet network
#[clap(
long = "mainnet",
conflicts_with = "testnet",
conflicts_with = "devnet"
)]
pub mainnet: bool,
}

#[derive(Parser, PartialEq, Clone, Debug)]
struct NewPredicate {
/// Predicate's name
Expand Down Expand Up @@ -269,6 +305,20 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
return node.run().await;
}
},
Command::Config(subcmd) => match subcmd {
ConfigCommand::New(cmd) => {
use std::fs::File;
use std::io::Write;
let config_content = generate_config();
let mut file_path = PathBuf::new();
file_path.push("Chainhook.toml");
let mut file = File::create(&file_path)
.map_err(|e| format!("unable to open file {}\n{}", file_path.display(), e))?;
file.write_all(config_content.as_bytes())
.map_err(|e| format!("unable to write file {}\n{}", file_path.display(), e))?;
println!("Created file Chainhook.toml");
}
}
Command::Predicates(subcmd) => match subcmd {
PredicatesCommand::New(_cmd) => {
// let manifest = clarinet_files::get_manifest_location(None);
Expand Down
10 changes: 5 additions & 5 deletions components/chainhook-cli/src/config/generator.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
pub fn generate_config() -> String {
let conf = format!(
r#"
[storage]
r#"[storage]
driver = "redis"
redis_uri = "redis://localhost:6379/"
cache_path = "chainhook_cache"
[chainhooks]
max_stacks_registrations = 500
max_bitcoin_registrations = 500
[network]
mode = "devnet"
bitcoin_node_rpc_url = "http://0.0.0.0:18443"
mode = "mainnet"
bitcoin_node_rpc_url = "http://localhost:8332"
bitcoin_node_rpc_username = "devnet"
bitcoin_node_rpc_password = "devnet"
stacks_node_rpc_url = "http://0.0.0.0:20443"
stacks_node_rpc_url = "http://localhost:20443"
"#
);
return conf;
Expand Down

0 comments on commit 9fda9d0

Please sign in to comment.