aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 80a03b9dd641dd25c8297220465a42d2e9bc0092 (plain)
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
#![feature(drain_filter)]

//pub mod config;
pub mod dotup;

use std::path::PathBuf;

use anyhow::Context;
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
struct GlobalFlags {
    #[clap(long)]
    base: Option<PathBuf>,

    #[clap(long, default_value = "./dotup")]
    config: PathBuf,
}

#[derive(Subcommand, Debug)]
enum SubCommand {
    Install(InstallArgs),
    Uninstall(UninstallArgs),
    Status(StatusArgs),
    Format(FormatArgs),
}

#[derive(Parser, Debug)]
struct InstallArgs {
    groups: Vec<String>,
}

#[derive(Parser, Debug)]
struct UninstallArgs {
    groups: Vec<String>,
}

#[derive(Parser, Debug)]
struct StatusArgs {
    groups: Vec<String>,
}

#[derive(Parser, Debug)]
struct FormatArgs {}

#[derive(Parser, Debug)]
struct Args {
    #[clap(flatten)]
    globals: GlobalFlags,
    #[clap(subcommand)]
    command: SubCommand,
}

fn main() -> anyhow::Result<()> {
    env_logger::init();

    let args = Args::parse();
    match args.command {
        SubCommand::Install(install) => command_install(args.globals, install),
        SubCommand::Uninstall(uninstall) => command_uninstall(args.globals, uninstall),
        SubCommand::Status(status) => command_status(args.globals, status),
        SubCommand::Format(format) => command_format(args.globals, format),
    }
}

impl GlobalFlags {
    fn base_path_or_default(&self) -> PathBuf {
        self.base.clone().unwrap_or_else(|| {
            PathBuf::from(std::env::var("HOME").expect("failed to get HOME directory"))
        })
    }
}

fn command_install(globals: GlobalFlags, args: InstallArgs) -> anyhow::Result<()> {
    let dotup = dotup::load_file(&globals.config).context("failed to parse config")?;
    let cwd = std::env::current_dir().context("failed to get current directory")?;
    let install_params = dotup::InstallParams {
        cwd: &cwd,
        home: &globals.base_path_or_default(),
    };
    for group in args.groups {
        match dotup.find_group_by_name(&group) {
            Some(group_id) => dotup.install(install_params, group_id)?,
            None => log::error!("group not found: {}", group),
        };
    }
    Ok(())
}

fn command_uninstall(globals: GlobalFlags, args: UninstallArgs) -> anyhow::Result<()> {
    let dotup = dotup::load_file(&globals.config).context("failed to parse config")?;
    let cwd = std::env::current_dir().context("failed to get current directory")?;
    let uninstall_params = dotup::UninstallParams {
        cwd: &cwd,
        home: &globals.base_path_or_default(),
    };
    for group in args.groups {
        match dotup.find_group_by_name(&group) {
            Some(group_id) => dotup.uninstall(uninstall_params, group_id)?,
            None => log::error!("group not found: {}", group),
        };
    }
    Ok(())
}

fn command_status(globals: GlobalFlags, args: StatusArgs) -> anyhow::Result<()> {
    let dotup = dotup::load_file(&globals.config).context("failed to parse config")?;
    let cwd = std::env::current_dir().context("failed to get current directory")?;
    let install_params = dotup::InstallParams {
        cwd: &cwd,
        home: &globals.base_path_or_default(),
    };
    for group in args.groups {
        match dotup.find_group_by_name(&group) {
            Some(group_id) => dotup.status(install_params, group_id)?,
            None => log::error!("group not found: {}", group),
        };
    }
    Ok(())
}

fn command_format(globals: GlobalFlags, _args: FormatArgs) -> anyhow::Result<()> {
    dotup::format_file_inplace(&globals.config).context("failed to format config")?;
    Ok(())
}