Skip to content

Commit

Permalink
Merge pull request #40 from fpco/config-subcommand
Browse files Browse the repository at this point in the history
Config subcommand
  • Loading branch information
snoyberg authored Aug 1, 2024
2 parents b35723b + 974a26a commit 0f2a5ad
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 33 deletions.
35 changes: 15 additions & 20 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions packages/cosmos-bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@ pub(crate) enum Subcommand {
#[clap(flatten)]
opt: crate::cw3::Opt,
},
/// Manage config file
Config {
#[clap(subcommand)]
opt: crate::config::Opt,
},
}
132 changes: 132 additions & 0 deletions packages/cosmos-bin/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use std::str::FromStr;

use anyhow::Result;
use cosmos::{AddressHrp, CosmosConfig, CosmosConfigError};

#[derive(clap::Parser)]
pub(crate) enum Opt {
/// Print the location of the config file
File {},
/// Print the values from the config
Print {},
/// Configure a new network
///
/// This forces the caller to provide all required fields.
/// If you want to make smaller updates, use the set subcommand.
NewNetwork {
/// Name to be used for this network
#[clap(long)]
name: String,
/// Primary gRPC endpoint
#[clap(long)]
grpc: String,
/// Chain ID
#[clap(long)]
chain_id: String,
/// Address prefix/HRP
#[clap(long)]
hrp: AddressHrp,
/// Gas coin
#[clap(long)]
gas_coin: String,
},
/// Set a config value for a specific network
Set {
/// Network name
name: String,
/// Config key
key: ConfigKey,
/// Value
value: String,
},
/// Add a gRPC fallback
AddFallback {
/// Network name
name: String,
/// gRPC URL
url: String,
},
}

// Strum would be more approriate, but serde gives better error messages
#[derive(serde::Deserialize, Clone, Copy, Debug)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum ConfigKey {
Grpc,
ChainId,
Hrp,
GasCoin,
}

impl FromStr for ConfigKey {
type Err = serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_value(serde_json::Value::String(s.to_owned()))
}
}

fn load(opt: &crate::cli::Opt) -> Result<CosmosConfig, CosmosConfigError> {
opt.network_opt
.config
.as_ref()
.map_or_else(CosmosConfig::load, |path| {
CosmosConfig::load_from(path, true)
})
}

pub(crate) fn go(opt: crate::cli::Opt, inner: Opt) -> Result<()> {
match inner {
Opt::File {} => {
match opt.network_opt.config {
Some(file) => {
tracing::info!(
"Config file overridden by command line parameter or environment variable"
);
println!("{}", file.display());
}
None => {
println!("{}", CosmosConfig::default_file()?.display())
}
}
Ok(())
}
Opt::Print {} => {
let config = load(&opt)?;
config.print();
Ok(())
}
Opt::NewNetwork {
name,
chain_id,
hrp,
grpc,
gas_coin,
} => {
let mut config = load(&opt)?;
config.new_network(name, grpc, chain_id, gas_coin, hrp);
config.save()?;
println!("Changes saved");
Ok(())
}
Opt::Set { name, key, value } => {
let mut config = load(&opt)?;
match key {
ConfigKey::Grpc => config.set_grpc(name, value),
ConfigKey::ChainId => config.set_chain_id(name, value),
ConfigKey::Hrp => config.set_hrp(name, value.parse()?),
ConfigKey::GasCoin => config.set_gas_coin(name, value),
}
config.save()?;
println!("Changes saved");
Ok(())
}
Opt::AddFallback { name, url } => {
let mut config = load(&opt)?;
config.add_grpc_fallback(name, url);
config.save()?;
println!("Changes saved");
Ok(())
}
}
}
2 changes: 2 additions & 0 deletions packages/cosmos-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod authz;
mod bank;
mod chain;
mod cli;
mod config;
mod contract;
mod cw3;
mod my_duration;
Expand Down Expand Up @@ -68,6 +69,7 @@ impl Subcommand {
let cosmos = opt.network_opt.build().await?;
cw3::go(cosmos, inner).await?;
}
Subcommand::Config { opt: inner } => config::go(opt, inner)?,
}

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion packages/cosmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ strum_macros = "0.26.2"
tendermint-proto = "0.35.0"
figment = { version = "0.10.19", features = ["env", "toml"], optional = true }
directories = { version = "5.0.1", optional = true }
toml = { version = "0.8.19", optional = true }

[features]
clap = ["dep:clap"]
config = ["dep:figment", "dep:directories"]
config = ["dep:figment", "dep:directories", "dep:toml"]
default = ["rustls-tls", "compression"]
compression = ["reqwest/gzip", "reqwest/brotli"]
rustls-tls = ["reqwest/rustls-tls"]
native-tls = ["reqwest/native-tls"]
directories = ["dep:directories"]
toml = ["dep:toml"]

[dev-dependencies]
quickcheck = "1"
Loading

0 comments on commit 0f2a5ad

Please sign in to comment.