wireman_config/
setup.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::env::var;
use std::error::Error as StdError;
use std::fmt::{self};
use std::path::Path;

use logger::Logger;

use crate::cli::Args;
use crate::config::{HistoryConfig, LoggingConfig};
use crate::install::{expand_file, expand_path, make_absolute_path};
use crate::{Config, CONFIG_FNAME, DEFAULT_CONFIG_DIR, ENV_CONFIG_DIR};
use theme::Theme;

use crate::error::{Error, Result};
use std::result::Result as StdResult;

/// Initializes the `Config` from environment variables
///
/// # Errors
/// See [`setup`].
pub fn init_from_env(args: &Args) -> Result<(Config, Option<String>)> {
    setup(false, args)
}

/// Runs the setup, allowing for a dry-run mode where no files are created.
///
/// In dry-run mode, additional information is logged to the console.
///
/// # Errors
///
/// This function can return the following errors:
///
/// - `Config Init Errors`: Error initializing the configuration.
/// - `Logger Init Errors`: Error initializing the logger.
/// - `History Init Errors`: Error initializing the history.
#[allow(clippy::too_many_lines)]
pub fn setup(dry_run: bool, args: &Args) -> Result<(Config, Option<String>)> {
    let (config_dir_str, config_file) = if let Some(config_file) = &args.config {
        let config_file_abs = expand_file(&make_absolute_path(config_file));
        let config_dir_str = get_parent_dir(&config_file_abs);
        (config_dir_str, config_file.to_string())
    } else {
        let config_dir_str = get_config_dir(dry_run)?;
        let config_dir = Path::new(&config_dir_str);
        let config_file = get_config_file(config_dir, dry_run)?;
        (config_dir_str, config_file)
    };
    let config_dir = Path::new(&config_dir_str);

    let mut config = match load_config(&config_file, dry_run) {
        Ok(config) => config,
        Err(err) => {
            if args.local_protos {
                Config::default()
            } else {
                return Err(err);
            }
        }
    };
    if args.local_protos {
        update_config_with_local_protos(&mut config)?;
    };

    init_history(&mut config, config_dir, dry_run)?;

    init_logger(&mut config, config_dir, dry_run)?;

    if !dry_run {
        Theme::init(&config.ui);
    }

    Ok((config, Some(config_file)))
}

fn get_config_dir(dry_run: bool) -> Result<String> {
    match config_dir_checked() {
        Err(err) => {
            if dry_run {
                println!("{:<20} Error: {}", "Config:", err);
            }
            Err(Error::SetupError(err))
        }
        Ok(config_dir) => Ok(config_dir),
    }
}

fn get_config_file(config_dir: &Path, dry_run: bool) -> Result<String> {
    match config_file_checked(config_dir) {
        Err(err) => {
            if dry_run {
                println!("{:<20} Error: {}", "Config:", err);
            }
            Err(Error::SetupError(err))
        }
        Ok(config_file) => {
            if dry_run {
                println!("{:<20} {}", "Config:", config_file);
            }
            Ok(config_file)
        }
    }
}

fn get_parent_dir(config_file: &str) -> String {
    let path = Path::new(&config_file);
    path.parent().map_or(
        std::env::current_dir()
            .unwrap()
            .to_string_lossy()
            .to_string(),
        |dir| dir.to_string_lossy().to_string(),
    )
}

fn load_config(config_file: &str, dry_run: bool) -> Result<Config> {
    match Config::load(config_file) {
        Ok(config) => Ok(config),
        Err(err) => {
            if dry_run {
                println!("{:<20} Error: {}", "Config:", err);
            }
            Err(err)
        }
    }
}

fn update_config_with_local_protos(config: &mut Config) -> Result<()> {
    let (current_dir, protos) =
        list_local_protos().map_err(|err| Error::SetupError(SetupError::ListLocalProtos(err)))?;

    config.files = protos;
    config.includes = vec![current_dir];

    Ok(())
}

fn init_history(config: &mut Config, config_dir: &Path, dry_run: bool) -> Result<()> {
    if config.history.disabled {
        if dry_run {
            println!("{:<20} disabled", "History:");
        }
        return Ok(());
    }

    let history_dir = match history_dir_checked(config_dir, &config.history) {
        Err(err) => {
            if dry_run {
                println!("{:<20} Error: {}", "History:", err);
            }
            return Err(Error::SetupError(err));
        }
        Ok(history_dir) => {
            if dry_run {
                println!("{:<20} {}", "History:", history_dir);
            }
            history_dir
        }
    };

    config.history.directory.clone_from(&history_dir);
    if dry_run {
        return Ok(());
    }

    if !Path::new(&history_dir).exists() {
        if let Err(err) = std::fs::create_dir(&history_dir) {
            return Err(Error::SetupError(SetupError::CreateDirectory(Box::new(
                err,
            ))));
        }
    }
    Ok(())
}

fn init_logger(config: &mut Config, config_dir: &Path, dry_run: bool) -> Result<()> {
    let logger_file = match logger_dir_checked(config_dir, &config.logging) {
        Err(err) => {
            if dry_run {
                println!("{:<20} Error: {}", "Logging:", err);
            }
            return Err(Error::SetupError(err));
        }
        Ok(logger_dir) => {
            config.logging.directory.clone_from(&logger_dir);
            let logger_file = config.logging.file_path_expanded();
            if dry_run {
                println!("{:<20} {}", "Logging:", logger_file);
            }
            logger_file
        }
    };

    if dry_run {
        return Ok(());
    }

    if let Err(err) = Logger::init(logger_file, config.logging.level) {
        return Err(Error::SetupError(SetupError::InitializeLogger(Box::new(
            err,
        ))));
    }

    Ok(())
}

fn config_dir_checked() -> StdResult<String, SetupError> {
    let config_dir = var(ENV_CONFIG_DIR).unwrap_or(DEFAULT_CONFIG_DIR.to_string());
    let config_dir_expanded = expand_path(&config_dir);

    let config_path = Path::new(&config_dir_expanded);
    if config_dir.is_empty() || !config_path.exists() {
        return Err(SetupError::ConfigDirInvalid(config_dir));
    }

    Ok(config_dir_expanded)
}

fn config_file_checked(config_path: &Path) -> StdResult<String, SetupError> {
    let config_file_path = config_path.join(CONFIG_FNAME);
    let config_file = config_file_path.to_string_lossy();
    if !config_path.exists() {
        return Err(SetupError::ConfigFileNotFound(config_file.to_string()));
    }

    Ok(config_file.to_string())
}

fn history_dir_checked(
    config_dir: &Path,
    history: &HistoryConfig,
) -> StdResult<String, SetupError> {
    let mut history_dir_path = history.directory_expanded();
    if history_dir_path.is_empty() {
        let default_history_path = {
            let path = config_dir.join("history").clone();
            path.to_string_lossy().to_string()
        };
        history_dir_path = default_history_path.to_string();
    }

    if Path::new(&history_dir_path)
        .parent()
        .is_none_or(|p| !p.exists())
    {
        return Err(SetupError::HistoryPathNotFound(
            history_dir_path.to_string(),
        ));
    }

    Ok(history_dir_path)
}

fn logger_dir_checked(
    config_path: &Path,
    logging: &LoggingConfig,
) -> StdResult<String, SetupError> {
    let mut logger_dir = logging.directory_expanded();
    if logger_dir.is_empty() {
        let default_logger_path = config_path.to_string_lossy().to_string();
        logger_dir = default_logger_path.to_string();
    }

    if !Path::new(&logger_dir).exists() {
        return Err(SetupError::LoggerPathNotFound(logger_dir.to_string()));
    }

    Ok(logger_dir)
}

fn list_local_protos() -> std::io::Result<(String, Vec<String>)> {
    let current_dir = std::env::current_dir()?;
    let current_dir_str = current_dir.to_string_lossy().to_string();

    let mut proto_files = Vec::new();
    for entry in std::fs::read_dir(current_dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() && path.extension().is_some_and(|ext| ext == "proto") {
            if let Some(file_name) = path.file_name().and_then(|name| name.to_str()) {
                proto_files.push(file_name.to_string());
            }
        }
    }
    Ok((current_dir_str, proto_files))
}

#[derive(Debug)]
pub enum SetupError {
    ConfigDirEnvNotFound,
    ConfigDirInvalid(String),
    ConfigFileNotFound(String),
    HistoryPathNotFound(String),
    LoggerPathNotFound(String),
    CreateDirectory(Box<dyn StdError>),
    InitializeLogger(Box<dyn StdError>),
    ListLocalProtos(std::io::Error),
}

impl fmt::Display for SetupError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SetupError::ConfigDirEnvNotFound => {
                write!(f, "The config dir ${ENV_CONFIG_DIR} was not found.")
            }
            SetupError::ConfigDirInvalid(d) => {
                write!(f, "The config dir ${d} is not a valid path.")
            }
            SetupError::CreateDirectory(err) => {
                write!(f, "Could not create directory in ${ENV_CONFIG_DIR}: {err}.")
            }
            SetupError::ConfigFileNotFound(file) => {
                write!(f, "The config file {file} was not found.")
            }
            SetupError::HistoryPathNotFound(path) => {
                write!(f, "The historys parent path {path} does not exist.")
            }
            SetupError::LoggerPathNotFound(path) => {
                write!(f, "The loggers parent path {path} does not exist.")
            }
            SetupError::InitializeLogger(err) => write!(f, "Failed to initialize logger: {err}."),
            SetupError::ListLocalProtos(err) => write!(f, "Cannot list local protos: {err}."),
        }
        // let general_error_msg = "Check the README for tips on how to set up wireman: \
        // https://github.com/preiter93/wireman";
        // write!(f, "{}\n{}\n", self.error_msg, general_error_msg)
    }
}

impl StdError for SetupError {}