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
#![forbid(unsafe_code)]
use clap::ArgMatches;
use failure::ResultExt;
use std::fs;
use std::os::unix;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Handle all subcommands and calls appropriate function
#[inline]
pub fn run(matches: &ArgMatches) -> Result<(), failure::Error> {
    let config_paths = get_config_paths()?;

    create_config_dir(&config_paths.config_dir).with_context(|_| "failed to create config dir")?;

    match matches.subcommand() {
        ("create", Some(create_matches)) => {
            let profile = create_matches.value_of("profile").unwrap();

            create_profile(profile, &config_paths.config_dir)
                .with_context(|_| format!("Failed to create profile \"{}\"", profile))?;
        }
        ("list", Some(_)) => {
            list_all_profiles(&config_paths.config_dir)
                .with_context(|_| "Failed to list all profiles")?;
        }
        ("open", Some(open_matcher)) => {
            let profile = open_matcher.value_of("profile").unwrap();
            let editor = open_matcher.value_of("editor").unwrap();

            open_profile(profile, &config_paths.config_dir, editor)
                .with_context(|_| format!("Failed to open profile \"{}\"", profile))?;
        }
        ("activate", Some(activate_matcher)) => {
            let profile = activate_matcher.value_of("profile").unwrap();

            activate_profile(profile, &config_paths.config_dir, &config_paths.home_dir)
                .with_context(|_| format!("Failed to activate profile \"{}\"", profile))?;
        }
        ("status", Some(_)) => {
            show_active_profile(&config_paths.config_dir, &config_paths.home_dir);
        }
        ("remove", Some(remove_matches)) => {
            let profile = remove_matches.value_of("profile").unwrap();

            remove_profile(profile, &config_paths.config_dir)
                .with_context(|_| format!("Failed to remove profile \"{}\"", profile))?;
        }
        ("backup", Some(backup_matches)) => {
            let profile = backup_matches.value_of("profile").unwrap();

            create_backup_profile(profile, &config_paths.config_dir, &config_paths.home_dir)
                .with_context(|_| "Failed to create a backup for \".npmrc\"")?;
        }
        ("", None) => return Err(failure::err_msg("no subcommand was used")),
        _ => unreachable!(),
    };

    Ok(())
}

struct ConfigPaths {
    home_dir: PathBuf,
    config_dir: PathBuf,
}

fn get_config_paths() -> Result<ConfigPaths, failure::Error> {
    let home_dir = match dirs::home_dir() {
        Some(path) => path,
        None => return Err(failure::err_msg("did not find home directory")),
    };

    let mut config_dir = PathBuf::from(&home_dir);
    config_dir.push(".rnpmrc");

    Ok(ConfigPaths {
        home_dir,
        config_dir,
    })
}

fn create_config_dir(dir_path: &Path) -> Result<(), failure::Error> {
    let dir_exists = dir_path.is_dir();

    if !dir_exists {
        print!("Creating directory {:?}... ", dir_path);
        fs::DirBuilder::new().recursive(true).create(dir_path)?;
        println!("Succeed");
    }

    Ok(())
}

fn create_backup_profile(
    profile: &str,
    config_dir: &Path,
    home_dir: &Path,
) -> Result<(), failure::Error> {
    let file_path = build_file_path(config_dir, &format!(".npmrc.{}", profile));
    let npmrc_path = build_file_path(home_dir, ".npmrc");

    if !exists_or_symlinked(&npmrc_path) {
        return Err(failure::err_msg(format!(
            "file {:?} doesn't exist",
            npmrc_path
        )));
    }

    create_profile(profile, &config_dir)?;

    print!("Copying data from {:?} to {:?}... ", npmrc_path, file_path);
    fs::copy(&npmrc_path, &file_path)?;
    println!("Succeed");

    Ok(())
}

fn create_profile(profile: &str, config_dir: &Path) -> Result<(), failure::Error> {
    let file_path = build_file_path(config_dir, &format!(".npmrc.{}", profile));

    if file_path.is_file() {
        return Err(failure::err_msg(format!(
            "file {:?} already exists",
            file_path
        )));
    }

    print!("Creating file {:?}... ", file_path);
    fs::File::create(file_path)?;
    println!("Succeed");

    Ok(())
}

fn list_all_profiles(config_dir: &Path) -> Result<(), failure::Error> {
    let paths = fs::read_dir(config_dir)?;
    let mut file_names = String::new();

    for entry in paths {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() {
            let file_name = match path.file_name() {
                Some(parsed_file_name) => parsed_file_name,
                None => return Err(failure::err_msg("Failed reading files".to_string())),
            };

            if let Some(file_name_str) = file_name.to_str() {
                if file_name_str.contains(".npmrc.") {
                    file_names.push_str(&format!("{}\n", file_name_str));
                }
            }
        }
    }

    println!("{}", file_names);

    Ok(())
}

fn open_profile(profile: &str, config_dir: &Path, editor: &str) -> Result<(), failure::Error> {
    let file_path = build_file_path(config_dir, &format!(".npmrc.{}", profile));

    if file_path.is_file() {
        Command::new(editor).arg(file_path).status()?;
        return Ok(());
    }

    Err(failure::err_msg(format!(
        "file {:?} doesn't exists",
        file_path
    )))
}

fn remove_profile(profile: &str, config_dir: &Path) -> Result<(), failure::Error> {
    let file_path = build_file_path(config_dir, &format!(".npmrc.{}", profile));

    if file_path.is_file() {
        print!("Removing file {:?}... ", file_path);
        fs::remove_file(file_path)?;
        println!("Succeed");

        return Ok(());
    }

    Err(failure::err_msg(format!(
        "file {:?} doesn't exists",
        file_path
    )))
}

fn activate_profile(
    profile: &str,
    config_dir: &Path,
    home_dir: &Path,
) -> Result<(), failure::Error> {
    let file_path = build_file_path(config_dir, &format!(".npmrc.{}", profile));
    let npmrc_path = build_file_path(home_dir, ".npmrc");

    if !file_path.is_file() {
        return Err(failure::err_msg(format!(
            "file {:?} doesn't exists",
            file_path
        )));
    }

    if exists_or_symlinked(&npmrc_path) {
        print!("Removing {:?}... ", npmrc_path);
        fs::remove_file(&npmrc_path)?;
        println!("Succeed");
    }

    print!("Creating symlink for {:?}... ", file_path);
    unix::fs::symlink(&file_path, &npmrc_path)?;
    println!("Succeed");

    Ok(())
}

fn show_active_profile(config_dir: &Path, home_dir: &Path) {
    let npmrc_path = build_file_path(home_dir, ".npmrc");

    if let Ok(info) = fs::read_link(&npmrc_path) {
        if info.is_file() && info.starts_with(&config_dir) {
            if let Some(file_name) = info.file_name() {
                println!("{:?} is active", file_name);
                return;
            }
        }
    }

    println!("No active profile");
}

// Utilities
fn build_file_path(dir_path: &Path, file_name: &str) -> PathBuf {
    let mut file_path = PathBuf::from(dir_path);
    file_path.push(file_name);

    file_path
}

fn exists_or_symlinked(path: &Path) -> bool {
    if path.is_file() || path.is_dir() {
        return true;
    }

    if fs::read_link(&path).is_ok() {
        return true;
    }

    false
}