n4n5/commands/
config.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
//! Config subcommand
use std::process::Command;

use clap::{arg, ArgAction, ArgMatches, Command as ClapCommand};

use crate::{cli::CliCommand, config::Config};

impl CliCommand for Config {
    fn get_subcommand() -> ClapCommand {
        ClapCommand::new("config")
            .about("config subcommand")
            .subcommand(
                ClapCommand::new("open")
                    .about("open config with default editor")
                    .arg(
                        arg!(
                            -p --path "Print the path"
                        )
                        .action(ArgAction::SetTrue)
                        .required(false),
                    ),
            )
            .arg_required_else_help(true)
    }

    fn invoke(config: &mut Config, args_matches: &ArgMatches) {
        if let Some(matches) = args_matches.subcommand_matches("open") {
            Config::open(config, matches);
        }
    }
}

impl Config {
    /// Open the config file with the default editor
    /// # Panics
    /// Panics if the editor returns a non-zero status
    fn open(config: &mut Config, matches: &ArgMatches) {
        let config_path = &config.config_path;
        let only_path = matches.get_flag("path");
        if only_path {
            println!("{}", config_path.display());
            return;
        }
        println!("Opening config {}", config_path.display());
        Command::new("vi")
            .arg(config_path)
            .spawn()
            .expect("Unable to open config with default editor")
            .wait()
            .expect("Error: Editor returned a non-zero status");
    }
}