Files
nym/ephemera/src/cli/config.rs
T
Bogdan-Ștefan Neacşu ee5b55fab6 Feature/ephemera (#3731)
* Feature/ephemera compile (#3437)

* Include ephemera node code in repo

* Upgrade deps

* Bump minor version of cosmwasm-std

* Include ephemera in nym-api dep and downgrade rusqlite

* Fix clippy and ephemera docs code

* More clippy on ephemera

---------

Co-authored-by: Andrus Salumets <andrus@nymtech.net>

* Start ephemera components in nym-api (#3475)

* Start ephemera components in nym-api

* Pass nyxd client and use common metric structures

* Swap url endpoint with contract for sending rewarding messages

* Fix build after rebase

* Perform ephemera rewards computation before normal nym-api ones

* Remove contract mock from ephemera

* Take raw rewards from network monitor

* Remove ephemera old reward version

* Use nym shutdown procedure in ephemera

* Temporary fix for some warnings

* Umock contract membership of ephemera (#3574)

* Pass nyxd client to members provider

* Basic ephemera contract

* Add register peer tx

* Add query all peers

* Nyxd ephemera client

* Add registration of ephemera peer

* Replace epoch http api with actual contract

* Merge ephemera config into nym-api config

* Load cluster from contract

* Guard nym-outfox out of cosmwasm builds (#3650)

* Feature/fixes while testing (#3668)

* Commit local peer before querying contract

* Default to anyonline

* Remove string from template

* Fix avg computing

* Use updated qa env

* Fix clippy

* Add unit tests for ephemera contract

* Upload ephemera contract in CI

* Add group check for peer signup

* Peer registration unit test

* Start ephemera only on monitoring

* Remove old MixnodeToReward struct

* Move all ephemera config to its file

* Skip with serde ephemera config

* Fix default value in args

* Feature/add ephemera flag (#3727)

* Replace unwrap with error handling

* Add ephemera enable flag

* Fix template

* Add json schema to ephemera contract (#3735)

* Update lock files

* Update changelog

---------

Co-authored-by: Andrus Salumets <andrus@nymtech.net>
2023-08-18 14:14:13 +03:00

133 lines
4.3 KiB
Rust

use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use clap::Parser;
use log::{error, info};
use toml::{Table, Value};
use crate::config::Configuration;
#[derive(Debug, Clone, Parser)]
pub struct UpdateConfigCmd {
#[clap(long)]
pub config_path: String,
#[clap(long)]
pub property: String,
#[clap(long)]
pub value: String,
}
impl UpdateConfigCmd {
/// # Panics
/// Panics if the config file does not exist.
pub fn execute(self) {
let path: PathBuf = self.config_path.clone().into();
match Configuration::try_load(path.clone()) {
Ok(_) => {
info!("Updating config: {:?}", self);
let toml_str = fs::read_to_string(path.clone()).unwrap();
let table = toml_str.parse::<Table>().unwrap();
let keys = self.property.split('.').collect::<Vec<&str>>();
if !table.contains_key(keys[0]) {
println!("Key '{}' does not exist", keys[0]);
return;
}
let mut visitor = ConfigVisitor::new(keys, self.value);
let table = visitor.process(table);
let toml_str = toml::to_string(&table).unwrap();
fs::write(path, toml_str).unwrap();
}
Err(err) => {
error!("Error loading configuration file: {err:?}");
}
}
}
}
struct ConfigVisitor<'a> {
keys: Vec<&'a str>,
value: String,
in_array: bool,
data_in_array: Vec<Value>,
}
impl<'a> ConfigVisitor<'a> {
fn new(keys: Vec<&'a str>, value: String) -> Self {
Self {
keys,
value,
in_array: false,
data_in_array: vec![],
}
}
#[allow(clippy::needless_pass_by_value)]
fn process(&mut self, table: Table) -> Table {
let key = self.keys[0];
let value = table.get(key).unwrap();
self.process_value(table.clone(), value.clone(), 0)
}
fn process_value(&mut self, mut table: Table, value: Value, key_index: usize) -> Table {
let root_key = self.keys[key_index];
match value {
Value::String(str) => {
println!("Old value: {str}",);
let value = String::from(&self.value);
println!("New value: {value}",);
table.insert(root_key.to_string(), Value::String(value));
}
Value::Integer(i) => {
println!("Old value: {i}",);
let value = i64::from_str(&self.value).unwrap();
println!("New value: {value}",);
table.insert(root_key.to_string(), Value::Integer(value));
}
Value::Float(f) => {
println!("Old value: {f}",);
let value = f64::from_str(&self.value).unwrap();
println!("New value: {value}",);
table.insert(root_key.to_string(), Value::Float(value));
}
Value::Boolean(b) => {
println!("Old value: {b}",);
let value = bool::from_str(&self.value).unwrap();
println!("New value: {value}",);
table.insert(root_key.to_string(), Value::Boolean(value));
}
Value::Datetime(_) => {
println!("Datetime not supported");
}
Value::Array(ar) => {
self.in_array = true;
for value in ar {
self.process_value(table.clone(), value, key_index);
}
table.remove(root_key);
table.insert(
root_key.to_string(),
Value::Array(self.data_in_array.clone()),
);
self.data_in_array.clear();
self.in_array = false;
}
Value::Table(t) => {
let value = t.get(self.keys[key_index + 1]).cloned().unwrap();
let updated_table = self.process_value(t, value, key_index + 1);
if self.in_array {
self.data_in_array.push(updated_table.into());
return table.clone();
}
table.insert(root_key.to_string(), updated_table.into());
}
}
table
}
}