From 98514aa8008045ca98b00c13b0db55f18b606d6a Mon Sep 17 00:00:00 2001
From: dvermd <888-dvermd@users.noreply.git.duniter.org>
Date: Sun, 8 Mar 2020 10:22:06 +0100
Subject: [PATCH] wip: add explicit keypairs dependency for commands

---
 bin/dunitrust-server/src/cli.rs       |  5 ++++-
 lib/core/core/src/commands/dbex.rs    |  4 +++-
 lib/core/core/src/commands/keys.rs    |  8 +++++++-
 lib/core/core/src/commands/mod.rs     | 26 +++++++++++++++++++++++++-
 lib/core/core/src/commands/modules.rs |  8 +++++++-
 lib/core/core/src/commands/reset.rs   | 11 ++++++++++-
 lib/core/core/src/commands/start.rs   |  8 ++++++++
 lib/core/network/cli/sync.rs          |  7 +++++++
 8 files changed, 71 insertions(+), 6 deletions(-)

diff --git a/bin/dunitrust-server/src/cli.rs b/bin/dunitrust-server/src/cli.rs
index eaf19610..77117928 100644
--- a/bin/dunitrust-server/src/cli.rs
+++ b/bin/dunitrust-server/src/cli.rs
@@ -21,7 +21,8 @@ use durs_core::commands::modules::{DisableOpt, EnableOpt, ListModulesOpt};
 use durs_core::commands::reset::ResetOpt;
 use durs_core::commands::start::StartOpt;
 use durs_core::commands::{
-    DursCommand, DursCommandEnum, DursCoreCommand, DursCoreOptions, ExecutableModuleCommand,
+    CommandNeedKeypairs, DursCommand, DursCommandEnum, DursCoreCommand, DursCoreOptions,
+    ExecutableModuleCommand,
 };
 use durs_core::errors::DursCoreError;
 use durs_core::DursCore;
@@ -62,6 +63,8 @@ pub struct DursCliOpt {
     profile_name: Option<String>,
 }
 
+impl CommandNeedKeypairs for DursCliOpt {}
+
 impl ExecutableModuleCommand for DursCliOpt {
     /// Execute command
     fn execute_module_command(self, options: DursCoreOptions) -> Result<(), DursCoreError> {
diff --git a/lib/core/core/src/commands/dbex.rs b/lib/core/core/src/commands/dbex.rs
index f406bb75..b431c32e 100644
--- a/lib/core/core/src/commands/dbex.rs
+++ b/lib/core/core/src/commands/dbex.rs
@@ -15,7 +15,7 @@
 
 //! Durs-core cli : dbex subcommands.
 
-use crate::commands::DursExecutableCoreCommand;
+use crate::commands::{CommandNeedKeypairs, DursExecutableCoreCommand};
 use crate::dbex;
 use crate::errors::DursCoreError;
 use crate::DursCore;
@@ -34,6 +34,8 @@ pub struct DbExOpt {
     pub subcommand: DbExSubCommand,
 }
 
+impl CommandNeedKeypairs for DbExOpt {}
+
 #[derive(StructOpt, Debug, Clone)]
 /// dbex subcommands
 pub enum DbExSubCommand {
diff --git a/lib/core/core/src/commands/keys.rs b/lib/core/core/src/commands/keys.rs
index c91a56d2..842eaaa0 100644
--- a/lib/core/core/src/commands/keys.rs
+++ b/lib/core/core/src/commands/keys.rs
@@ -15,7 +15,7 @@
 
 //! Durs-core cli : keys subcommands.
 
-use crate::commands::DursExecutableCoreCommand;
+use crate::commands::{CommandNeedKeypairs, DursExecutableCoreCommand};
 use crate::errors::DursCoreError;
 use crate::DursCore;
 use clap::arg_enum;
@@ -36,6 +36,12 @@ pub struct KeysOpt {
     pub subcommand: KeysSubCommand,
 }
 
+impl CommandNeedKeypairs for KeysOpt {
+    fn needs_keypairs(&self) -> bool {
+        true
+    }
+}
+
 #[derive(StructOpt, Debug, Clone, Copy)]
 /// keys subcommands
 pub enum KeysSubCommand {
diff --git a/lib/core/core/src/commands/mod.rs b/lib/core/core/src/commands/mod.rs
index d6949a82..324ca466 100644
--- a/lib/core/core/src/commands/mod.rs
+++ b/lib/core/core/src/commands/mod.rs
@@ -70,11 +70,19 @@ pub trait DursExecutableCoreCommand {
 }
 
 /// Executable module command
-pub trait ExecutableModuleCommand {
+pub trait ExecutableModuleCommand: CommandNeedKeypairs {
     /// Execute module command
     fn execute_module_command(self, options: DursCoreOptions) -> Result<(), DursCoreError>;
 }
 
+/// Do this command use the keypairs
+pub trait CommandNeedKeypairs {
+    /// Do this command use the keypairs
+    fn needs_keypairs(&self) -> bool {
+        false
+    }
+}
+
 /// Dunitrust command with options
 pub struct DursCommand<T: ExecutableModuleCommand> {
     /// Dunitrust core options
@@ -149,6 +157,22 @@ pub enum DursCoreCommand {
     KeysOpt(KeysOpt),
 }
 
+impl CommandNeedKeypairs for DursCoreCommand {
+    /// Do this command use the keypairs
+    fn needs_keypairs(&self) -> bool {
+        match self {
+            DursCoreCommand::EnableOpt(opt) => opt.needs_keypairs(),
+            DursCoreCommand::DisableOpt(opt) => opt.needs_keypairs(),
+            DursCoreCommand::ListModulesOpt(opt) => opt.needs_keypairs(),
+            DursCoreCommand::StartOpt(opt) => opt.needs_keypairs(),
+            DursCoreCommand::SyncOpt(opt) => opt.needs_keypairs(),
+            DursCoreCommand::ResetOpt(opt) => opt.needs_keypairs(),
+            DursCoreCommand::DbExOpt(opt) => opt.needs_keypairs(),
+            DursCoreCommand::KeysOpt(opt) => opt.needs_keypairs(),
+        }
+    }
+}
+
 /// InvalidInput
 #[derive(Debug, Copy, Clone)]
 pub struct InvalidInput(&'static str);
diff --git a/lib/core/core/src/commands/modules.rs b/lib/core/core/src/commands/modules.rs
index 27f553f1..5b08d1d5 100644
--- a/lib/core/core/src/commands/modules.rs
+++ b/lib/core/core/src/commands/modules.rs
@@ -15,7 +15,7 @@
 
 //! Durs-core cli : modules manager subcommands.
 
-use crate::commands::DursExecutableCoreCommand;
+use crate::commands::{CommandNeedKeypairs, DursExecutableCoreCommand};
 use crate::errors::DursCoreError;
 use crate::DursCore;
 use durs_conf::{ChangeGlobalConf, DuRsConf};
@@ -31,6 +31,8 @@ pub struct EnableOpt {
     pub module_name: ModuleName,
 }
 
+impl CommandNeedKeypairs for EnableOpt {}
+
 impl DursExecutableCoreCommand for EnableOpt {
     #[inline]
     fn execute(self, mut durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> {
@@ -51,6 +53,8 @@ pub struct DisableOpt {
     pub module_name: ModuleName,
 }
 
+impl CommandNeedKeypairs for DisableOpt {}
+
 impl DursExecutableCoreCommand for DisableOpt {
     #[inline]
     fn execute(self, mut durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> {
@@ -99,3 +103,5 @@ impl ListModulesOpt {
         filters
     }
 }
+
+impl CommandNeedKeypairs for ListModulesOpt {}
diff --git a/lib/core/core/src/commands/reset.rs b/lib/core/core/src/commands/reset.rs
index 528cf531..4dc34ae8 100644
--- a/lib/core/core/src/commands/reset.rs
+++ b/lib/core/core/src/commands/reset.rs
@@ -16,7 +16,7 @@
 //! Durs-core cli : reset subcommand.
 
 use super::InvalidInput;
-use crate::commands::DursExecutableCoreCommand;
+use crate::commands::{CommandNeedKeypairs, DursExecutableCoreCommand};
 use crate::errors::DursCoreError;
 use crate::DursCore;
 use durs_conf::DuRsConf;
@@ -54,6 +54,15 @@ pub struct ResetOpt {
     pub reset_type: ResetType,
 }
 
+impl CommandNeedKeypairs for ResetOpt {
+    fn needs_keypairs(&self) -> bool {
+        match self.reset_type {
+            ResetType::All | ResetType::Conf => true,
+            ResetType::Datas => false,
+        }
+    }
+}
+
 impl DursExecutableCoreCommand for ResetOpt {
     fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError> {
         let profile_path = durs_core.soft_meta_datas.profile_path;
diff --git a/lib/core/core/src/commands/start.rs b/lib/core/core/src/commands/start.rs
index d5e35432..ea3ee1b8 100644
--- a/lib/core/core/src/commands/start.rs
+++ b/lib/core/core/src/commands/start.rs
@@ -15,7 +15,15 @@
 
 //! Durs-core cli : start subcommands.
 
+use crate::commands::CommandNeedKeypairs;
+
 #[derive(StructOpt, Debug, Copy, Clone)]
 #[structopt(name = "start", setting(structopt::clap::AppSettings::ColoredHelp))]
 /// start durs server
 pub struct StartOpt {}
+
+impl CommandNeedKeypairs for StartOpt {
+    fn needs_keypairs(&self) -> bool {
+        true
+    }
+}
diff --git a/lib/core/network/cli/sync.rs b/lib/core/network/cli/sync.rs
index 46a10e2b..08dc2349 100644
--- a/lib/core/network/cli/sync.rs
+++ b/lib/core/network/cli/sync.rs
@@ -47,3 +47,10 @@ pub struct SyncOpt {
     #[structopt(short = "u", long = "unsafe", hidden = true)]
     pub unsafe_mode: bool,
 }
+
+impl SyncOpt {
+    /// Do this command use the keypairs
+    pub fn needs_keypairs(&self) -> bool {
+        true
+    }
+}
-- 
GitLab