Skip to content
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

Ukpai/delete chain #57

Draft
wants to merge 2 commits into
base: old-main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ocular_cli/src/commands/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use clap::Parser;

use self::list::ListCmd;
use self::show::ShowCmd;
use self::delete::DeleteCmd;
/// `start` subcommand
///
/// The `Parser` proc macro generates an option parser based on the struct
Expand All @@ -24,6 +25,7 @@ use self::show::ShowCmd;
pub enum ChainsCmd {
Show(ShowCmd),
List(ListCmd),
Delete(DeleteCmd),
}

impl config::Override<OcularCliConfig> for ChainsCmd {
Expand Down
61 changes: 61 additions & 0 deletions ocular_cli/src/commands/chains/delete.rs
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
use crate::{config, prelude::*};
use abscissa_core::{Command, Runnable};
use clap::Parser;
use ocular::chain::info::ChainInfo;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, io::Write, path::Path};

#[derive(Command, Debug, Parser)]
pub struct DeleteCmd {
name: String,
}

#[derive(Deserialize)]
struct Chains {
chains: Vec<ChainInfo>,
}

impl Runnable for DeleteCmd {
fn run(&self) {
let path = config::get_config_path();
let config_file = Path::new(path.to_str().unwrap());

let chain_list = fs::read_to_string(config_file).unwrap_or_else(|err| {
status_err!("Can't fetch config file: {}", err);
std::process::exit(1);
});

let data: Chains = toml::from_str(&chain_list).unwrap_or_else(|err| {
status_err!("Can't fetch list of local chains:{}", err);
std::process::exit(1);
});

let mut info = HashMap::new();

for chain_info in data.chains {
info.insert(chain_info.chain_name.clone(), chain_info);
}

let chains = self.name.clone();

if let Some(chain_details) = info.get(chains.as_str()) {
// Remove the map content from file

info.remove_entry(&chain_details.chain_name.clone());

for values in info.values() {
let config_content = toml::to_string(values).unwrap_or_else(|err| {
status_err!("{}", err);
std::process::exit(1);
});

fs::write(config_file, config_content);
}
} else {
println!("can't find chain in local registry, run the add command to add chain")
}

// Use hashmap to map chain name and chain info

// Print message that delete was successful else, print error
}
}