redgold_schema/data_folder/
mod.rs

1use crate::config_data::ConfigData;
2use crate::helpers::easy_json::{json_from, EasyJsonDeser};
3use crate::servers::ServerOldFormat;
4use crate::structs::{NetworkEnvironment, Transaction};
5use crate::{ErrorInfoContext, RgResult};
6use std::path::PathBuf;
7
8#[derive(Clone, Debug)]
9pub struct EnvDataFolder {
10    pub path: PathBuf
11}
12
13impl EnvDataFolder {
14
15    pub fn mnemonic_no_tokio(&self) -> RgResult<String> {
16        std::fs::read_to_string(self.mnemonic_path()).error_info("Bad mnemonic read")
17    }
18
19    pub fn backups(&self) -> PathBuf {
20        self.path.join("backups")
21    }
22
23    pub fn backups_ds(&self) -> PathBuf {
24        self.path.join("backups-ds")
25    }
26
27    pub fn data_store_path(&self) -> PathBuf {
28        self.path.join("data_store.sqlite")
29    }
30
31    pub fn bdk_sled_path(&self) -> PathBuf {
32        self.path.join("bdk_sled")
33    }
34
35    pub fn monero_wallet_dir(&self) -> PathBuf {
36        self.path.join("monerow")
37    }
38
39    pub fn monero_cli_wallet_dir(&self) -> PathBuf {
40        self.path.join("monero_cli_wallet")
41    }
42
43    pub fn monero_wallet_expect(&self) -> PathBuf {
44        self.path.join("wallet.exp")
45    }
46
47    pub fn bdk_sled_path2(&self) -> PathBuf {
48        self.path.join("bdk_sled2")
49    }
50
51    pub fn mnemonic_path(&self) -> PathBuf {
52        self.path.join("mnemonic")
53    }
54
55    pub fn peer_tx(&self) -> RgResult<Transaction> {
56        let contents = std::fs::read_to_string(self.peer_tx_path()).error_info("Bad peer tx read")?;
57        json_from(&*contents)
58    }
59
60    pub fn peer_id_path(&self) -> PathBuf {
61        self.path.join("peer_id")
62    }
63
64    pub fn peer_tx_path(&self) -> PathBuf {
65        self.path.join("peer_tx")
66    }
67
68    pub fn metrics_list(&self) -> PathBuf {
69        self.path.join("metrics_list")
70    }
71
72    pub fn targets(&self) -> PathBuf {
73        self.path.join("targets.json")
74    }
75    pub fn parquet_exports(&self) -> PathBuf {
76        self.path.join("parquet_exports")
77    }
78    pub fn parquet_tx(&self) -> PathBuf {
79        self.parquet_exports().join("transactions")
80    }
81    pub fn parquet_self_observations(&self) -> PathBuf {
82        self.parquet_exports().join("observations")
83    }
84
85    pub fn servers_path(&self) -> PathBuf {
86        self.path.join("servers")
87    }
88
89    pub fn servers(&self) -> RgResult<Vec<ServerOldFormat>> {
90        ServerOldFormat::parse_from_file(self.servers_path())
91    }
92
93    pub fn multiparty_import(&self) -> PathBuf {
94        self.path.join("multiparty-import.csv")
95    }
96
97    // Change to cert.pem
98    pub fn cert_path(&self) -> PathBuf {
99        //self.path.join("certificate.crt")
100        PathBuf::from("/etc/letsencrypt/live/lb.redgold.io/fullchain.pem")
101    }
102
103    // Change to privkey.pem
104    pub fn key_path(&self) -> PathBuf {
105        // self.path.join("private_key.key")
106        PathBuf::from("/etc/letsencrypt/live/lb.redgold.io/privkey.pem")
107    }
108
109    pub fn ensure_exists(&self) -> &Self {
110        std::fs::create_dir_all(&self.path).ok();
111        self
112    }
113
114    pub fn delete(&self) -> &Self {
115        std::fs::remove_dir_all(&self.path).ok();
116        self
117    }
118
119
120}
121
122#[derive(Clone, Debug)]
123pub struct DataFolder {
124    pub path: PathBuf,
125}
126
127impl DataFolder {
128
129    pub fn from_string(path: String) -> Self {
130        Self{path: PathBuf::from(path)}
131    }
132
133    pub fn from_path(path: PathBuf) -> Self {
134        Self{path}
135    }
136
137    pub fn all(&self) -> EnvDataFolder {
138        self.by_env(NetworkEnvironment::All)
139    }
140
141    pub fn config_path(&self) -> PathBuf {
142        self.path.join("config.toml")
143    }
144
145    pub fn config(&self) -> Option<RgResult<ConfigData>> {
146        std::fs::read_to_string(self.config_path()).ok().map(|s| toml::from_str(&s).error_info("Bad config read"))
147    }
148
149    pub fn write_config(&self, config: &ConfigData) -> RgResult<()> {
150        let string = toml::to_string(config).error_info("Bad config write")?;
151        std::fs::write(self.config_path(), string).error_info("Bad config write")
152    }
153
154    pub fn by_env(&self, env: NetworkEnvironment) -> EnvDataFolder {
155        let path = self.path.join(env.to_std_string());
156        let ret = EnvDataFolder { path };
157        // TODO: Remove this
158        ret.ensure_exists();
159        ret
160    }
161
162    pub fn target(id: u32) -> Self {
163        let cwd = std::env::current_dir().expect("Current dir");
164        // let cwd_target = cwd.join("../../../../target");
165        let cwd_target = cwd.join("target");
166        Self{path: cwd_target.join(format!("node_{}", id))}
167    }
168
169    pub fn ensure_exists(&self) -> &Self {
170        let path = &self.path;
171        let err_path = path.to_string_lossy().to_string();
172        std::fs::create_dir_all(&path).expect(&*format!("Failed to create data folder {}", err_path));
173        self
174    }
175
176    pub fn delete(&self) -> &Self {
177        std::fs::remove_dir_all(&self.path).ok();
178        self
179    }
180
181}
182
183#[test]
184fn debug() {
185
186}