use std::path::{Path, PathBuf};
use redgold_schema::{ErrorInfoContext, json_from, RgResult, structs};
use redgold_schema::structs::{ErrorInfo, NetworkEnvironment, Transaction};
use redgold_data::data_store::DataStore;
use redgold_schema::servers::Server;
#[derive(Clone, Debug)]
pub struct EnvDataFolder {
pub path: PathBuf
}
impl EnvDataFolder {
pub fn backups(&self) -> PathBuf {
self.path.join("backups")
}
pub fn data_store_path(&self) -> PathBuf {
self.path.join("data_store.sqlite")
}
pub fn mnemonic_path(&self) -> PathBuf {
self.path.join("mnemonic")
}
pub async fn mnemonic(&self) -> RgResult<String> {
tokio::fs::read_to_string(self.mnemonic_path()).await.error_info("Bad mnemonic read")
}
pub fn peer_tx(&self) -> RgResult<Transaction> {
let contents = std::fs::read_to_string(self.peer_tx_path()).error_info("Bad peer tx read")?;
json_from(&*contents)
}
pub fn peer_id_path(&self) -> PathBuf {
self.path.join("peer_id")
}
pub fn peer_tx_path(&self) -> PathBuf {
self.path.join("peer_tx")
}
pub fn metrics_list(&self) -> PathBuf {
self.path.join("metrics_list")
}
pub fn targets(&self) -> PathBuf {
self.path.join("targets.json")
}
pub fn parquet_exports(&self) -> PathBuf {
self.path.join("parquet_exports")
}
pub fn parquet_tx(&self) -> PathBuf {
self.parquet_exports().join("transactions")
}
pub fn parquet_self_observations(&self) -> PathBuf {
self.parquet_exports().join("observations")
}
pub fn servers_path(&self) -> PathBuf {
self.path.join("servers")
}
pub fn servers(&self) -> RgResult<Vec<Server>> {
Server::parse_from_file(self.servers_path())
}
pub async fn multiparty_import_str(&self) -> RgResult<String> {
let mp_import = self.multiparty_import();
tokio::fs::read_to_string(mp_import).await.error_info("Failed to read multiparty import")
}
pub fn multiparty_import(&self) -> PathBuf {
self.path.join("multiparty-import.csv")
}
pub async fn remove_multiparty_import(&self) -> RgResult<()> {
tokio::fs::remove_file(self.multiparty_import()).await.error_info("Failed to remove multiparty import")
}
pub fn cert_path(&self) -> PathBuf {
PathBuf::from("/etc/letsencrypt/live/lb.redgold.io/fullchain.pem")
}
pub async fn cert(&self) -> Result<Vec<u8>, ErrorInfo> {
tokio::fs::read(self.cert_path()).await.error_info("Missing cert")
.or(
tokio::fs::read(self.path.join("certificate.crt")).await
.error_info("Missing cert")
)
}
pub async fn key(&self) -> Result<Vec<u8>, ErrorInfo> {
tokio::fs::read(self.key_path()).await
.error_info("Missing key")
.or(
tokio::fs::read(self.path.join("private_key.key")).await
.error_info("Missing key")
)
}
pub fn key_path(&self) -> PathBuf {
PathBuf::from("/etc/letsencrypt/live/lb.redgold.io/privkey.pem")
}
pub fn ensure_exists(&self) -> &Self {
std::fs::create_dir_all(&self.path).ok();
self
}
pub fn delete(&self) -> &Self {
std::fs::remove_dir_all(&self.path).ok();
self
}
pub async fn data_store(&self) -> DataStore {
DataStore::from_file_path(self.data_store_path().to_str().expect("Data store path").to_string()).await
}
}
#[derive(Clone, Debug)]
pub struct DataFolder {
pub path: PathBuf,
}
impl DataFolder {
pub fn from_string(path: String) -> Self {
Self{path: PathBuf::from(path)}
}
pub fn from_path(path: PathBuf) -> Self {
Self{path}
}
pub fn all(&self) -> EnvDataFolder {
self.by_env(NetworkEnvironment::All)
}
pub fn by_env(&self, env: NetworkEnvironment) -> EnvDataFolder {
let path = self.path.join(env.to_std_string());
let ret = EnvDataFolder { path };
ret.ensure_exists();
ret
}
pub fn target(id: u32) -> Self {
let cwd = std::env::current_dir().expect("Current dir");
let cwd_target = cwd.join("target");
Self{path: cwd_target.join(format!("node_{}", id))}
}
pub fn ensure_exists(&self) -> &Self {
std::fs::create_dir_all(&self.path).expect("Failed to create data folder");
self
}
pub fn delete(&self) -> &Self {
std::fs::remove_dir_all(&self.path).ok();
self
}
}
#[test]
fn debug() {
}