-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcoffee.rs
733 lines (672 loc) · 27.6 KB
/
coffee.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Coffee mod implementation
use std::collections::HashMap;
use std::fmt::Debug;
use std::vec::Vec;
use tokio::fs;
use async_trait::async_trait;
use clightningrpc_common::client::Client;
use clightningrpc_common::json_utils;
use clightningrpc_conf::{CLNConf, SyncCLNConf};
use log;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::process::Command;
use coffee_github::repository::Github;
use coffee_github::utils::git_checkout;
use coffee_lib::errors::CoffeeError;
use coffee_lib::plugin_manager::PluginManager;
use coffee_lib::repository::Repository;
use coffee_lib::types::response::*;
use coffee_lib::url::URL;
use coffee_lib::{commit_id, error, get_repo_info, sh};
use coffee_storage::model::repository::{Kind, Repository as RepositoryInfo};
use coffee_storage::nosql_db::NoSQlStorage;
use coffee_storage::storage::StorageManager;
use super::config;
use crate::config::CoffeeConf;
use crate::nurse::chain::RecoveryChainOfResponsibility;
use crate::CoffeeArgs;
pub type RepoName = String;
#[derive(Serialize, Deserialize)]
/// FIXME: move the list of plugin
/// and the list of repository inside this struct.
pub struct CoffeeStorageInfo {
pub config: config::CoffeeConf,
pub repositories: HashMap<RepoName, RepositoryInfo>,
}
impl From<&CoffeeManager> for CoffeeStorageInfo {
fn from(value: &CoffeeManager) -> Self {
let mut repos = HashMap::new();
// FIXME: improve the down cast
for (name, repo) in value.repos.iter() {
let repo = if let Some(git) = repo.as_any().downcast_ref::<Github>() {
RepositoryInfo::from(git)
} else {
panic!("this should never happens")
};
repos.insert(name.to_string(), repo);
}
CoffeeStorageInfo {
config: value.config.to_owned(),
repositories: repos, // FIXME: find a way to downcast
}
}
}
pub struct CoffeeManager {
pub config: config::CoffeeConf,
pub repos: HashMap<String, Box<dyn Repository + Send + Sync>>,
/// Core lightning configuration managed by coffee
pub coffee_cln_config: CLNConf,
/// Core lightning configuration that include the
/// configuration managed by coffee
pub cln_config: Option<CLNConf>,
/// storage instance to make all the plugin manager
/// information persistent on disk
pub storage: NoSQlStorage,
/// core lightning rpc connection
pub rpc: Option<Client>,
/// Recovery Strategies for the nurse command.
pub recovery_strategies: RecoveryChainOfResponsibility,
}
impl CoffeeManager {
pub async fn new(conf: &dyn CoffeeArgs) -> Result<Self, CoffeeError> {
let conf = CoffeeConf::new(conf).await?;
let mut coffee = CoffeeManager {
config: conf.clone(),
coffee_cln_config: CLNConf::new(conf.config_path, true),
repos: HashMap::new(),
storage: NoSQlStorage::new(&conf.root_path).await?,
cln_config: None,
rpc: None,
recovery_strategies: RecoveryChainOfResponsibility::new().await?,
};
coffee.inventory().await?;
Ok(coffee)
}
/// when coffee is configured, run an inventory to collect all the necessary information
/// about the coffee ecosystem.
async fn inventory(&mut self) -> Result<(), CoffeeError> {
let _ = self
.storage
.load::<CoffeeStorageInfo>(&self.config.network)
.await
.map(|store| {
self.config = store.config;
});
// FIXME: check if this exist in a better wai
let _ = self
.storage
.load::<HashMap<RepoName, RepositoryInfo>>("repositories")
.await
.map(|item| {
log::debug!("repositories in store {:?}", item);
item.iter().for_each(|repo| match repo.1.kind {
Kind::Git => {
let repo = Github::from(repo.1);
self.repos.insert(repo.name(), Box::new(repo));
}
});
});
if let Err(err) = self.coffee_cln_config.parse() {
log::error!("{}", err.cause);
}
if !self.config.skip_verify {
// Check for the chain of responsibility
let status = self.recovery_strategies.scan(self).await?;
log::debug!("Chain of responsibility status: {:?}", status);
// if any defect is found, we print a warning message (we don't take action)
if !status.defects.is_empty() {
return Err(
error!("Coffee found some defects in the configuration. Please run `coffee nurse` to fix them.
If you are want to skip the verification, please add the `--skip-verify ` flag to the command.")
);
};
}
self.load_cln_conf().await?;
log::debug!("cln conf {:?}", self.coffee_cln_config);
log::debug!("finish plugin manager inventory");
Ok(())
}
pub async fn cln<T: Serialize, U: DeserializeOwned + Debug>(
&self,
method: &str,
payload: T,
) -> Result<U, CoffeeError> {
if let Some(rpc) = &self.rpc {
let response = rpc
.send_request(method, payload)
.map_err(|err| error!("{}", &format!("{err}")))?;
log::debug!("cln answer with {:?}", response);
if let Some(err) = response.error {
return Err(error!("{}", &format!("cln error: {}", err.message)));
}
return Ok(response.result.unwrap());
}
Err(error!("rpc connection to core lightning not available"))
}
pub async fn start_plugin(&self, path: &str) -> Result<(), CoffeeError> {
let mut payload = json_utils::init_payload();
json_utils::add_str(&mut payload, "subcommand", "start");
json_utils::add_str(&mut payload, "plugin", path);
let response = self
.cln::<serde_json::Value, serde_json::Value>("plugin", payload)
.await?;
log::debug!("plugin registered: {response}");
Ok(())
}
pub async fn stop_plugin(&self, path: &str) -> Result<(), CoffeeError> {
let mut payload = json_utils::init_payload();
json_utils::add_str(&mut payload, "subcommand", "stop");
json_utils::add_str(&mut payload, "plugin", path);
let response = self
.cln::<serde_json::Value, serde_json::Value>("plugin", payload)
.await?;
log::debug!("plugin stopped: {response}");
Ok(())
}
pub fn storage_info(&self) -> CoffeeStorageInfo {
CoffeeStorageInfo::from(self)
}
pub async fn flush(&self) -> Result<(), CoffeeError> {
let store_info = self.storage_info();
self.storage
.store(&self.config.network, &store_info)
.await?;
self.storage
.store("repositories", &store_info.repositories)
.await?;
Ok(())
}
pub async fn update_conf(&self) -> Result<(), CoffeeError> {
self.coffee_cln_config.flush()?;
log::debug!("stored all the cln info in {}", self.coffee_cln_config);
Ok(())
}
pub async fn load_cln_conf(&mut self) -> Result<(), CoffeeError> {
if self.config.cln_config_path.is_none() {
return Ok(());
}
let root = self.config.cln_root.clone().unwrap();
// We check if there is some problem we the path that we know
if !fs::try_exists(root.clone()).await? {
return Err(error!("lightning root path `{}` do not exist", root));
} else if !fs::try_exists(format!("{root}/{}", self.config.network)).await? {
return Err(error!(
"lightning network path `{root}/{}` do not exist",
self.config.network
));
}
// All safe, we can move with the logic
let rpc = Client::new(format!("{root}/{}/lightning-rpc", self.config.network));
self.rpc = Some(rpc);
let path = self.config.cln_config_path.clone().unwrap();
let mut file = CLNConf::new(path.clone(), true);
log::info!("looking for the cln config: {path}");
file.parse()
.map_err(|err| CoffeeError::new(err.core, &err.cause))?;
log::trace!("{:?}", file.fields);
self.cln_config = Some(file);
Ok(())
}
pub async fn setup_with_cln(&mut self, cln_dir: &str) -> Result<(), CoffeeError> {
if self.cln_config.is_some() {
log::warn!("you are overriding the previous set up");
}
let path_with_network = format!("{cln_dir}/{}/config", self.config.network);
log::info!("configure coffee in the following cln config {path_with_network}");
self.config.cln_config_path = Some(path_with_network);
self.config.cln_root = Some(cln_dir.to_owned());
self.load_cln_conf().await?;
let mut conf = self.cln_config.clone().unwrap();
conf.add_subconf(self.coffee_cln_config.clone())
.map_err(|err| error!("{}", &err.cause))?;
conf.flush()?;
Ok(())
}
}
#[async_trait]
impl PluginManager for CoffeeManager {
async fn configure(&mut self) -> Result<(), CoffeeError> {
log::debug!("plugin configured");
Ok(())
}
async fn install(
&mut self,
plugin: &str,
branch: Option<String>,
verbose: bool,
try_dynamic: bool,
) -> Result<(), CoffeeError> {
let mut plugins = self.config.plugins.clone();
plugins.retain(|p| p.name().eq(plugin));
if !plugins.is_empty() {
return Err(error!("Plugin with name `{plugin}` already installed"));
}
log::debug!("installing plugin: {plugin}");
// keep track if the plugin is successfully installed
for repo in self.repos.values() {
if let Some(mut plugin) = repo.get_plugin_by_name(plugin) {
log::trace!("{:?}", plugin);
if try_dynamic && plugin.important() {
return Err(error!(
"plugin is important, can't be dynamically installed"
));
}
// old_root_path is the path where the plugin is cloned and currently stored
// eg. ~/.coffee/repositories/<repo_name>/<plugin_name>
let old_root_path = plugin.root_path.clone();
// new_root_path is the path where the plugin will be installed specific to the network
// eg. ~/.coffee/<network>/plugins/<plugin_name>
let new_root_path = format!(
"{}/{}/plugins/{}",
self.config.root_path,
self.config.network,
plugin.name()
);
log::debug!(
"Start! copying directory from {} inside the new one {}",
old_root_path,
new_root_path
);
let script = format!("cp -r {old_root_path} {new_root_path}");
sh!(self.config.root_path.clone(), script, verbose);
log::debug!(
"Done! copying directory from {} inside the new one {}",
old_root_path,
new_root_path
);
let old_exec_path = plugin.exec_path.clone();
let plugin_conf_key = if plugin.important() {
"important-plugin"
} else {
"plugin"
};
match old_exec_path.strip_prefix(&old_root_path) {
Some(relative_path) => {
let new_exec_path = format!("{}{}", new_root_path, relative_path);
plugin.root_path = new_root_path;
plugin.exec_path = new_exec_path;
if let Some(branch) = branch {
// FIXME: Where we store the date? how we manage it?
let (commit, _) =
git_checkout(&plugin.root_path, &branch, verbose).await?;
plugin.commit = Some(commit);
}
log::debug!("plugin: {:?}", plugin);
let path = plugin.configure(verbose).await?;
log::debug!("runnable plugin path {path}");
if !try_dynamic {
// mark the plugin enabled
plugin.enabled = Some(true);
self.config.plugins.push(plugin);
log::debug!("path coffee conf: {}", self.coffee_cln_config.path);
self.coffee_cln_config
.add_conf(plugin_conf_key, &path.to_owned())
.map_err(|err| error!("{}", err.cause))?;
log::debug!("coffee conf updated: {}", self.coffee_cln_config);
self.flush().await?;
self.update_conf().await?;
} else {
self.config.plugins.push(plugin);
self.flush().await?;
self.start_plugin(&path).await?;
}
return Ok(());
}
None => return Err(error!("exec path not found")),
};
}
}
Err(error!(
"plugin `{plugin}` are not present inside the repositories"
))
}
async fn remove(&mut self, plugin: &str) -> Result<CoffeeRemove, CoffeeError> {
log::debug!("removing plugin: {plugin}");
let plugins = &mut self.config.plugins;
if let Some(index) = plugins.iter().position(|x| x.name() == plugin) {
let plugin = plugins[index].clone();
let exec_path = plugin.exec_path.clone();
let root_path = plugin.root_path.clone();
let cloned_repositories_path = format!("{}/repositories", self.config.root_path,);
// make sure that we are not deleting the cloned repositories
if !root_path.contains(&cloned_repositories_path) {
fs::remove_dir_all(root_path).await?;
}
log::debug!("runnable plugin path: {exec_path}");
plugins.remove(index);
log::debug!("coffee cln config: {}", self.coffee_cln_config);
let plugin_conf_key = if plugin.important() {
"important-plugin"
} else {
"plugin"
};
let remove_config = self
.coffee_cln_config
.rm_conf(plugin_conf_key, Some(&exec_path.to_owned()));
if let Err(err) = remove_config {
// if this is true, we are probably a dynamic plugin:
if err.cause.contains("field with `plugin` not present") {
if let Err(e) = self.stop_plugin(&exec_path).await {
log::warn!("{}", e);
};
} else {
return Err(error!("{}", &err.cause));
}
}
self.flush().await?;
self.update_conf().await?;
Ok(CoffeeRemove { plugin })
} else {
return Err(error!("plugin `{plugin}` is already not installed"));
}
}
async fn list(&mut self) -> Result<CoffeeList, CoffeeError> {
Ok(CoffeeList {
plugins: self.config.plugins.clone(),
})
}
async fn upgrade(&mut self, repo: &str, verbose: bool) -> Result<CoffeeUpgrade, CoffeeError> {
// TODO: upgrade should now be able to upgrade a single plugin
// without affecting other plugins installed from the same repo
let repository = self
.repos
.get_mut(repo)
.ok_or_else(|| error!("Repository with name: `{}` not found", repo))?;
let status = repository.upgrade(&self.config.plugins, verbose).await?;
// if status is not up to date, we need to update the plugins as well
match status.status {
UpgradeStatus::Updated(_, _) => {
for plugins in status.plugins_effected.iter() {
self.remove(plugins).await?;
self.install(plugins, None, verbose, false).await?;
}
}
_ => {}
}
self.flush().await?;
Ok(status)
}
async fn setup(&mut self, cln_dir: &str) -> Result<(), CoffeeError> {
self.setup_with_cln(cln_dir).await?;
log::info!("cln configured");
self.flush().await?;
Ok(())
}
async fn add_remote(&mut self, name: &str, url: &str) -> Result<(), CoffeeError> {
// FIXME: we should allow some error here like
// for the add remote command the no found error for the `repository`
// directory is fine.
if self.repos.contains_key(name) {
return Err(error!("repository with name: {name} already exists"));
}
let url = URL::new(&self.config.root_path, url, name);
log::debug!("remote adding: {} {}", name, &url.url_string);
let mut repo = Github::new(name, &url);
repo.init().await?;
self.repos.insert(repo.name(), Box::new(repo));
log::debug!("remote added: {} {}", name, &url.url_string);
self.flush().await?;
Ok(())
}
async fn rm_remote(&mut self, name: &str) -> Result<(), CoffeeError> {
log::debug!("remote removing: {}", name);
match self.repos.get(name) {
Some(repo) => {
let remote_repo = repo.list().await?;
let repo_path = repo.url().path_string;
let plugins = self.config.plugins.clone();
for plugin in &remote_repo {
if let Some(ind) = plugins
.iter()
.position(|elem| elem.name() == *plugin.name())
{
let plugin_name = &plugins[ind].name().clone();
match self.remove(plugin_name).await {
Ok(_) => {}
Err(err) => return Err(err),
}
}
}
fs::remove_dir_all(repo_path).await?;
self.repos.remove(name);
log::debug!("remote removed: {}", name);
self.flush().await?;
}
None => {
return Err(error!("repository with name: {name} not found"));
}
};
Ok(())
}
async fn list_remotes(&mut self) -> Result<CoffeeRemote, CoffeeError> {
let mut remote_list = Vec::new();
for repo in self.repos.values() {
let repository = git2::Repository::open(repo.url().path_string.as_str())
.map_err(|err| error!("{}", err.message()))?;
let (commit, date) = get_repo_info!(repository);
remote_list.push(CoffeeListRemote {
local_name: repo.name(),
url: repo.url().url_string,
plugins: repo.list().await?,
commit_id: Some(commit),
date: Some(date),
});
}
Ok(CoffeeRemote {
remotes: Some(remote_list),
})
}
async fn get_plugins_in_remote(&self, name: &str) -> Result<CoffeeList, CoffeeError> {
log::debug!("Listing plugins for repository: {}", name);
let repo = self
.repos
.get(name)
.ok_or_else(|| error!("repository with name: {name} not found"))?;
let plugins = repo.list().await?;
Ok(CoffeeList { plugins })
}
async fn show(&mut self, plugin: &str) -> Result<CoffeeShow, CoffeeError> {
for repo in self.repos.values() {
if let Some(plugin) = repo.get_plugin_by_name(plugin) {
// FIXME: there are more README file options?
let readme_path = format!("{}/README.md", plugin.root_path);
let contents = fs::read_to_string(readme_path).await?;
return Ok(CoffeeShow { readme: contents });
}
}
let err = error!(
"{}",
&format!("plugin `{plugin}` are not present inside the repositories"),
);
Err(err)
}
async fn search(&mut self, plugin: &str) -> Result<CoffeeSearch, CoffeeError> {
for repo in self.repos.values() {
if let Some(plugin) = repo.get_plugin_by_name(plugin) {
return Ok(CoffeeSearch {
repository_url: repo.url().url_string,
plugin,
});
}
}
let err = CoffeeError::new(404, &format!("unable to locate plugin `{plugin}`"));
Err(err)
}
async fn nurse_verify(&self) -> Result<ChainOfResponsibilityStatus, CoffeeError> {
self.recovery_strategies.scan(self).await
}
async fn nurse(&mut self) -> Result<CoffeeNurse, CoffeeError> {
let status = self.recovery_strategies.scan(self).await?;
let mut nurse_actions: Vec<NurseStatus> = vec![];
for defect in status.defects.iter() {
log::debug!("defect: {:?}", defect);
match defect {
Defect::RepositoryLocallyAbsent(repos) => {
let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?;
nurse_actions.append(&mut actions);
}
}
}
let mut nurse = CoffeeNurse {
status: nurse_actions,
};
nurse.organize();
Ok(nurse)
}
async fn patch_repository_locally_absent(
&mut self,
repos: Vec<String>,
) -> Result<Vec<NurseStatus>, CoffeeError> {
// initialize the nurse actions
let mut nurse_actions: Vec<NurseStatus> = vec![];
// for every repository that is absent locally
// we try to recover it.
// There are 2 cases:
// 1. the repository can be recovered from the remote
// 2. the repository can't be recovered from the remote. In this case
// we remove the repository from the coffee configuration.
for repo_name in repos.iter() {
// Get the repository from the name
let repo = self
.repos
.get_mut(repo_name)
.ok_or_else(|| error!("repository with name: {repo_name} not found"))?;
match repo.recover().await {
Ok(_) => {
log::info!("repository {} recovered", repo_name.clone());
nurse_actions.push(NurseStatus::RepositoryLocallyRestored(vec![
repo_name.clone()
]));
}
Err(err) => {
log::debug!("error while recovering repository {repo_name}: {err}");
// We make sure that the repository folder is removed
// from local storage.
// Maybe when trying to recover the repository,
// we have created the folder but we were not able
// to clone the repository.
let repo_path = repo.url().path_string;
// This shouldn't return an error if the repository
// is not present locally.
let _ = fs::remove_dir_all(repo_path).await;
log::info!("removing repository {}", repo_name.clone());
self.repos.remove(repo_name);
log::debug!("remote removed: {}", repo_name);
self.flush().await?;
nurse_actions.push(NurseStatus::RepositoryLocallyRemoved(vec![
repo_name.clone()
]));
}
}
}
Ok(nurse_actions)
}
async fn tip(&mut self, plugin: &str, amount_msat: u64) -> Result<CoffeeTip, CoffeeError> {
let plugins = self
.config
.plugins
.iter()
.filter(|repo_plugin| plugin == repo_plugin.name())
.collect::<Vec<_>>();
let plugin = plugins.first().ok_or(error!(
"No plugin with name `{plugin}` found in the plugins installed"
))?;
let Some(tipping) = plugin.tipping_info() else {
return Err(error!("Plugin `{plugin}` has no tipping information"));
};
// FIXME write a tip_plugin method as method
#[derive(Debug, Deserialize)]
struct FetchResult {
invoice: String,
}
let invoice: FetchResult = self
.cln(
"fetchinvoice",
json!({
"offer": tipping.bolt12,
"amount_msat": amount_msat,
}),
)
.await?;
let pay: PayResponse = self
.cln(
"pay",
json!({
"bolt11": invoice.invoice,
}),
)
.await?;
let tip = CoffeeTip {
for_plugin: plugin.name(),
invoice: invoice.invoice,
status: pay.status,
destination: pay.destination,
amount_msat: pay.amount_msat,
amount_sent_msat: pay.amount_sent_msat,
warning_partial_completion: pay.warning_partial_completion,
};
Ok(tip)
}
async fn disable(&mut self, plugin: &str) -> Result<(), CoffeeError> {
log::debug!("disabling plugin: {plugin}");
let plugin = self
.config
.plugins
.iter_mut()
.find(|repo_plugin| plugin == repo_plugin.name())
.ok_or(error!(
"No plugin with name `{plugin}` found in the plugins installed"
))?;
log::debug!("plugin: {:?}", plugin);
if plugin.enabled == Some(false) {
return Err(error!("Plugin `{plugin}` is already disabled"));
}
self.coffee_cln_config
.add_conf("disable-plugin", &plugin.exec_path)
.map_err(|err| error!("{}", err.cause))?;
log::debug!(
"Plugin {} was removed from CLN configuration successfully",
plugin.name()
);
plugin.enabled = Some(false);
self.flush().await?;
self.update_conf().await?;
Ok(())
}
async fn enable(&mut self, plugin: &str) -> Result<(), CoffeeError> {
log::debug!("enabling plugin: {plugin}");
let plugin = self
.config
.plugins
.iter_mut()
.find(|repo_plugin| plugin == repo_plugin.name())
.ok_or(error!(
"No plugin with name `{plugin}` found in the plugins installed"
))?;
log::debug!("plugin: {:?}", plugin);
if plugin.enabled.is_none() || plugin.enabled == Some(true) {
return Err(error!(
"Plugin `{plugin}` is already enabled or enabled by default"
));
}
self.coffee_cln_config
.rm_conf("disable-plugin", Some(&plugin.exec_path))
.map_err(|err| error!("{}", err.cause))?;
log::debug!(
"Plugin {} was added to CLN configuration successfully",
plugin.name()
);
plugin.enabled = Some(true);
self.flush().await?;
self.update_conf().await?;
Ok(())
}
}
// FIXME: we need to move on but this is not safe and with the coffee
// implementation is not true!
unsafe impl Send for CoffeeManager {}
unsafe impl Sync for CoffeeManager {}
unsafe impl Send for CoffeeStorageInfo {}
unsafe impl Sync for CoffeeStorageInfo {}