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
// Copyright 2017 The Exonum Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Command line commands utilities.

use clap;
use toml::Value;
use serde::{Serialize, Deserialize};

use std::str::FromStr;
use std::error::Error;
use std::collections::BTreeMap;

use blockchain::Service;
use self::internal::NotFoundInMap;

pub use self::builder::NodeBuilder;
pub use self::details::{Run, Finalize, GenerateNodeConfig, GenerateCommonConfig, GenerateTestnet};
pub use self::shared::{AbstractConfig, NodePublicConfig, CommonConfigTemplate, NodePrivateConfig};

mod shared;
mod builder;
mod details;
mod internal;
mod clap_backend;

/// Default port value.
pub const DEFAULT_EXONUM_LISTEN_PORT: u16 = 6333;

/// Name of the `Command`.
pub type CommandName = &'static str;

/// `Argument` with name.
#[derive(Clone, Copy, Debug)]
pub struct NamedArgument {
    /// Short argument name, for example `-a`.
    pub short_name: Option<&'static str>,
    /// Long argument name, for example `--long-arg`.
    pub long_name: &'static str,
    /// If `multiple` is true, then argument has more than one value.
    pub multiple: bool,
}

/// Possible types of the arguments.
#[derive(Clone, Copy, Debug)]
pub enum ArgumentType {
    /// Unnamed positional argument.
    Positional,
    /// Named argument.
    Named(NamedArgument),
}

/// Abstraction to represent arguments in command line.
#[derive(Clone, Copy, Debug)]
pub struct Argument {
    /// Name of the current argument. This name is used in `context.arg(name)`.
    pub name: &'static str,
    /// Explains how this argument is represented.
    pub argument_type: ArgumentType,
    /// Explains if the argument required or not.
    pub required: bool,
    /// Help message.
    pub help: &'static str,
}

impl Argument {
    /// Creates a new argument with `long` and optionally `short` names.
    pub fn new_named<T>(
        name: &'static str,
        required: bool,
        help: &'static str,
        short_name: T,
        long_name: &'static str,
        multiple: bool,
    ) -> Argument
    where
        T: Into<Option<&'static str>>,
    {
        Argument {
            argument_type: ArgumentType::Named(NamedArgument {
                short_name: short_name.into(),
                long_name,
                multiple,
            }),
            name,
            help,
            required,
        }
    }

    /// Creates a new positional argument.
    pub fn new_positional(name: &'static str, required: bool, help: &'static str) -> Argument {
        Argument {
            argument_type: ArgumentType::Positional,
            name,
            help,
            required,
        }
    }
}

/// `Context` is a type, used to keep some values from `Command` into
/// `CommandExtension` and vice verse.
#[derive(PartialEq, Debug, Clone, Default)]
pub struct Context {
    args: BTreeMap<String, String>,
    multiple_args: BTreeMap<String, Vec<String>>,
    variables: BTreeMap<String, Value>,
}

impl Context {
    fn new_from_args(args: &[Argument], matches: &clap::ArgMatches) -> Context {
        let mut context = Context::default();
        for arg in args {
            // processing multiple value arguments make code ugly =(
            match arg.argument_type {
                ArgumentType::Named(detail) if detail.multiple => {
                    if let Some(values) = matches.values_of(&arg.name) {
                        let values: Vec<String> = values.map(|e| e.to_owned()).collect();
                        if context
                            .multiple_args
                            .insert(arg.name.to_owned(), values)
                            .is_some()
                        {
                            panic!("Duplicated argument: {}", arg.name);
                        }
                        continue;
                    }
                }
                _ => (),
            };

            if let Some(value) = matches.value_of(&arg.name) {
                if context
                    .args
                    .insert(arg.name.to_owned(), value.to_string())
                    .is_some()
                {
                    panic!("Duplicated argument: {}", arg.name);
                }


            } else if arg.required {
                panic!("Required argument is not found: {}", arg.name)
            }
        }
        context
    }

    /// Gets value of the command line argument.
    pub fn arg<T: FromStr>(&self, key: &str) -> Result<T, Box<Error>>
    where
        <T as FromStr>::Err: Error + 'static,
    {
        if let Some(v) = self.args.get(key) {
            Ok(v.parse()?)
        } else {
            Err(Box::new(NotFoundInMap))
        }
    }

    /// Gets multiple values of the command line argument.
    pub fn arg_multiple<T: FromStr>(&self, key: &str) -> Result<Vec<T>, Box<Error>>
    where
        <T as FromStr>::Err: Error + 'static,
    {
        if let Some(values) = self.multiple_args.get(key) {
            values.iter().map(|v| Ok(v.parse()?)).collect()
        } else {
            Err(Box::new(NotFoundInMap))
        }
    }

    /// Gets the variable from the context.
    pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T, Box<Error>> {
        if let Some(v) = self.variables.get(key) {
            Ok(v.clone().try_into()?)
        } else {
            Err(Box::new(NotFoundInMap))
        }
    }

    /// Sets the variable in the context and returns the previous value.
    ///
    /// # Panic
    ///
    /// Panics if value could not be serialized as TOML.
    pub fn set<T: Serialize>(&mut self, key: &'static str, value: T) -> Option<Value> {
        let value: Value = Value::try_from(value).expect("could not convert value into toml");
        self.variables.insert(key.to_owned(), value)
    }
}

/// `CommandExtension` is used for extending the existing commands.
pub trait CommandExtension {
    /// Returns arguments of the command.
    fn args(&self) -> Vec<Argument>;
    /// Executes command.
    fn execute(&self, context: Context) -> Result<Context, Box<Error>>;
}

/// Factory for service creation.
pub trait ServiceFactory: 'static {
    //TODO: we could move
    // `service_name` and `service_id` from `Service` trait into this one
    //fn name() -> &'static str;

    /// Returns `CommandExtension` for the specific `CommandName`.
    #[allow(unused_variables)]
    fn command(command: CommandName) -> Option<Box<CommandExtension>> {
        None
    }

    /// Create a new service instance from the context returned by the `Run` command.
    fn make_service(run_context: &Context) -> Box<Service>;
}