rsiot/env_vars/
cli.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
//! CLI интерфейс

use clap::{Parser, Subcommand};
use tracing::info;

use super::{create_env_file::create_env_file, load_config, Errors, IEnvVars};

const ENV_EXAMPLE_FILE: &str = ".env.example";

/// Структура входа CLI
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

/// Перечент команд CLI
#[derive(Subcommand)]
pub enum Commands {
    /// Создать файл .env.example с настройками по-умолчанию
    Create,
    /// Проверка файла .env на наличие и правильность конфигурации
    Check,
}

/// Запускаем CLI
pub fn env_vars_cli<TConfig>()
where
    TConfig: IEnvVars,
{
    let cli = Cli::parse();

    let value = cli.command;
    let command = match value {
        Commands::Create => command_create::<TConfig>(),
        Commands::Check => command_check::<TConfig>(),
    };
    command.ok();
}

fn command_create<TConfig>() -> Result<(), Errors>
where
    TConfig: IEnvVars,
{
    info!("Создаем файл {}", ENV_EXAMPLE_FILE);
    create_env_file::<TConfig>(ENV_EXAMPLE_FILE)?;
    info!("Файл {} создан", ENV_EXAMPLE_FILE);
    Ok(())
}

fn command_check<TConfig>() -> Result<(), Errors>
where
    TConfig: IEnvVars,
{
    info!("Пробуем загрузить файл .env");
    let config = load_config::<TConfig>()?;
    info!("Загружены настройки: {:#?}", config);
    Ok(())
}