-
Notifications
You must be signed in to change notification settings - Fork 3
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
Chain add command #51
base: old-main
Are you sure you want to change the base?
Changes from 8 commits
4cb4600
e77c134
dead4ef
3836d24
094bb2b
23faec7
8739ad4
d3e28d4
b8667ef
87812e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,11 +64,21 @@ pub async fn get_chain(name: &str) -> Result<ChainInfo, ChainRegistryError> { | |
} | ||
|
||
async fn get_content(path: String) -> Result<reqwest::Response, ChainRegistryError> { | ||
octocrab::instance() | ||
let response = octocrab::instance() | ||
.repos("cosmos", "chain-registry") | ||
.raw_file("88bde7fb534ed6f7c26c2073f57ec5135b470f56".to_string(), path) | ||
.await | ||
.map_err(|e| e.into()) | ||
.unwrap_or_else(|err| { | ||
panic!("executor exited with error: {}", err); | ||
}); | ||
|
||
let status = response.status(); | ||
|
||
if status == 404 { | ||
panic!("Chain not found in registry") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, |
||
} | ||
|
||
Ok(response) | ||
} | ||
|
||
async fn parse_json<T>(data: reqwest::Response) -> Result<T, ChainRegistryError> | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1 +1,72 @@ | ||||||||||||||||||||
use super::show::ShowCmd; | ||||||||||||||||||||
use crate::{config, prelude::*}; | ||||||||||||||||||||
use abscissa_core::{Command, Runnable}; | ||||||||||||||||||||
use clap::Parser; | ||||||||||||||||||||
use ocular::chain::{info::ChainInfo, registry}; | ||||||||||||||||||||
use serde::{Deserialize, Serialize}; | ||||||||||||||||||||
use std::{fs, io::Write, path::Path}; | ||||||||||||||||||||
|
||||||||||||||||||||
#[derive(Command, Debug, Parser)] | ||||||||||||||||||||
pub struct AddCmd { | ||||||||||||||||||||
name: String, | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
#[derive(Deserialize, Serialize)] | ||||||||||||||||||||
struct Chains { | ||||||||||||||||||||
chains: Vec<ChainInfo>, | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
impl Runnable for AddCmd { | ||||||||||||||||||||
/// Add chain to local config file | ||||||||||||||||||||
fn run(&self) { | ||||||||||||||||||||
// Navigate to file of local config | ||||||||||||||||||||
let path = config::get_config_path(); | ||||||||||||||||||||
let config_file = Path::new(path.to_str().unwrap()); | ||||||||||||||||||||
|
||||||||||||||||||||
// Check if chain already exists | ||||||||||||||||||||
let chain_content = fs::read_to_string(config_file).unwrap_or_else(|err| { | ||||||||||||||||||||
status_err!("Can't read config file: {}", err); | ||||||||||||||||||||
std::process::exit(1); | ||||||||||||||||||||
}); | ||||||||||||||||||||
|
||||||||||||||||||||
let chain_name = self.name.as_str(); | ||||||||||||||||||||
|
||||||||||||||||||||
abscissa_tokio::run(&APP, async { | ||||||||||||||||||||
let chain_info = registry::get_chain(chain_name).await.unwrap_or_else(|err| { | ||||||||||||||||||||
status_err!("Can't fetch chain from chain registry: {}", err); | ||||||||||||||||||||
std::process::exit(1); | ||||||||||||||||||||
}); | ||||||||||||||||||||
|
||||||||||||||||||||
let mut vec = Vec::new(); | ||||||||||||||||||||
vec.push(chain_info); | ||||||||||||||||||||
|
||||||||||||||||||||
if !chain_content.contains(chain_name) { | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the chain exists in the config we can avoid a call to github by putting a check for this before the async block. |
||||||||||||||||||||
// write in the file with fs:write | ||||||||||||||||||||
let config_content = Chains { chains: vec }; | ||||||||||||||||||||
let config_content = toml::ser::to_string(&config_content).unwrap_or_else(|err| { | ||||||||||||||||||||
status_err!("{}", err); | ||||||||||||||||||||
std::process::exit(1); | ||||||||||||||||||||
}); | ||||||||||||||||||||
|
||||||||||||||||||||
let mut file = fs::OpenOptions::new() | ||||||||||||||||||||
.append(true) | ||||||||||||||||||||
.open(config_file) | ||||||||||||||||||||
.expect("Could not open file"); | ||||||||||||||||||||
Comment on lines
+51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will be a better UX if the list of chains is in alphabetical order like I'd forgotten but there is actually an ocular/ocular_cli/src/config.rs Lines 16 to 24 in 608351f
|
||||||||||||||||||||
|
||||||||||||||||||||
write!(file, "{}", config_content).unwrap(); | ||||||||||||||||||||
let name = self.name.clone(); | ||||||||||||||||||||
let show_cmd = ShowCmd { name }; | ||||||||||||||||||||
show_cmd.run(); | ||||||||||||||||||||
} else if chain_content.contains(chain_name) { | ||||||||||||||||||||
error!( | ||||||||||||||||||||
"The chain {} already exists in the local registry", | ||||||||||||||||||||
chain_name | ||||||||||||||||||||
) | ||||||||||||||||||||
} | ||||||||||||||||||||
}) | ||||||||||||||||||||
.unwrap_or_else(|e| { | ||||||||||||||||||||
status_err!("executor exited with error: {}", e); | ||||||||||||||||||||
std::process::exit(1); | ||||||||||||||||||||
}); | ||||||||||||||||||||
} | ||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's return an error instead,
ChainRegistryError::Request()
takes in anoctocrab::Error