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
use std::convert::TryFrom;
use std::fs::{self, File};
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use crate::errors::InputError;
use dirs::{config_dir, home_dir};
use serde_derive::{Deserialize, Serialize};
use toml;

/// Handles and saves configuration variables between application calls.
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Config {
    /// Path of dotfiles directory.
    pub directory: PathBuf,
}

impl Config {
    pub(crate) fn new<P: AsRef<Path>>(directory: P) -> Result<Self, InputError> {
        let directory = directory.as_ref().to_path_buf();

        if directory.is_dir() {
            Ok(Self { directory })
        } else {
            Err(InputError::BadInput {
                err: io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Input to set dots directory is invalid",
                ),
            })
        }
    }

    // REVIEW: how should we handle if a dotfiles directory is already set?
    /// Sets arg `path` at dotfiles directory, and writes TOML config file.
    ///
    /// If path is not available it will try to be created.
    pub fn set_dots_dir<P: AsRef<Path>>(path: P) -> Result<PathBuf, InputError> {
        let path = path.as_ref();

        if !path.exists() {
            fs::create_dir_all(path).expect("could not create path");
        } else if !path.is_dir() {
            return Err(InputError::BadInput {
                err: io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Input to set dots directory is invalid",
                ),
            });
        };

        let config = Self::new(&path)?;

        config
            .write_toml_config()
            .expect("could not write toml config");
        Ok(path.to_path_buf())
    }

    /// If config file `.badm.toml` exists, get dotfiles directory path.
    pub fn get_dots_dir() -> Option<PathBuf> {
        if let Some(config_path) = Self::get_config_file() {
            let toml = crate::paths::read_path(&config_path).unwrap();

            let config: Self = toml::from_str(&toml).expect("Not able to read config!");
            Some(config.directory)
        } else {
            None
        }
    }

    /// Search `$HOME` and `$XDG_CONFIG_HOME` for config file path.
    fn get_config_file() -> Option<PathBuf> {
        let search_paths = |file_name: &str, dirs_vec: Vec<PathBuf>| -> Option<PathBuf> {
            for dir in dirs_vec {
                let possible_file_path = dir.join(file_name);

                if possible_file_path.exists() {
                    return Some(possible_file_path);
                };
            }
            None
        };

        let config_file_name = ".badm.toml";
        search_paths(config_file_name, vec![
            home_dir().unwrap(),
            config_dir().unwrap(),
        ])
    }

    /// Save configuration variables to config file `.badm.toml`. If file cannot be found
    /// it will be written to $HOME.
    ///
    /// Valid locations for file location include: `$HOME` and `$XDG_CONFIG_HOME`.
    pub fn write_toml_config(self) -> io::Result<()> {
        // check to see if config file already exists, if not default to HOME
        let config_file_path = match Self::get_config_file() {
            Some(path) => path,
            None => home_dir().unwrap().join(".badm.toml"),
        };

        let toml = toml::to_string(&self).unwrap();
        let mut file = File::create(config_file_path)?;

        file.write_all(&toml.into_bytes())?;
        file.sync_data()?;

        Ok(())
    }
}

impl TryFrom<File> for Config {
    type Error = InputError;
    fn try_from(file: File) -> Result<Self, Self::Error> {
        let mut file = file;

        let contents = crate::paths::read_file(&mut file)?;

        Ok(toml::from_str(&contents)?)
    }
}

impl TryFrom<PathBuf> for Config {
    type Error = InputError;
    fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
        let file = File::open(path)?;
        Self::try_from(file)
    }
}

impl FromStr for Config {
    type Err = toml::de::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let config: Self = toml::from_str(s)?;
        Ok(config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use failure::Error;
    use std::fs;

    #[ignore]
    #[test]
    fn set_dots_dir_test() -> Result<(), Error> {
        let home_dir = home_dir().unwrap();
        if !home_dir.exists() {
            fs::create_dir_all(&home_dir).expect("could not create home dir");
        }

        let dots_dir = home_dir.join(".dotfiles");
        let _ = Config::set_dots_dir(&dots_dir)?;

        let toml = crate::paths::read_path(&home_dir.join(".badm.toml"))
            .expect("could not read path");

        // Read file contents
        let config: Config =
            toml::from_str(toml.as_str()).expect("could not convert toml");
        assert_eq!(config.directory, dots_dir);

        Ok(())
    }

    #[ignore]
    #[test]
    fn write_toml_config_test() -> io::Result<()> {
        let config_path = home_dir().unwrap().join(".badm.toml");

        let dots_dir = home_dir().unwrap().join(".dotfiles");
        let expected_config = Config {
            directory: dots_dir,
        };

        let config = expected_config.clone();
        config.write_toml_config()?;

        let actual_config = match Config::try_from(config_path) {
            Ok(config) => config,
            Err(_) => Config {
                directory: PathBuf::from("/"),
            },
        };

        assert_eq!(expected_config, actual_config);

        Ok(())
    }
}