mod.rs 3.62 KB
Newer Older
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;
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;
30
use log::Level;
31 32 33
pub use modules::*;
pub use reset::*;
pub use start::*;
34
use std::path::PathBuf;
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>,
42
    /// Print logs in standard output
43
    pub log_stdout: bool,
44
    /// Set a custom user profile name
45 46 47
    pub profile_name: Option<String>,
    /// Path where user profiles are persisted
    pub profiles_path: Option<PathBuf>,
Jonas SPRENGER's avatar
Jonas SPRENGER committed
48
    //TODO: add env prefix
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
}

/// 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
81 82 83 84 85 86
    pub fn execute<PlugFunc>(
        self,
        soft_name: &'static str,
        soft_version: &'static str,
        plug_modules: PlugFunc,
    ) -> Result<(), DursCoreError>
87 88 89 90
    where
        PlugFunc: FnMut(&mut DursCore<DuRsConf>) -> Result<(), DursCoreError>,
    {
        match self.command {
91 92 93 94 95 96 97 98
            DursCommandEnum::Core(core_cmd) => DursCore::execute_core_command(
                core_cmd,
                self.options,
                vec![],
                plug_modules,
                soft_name,
                soft_version,
            ),
99 100 101
            DursCommandEnum::Other(cmd) => cmd.execute_module_command(self.options),
        }
    }
102 103 104 105
}

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

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

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