mod.rs 3.6 KB
Newer Older
Éloïs's avatar
Éloïs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//  Copyright (C) 2018  The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Define durs-core cli subcommands options.

pub mod dbex;
inso's avatar
inso committed
19
pub mod keys;
Éloïs's avatar
Éloïs committed
20 21 22 23
pub mod modules;
pub mod reset;
pub mod start;

24
use crate::errors::DursCoreError;
25
use crate::DursCore;
26 27
pub use dbex::*;
use durs_conf::DuRsConf;
28
pub use durs_network::cli::sync::SyncOpt;
29
pub use keys::KeysOpt;
Éloïs's avatar
Éloïs committed
30
use log::Level;
31 32 33
pub use modules::*;
pub use reset::*;
pub use start::*;
34
use std::path::PathBuf;
Éloïs's avatar
Éloïs committed
35

36 37
/// Durs core options
pub struct DursCoreOptions {
38
    /// Keypairs file path
39 40 41
    pub keypairs_file: Option<PathBuf>,
    /// Set log level.
    pub logs_level: Option<Level>,
Éloïs's avatar
Éloïs committed
42
    /// Print logs in standard output
43
    pub log_stdout: bool,
44
    /// Set a custom user profile name
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
    pub profile_name: Option<String>,
    /// Path where user profiles are persisted
    pub profiles_path: Option<PathBuf>,
}

/// Durs executable command
pub trait DursExecutableCoreCommand {
    /// Execute Durs command
    fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError>;
}

/// Executable module command
pub trait ExecutableModuleCommand {
    /// Execute module command
    fn execute_module_command(self, options: DursCoreOptions) -> Result<(), DursCoreError>;
}

/// Durs command with options
pub struct DursCommand<T: ExecutableModuleCommand> {
    /// Durs core options
    pub options: DursCoreOptions,
    /// Durs command
    pub command: DursCommandEnum<T>,
}

/// Durs command
pub enum DursCommandEnum<T: ExecutableModuleCommand> {
    /// Core command
    Core(DursCoreCommand),
    /// Other command
    Other(T),
}

impl<T: ExecutableModuleCommand> DursCommand<T> {
    /// Execute Durs command
80 81 82 83 84 85
    pub fn execute<PlugFunc>(
        self,
        soft_name: &'static str,
        soft_version: &'static str,
        plug_modules: PlugFunc,
    ) -> Result<(), DursCoreError>
86 87 88 89
    where
        PlugFunc: FnMut(&mut DursCore<DuRsConf>) -> Result<(), DursCoreError>,
    {
        match self.command {
90 91 92 93 94 95 96 97
            DursCommandEnum::Core(core_cmd) => DursCore::execute_core_command(
                core_cmd,
                self.options,
                vec![],
                plug_modules,
                soft_name,
                soft_version,
            ),
98 99 100
            DursCommandEnum::Other(cmd) => cmd.execute_module_command(self.options),
        }
    }
Éloïs's avatar
Éloïs committed
101 102 103 104
}

#[derive(StructOpt, Debug)]
/// Core cli subcommands
105
pub enum DursCoreCommand {
106
    /// Enable a module
Éloïs's avatar
Éloïs committed
107
    EnableOpt(EnableOpt),
108
    /// Disable a module
Éloïs's avatar
Éloïs committed
109
    DisableOpt(DisableOpt),
110
    /// List available modules
Éloïs's avatar
Éloïs committed
111
    ListModulesOpt(ListModulesOpt),
112
    /// Start node
Éloïs's avatar
Éloïs committed
113
    StartOpt(StartOpt),
114
    /// Synchronize
Éloïs's avatar
Éloïs committed
115
    SyncOpt(SyncOpt),
116
    /// Reset data or conf or all
Éloïs's avatar
Éloïs committed
117
    ResetOpt(ResetOpt),
118
    /// Database explorer
Éloïs's avatar
Éloïs committed
119
    DbExOpt(DbExOpt),
120
    /// Keys operations
inso's avatar
inso committed
121
    KeysOpt(KeysOpt),
Éloïs's avatar
Éloïs committed
122 123 124 125 126 127 128 129 130 131 132
}

/// InvalidInput
#[derive(Debug, Copy, Clone)]
pub struct InvalidInput(&'static str);

impl ToString for InvalidInput {
    fn to_string(&self) -> String {
        String::from(self.0)
    }
}