summaryrefslogtreecommitdiffstats
path: root/src/config_installer.rs
blob: 5ca9282fc15469128c5f902253b70cf84e7b2b23 (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
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
use std::fs::*;
use std::io::Write;
use std::process::Command;
use std::ffi::OsStr;
use std::path::Path;

use crate::fail::{HError, HResult};
use crate::widget::WidgetCore;


pub fn ensure_config(core: WidgetCore) -> HResult<()> {
    if has_config()? {
        let previewers_path = crate::paths::previewers_path()?;
        let actions_path = crate::paths::actions_path()?;

        if !previewers_path.exists() {
            core.show_status("Coulnd't find previewers in config dir! Adding!")?;
            install_config_previewers()
                .or_else(|_|
                         core.show_status("Error installing previewers! Check log!"))?;
        }

        if !actions_path.exists() {
            core.show_status("Coulnd't find actions in config dir! Adding!")?;
            install_config_actions()
                .or_else(|_|
                         core.show_status("Error installing actions! Check log!"))?;
        }

        return Ok(());
    }

    let msg = match install_config_all() {
        Ok(_) => format!("Config installed in: {}",
                         crate::paths::hunter_path()?.to_string_lossy()),
        Err(_) => format!("{}Problems with installation of default configuration! Look inside log.",
                          crate::term::color_red()),
    };
    core.show_status(&msg)?;

    Ok(())
}


fn default_config_archive() -> &'static [u8] {
    let default_config = include_bytes!("../config.tar.gz");
    default_config
}

fn has_config() -> HResult<bool> {
    let config_dir = crate::paths::hunter_path()?;

    if config_dir.exists() {
        return Ok(true);
    } else {
        return Ok(false);
    }
}


fn install_config_all() -> HResult<()> {
    let hunter_dir = crate::paths::hunter_path()?;
    let config_dir = hunter_dir.parent()?;

    if !hunter_dir.exists() {
        // create if non-existing
        std::fs::create_dir(&hunter_dir)
            .or_else(|_| HError::log(&format!("Couldn't create directory: {}",
                                              hunter_dir.as_os_str()
                                                        .to_string_lossy())))?;
    }

    let archive_path = create_archive()?;
    extract_archive(config_dir, &archive_path)?;
    delete_archive(archive_path)?;

    Ok(())
}

fn move_dir(from: &str, to: &Path) -> HResult<()> {
    let success = Command::new("mv")
        .arg(from)
        .arg(to.as_os_str())
        .status()
        .map(|s| s.success());

    if success.is_err() || !success.unwrap() {
        HError::log(&format!("Couldn't move {} to {} !",
                             from,
                             to.to_string_lossy()))
    } else {
        Ok(())
    }
}

fn install_config_previewers() -> HResult<()> {
    let hunter_dir = crate::paths::hunter_path()?;
    let archive_path = create_archive()?;
    extract_archive(Path::new("/tmp"), &archive_path)?;
    move_dir("/tmp/hunter/previewers", &hunter_dir)?;
    delete_archive(&archive_path)
}

fn install_config_actions() -> HResult<()> {
    let hunter_dir = crate::paths::hunter_path()?;
    let archive_path = create_archive()?;
    extract_archive(Path::new("/tmp"), &archive_path)?;
    move_dir("/tmp/hunter/actions", &hunter_dir)?;
    delete_archive(&archive_path)
}

fn create_archive() -> HResult<&'static str> {
    let archive_path = "/tmp/hunter-config.tar.gz";
    let def_config = default_config_archive();

    File::create(archive_path)
        .and_then(|mut f| {
            f.write_all(def_config).map(|_| f)
        })
        .and_then(|mut f| f.flush())
        .or_else(|_| {
            HError::log(&format!("Failed to write config archive to: {}",
                                 archive_path))
        })?;
    Ok(archive_path)
}


fn extract_archive(to: &Path, archive_path: &str) -> HResult<()> {
    let success = Command::new("tar")
        .args(&[OsStr::new("-C"),
                to.as_os_str(),
                OsStr::new("-xf"),
                OsStr::new(archive_path)])
        .status()
        .or_else(|_| HError::log(&format!("Couldn't run tar!")))
        .map(|s| s.success())?;

    if !success {
        HError::log(&format!("Extraction of archive failed! Archive: {}",
                             archive_path))?
    }

    Ok(())
}

fn delete_archive(archive_path: &str) -> HResult<()> {
    std::fs::remove_file(archive_path)
        .or_else(|_| HError::log(&format!("Deletion of archive failed! Archive: {}",
                                          archive_path)))
}