summaryrefslogtreecommitdiffstats
path: root/bin/core/imag-init/src/lib.rs
blob: acb7464de6fc0229b8cc2238a05692e7e299ab82 (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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

#![forbid(unsafe_code)]

#![deny(
    non_camel_case_types,
    non_snake_case,
    path_statements,
    trivial_numeric_casts,
    unstable_features,
    unused_allocation,
    unused_import_braces,
    unused_imports,
    unused_must_use,
    unused_mut,
    unused_qualifications,
    while_true,
)]

extern crate clap;
#[macro_use] extern crate anyhow;

#[cfg(test)]
extern crate toml;

#[macro_use] extern crate libimagrt;
extern crate libimagerror;

mod ui;

use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::path::Path;
use std::process::Command;

use anyhow::Result;
use anyhow::Context;
use anyhow::Error;


use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication;

use clap::App;

const CONFIGURATION_STR : &str = include_str!("../imagrc.toml");

const GITIGNORE_STR : &str = r#"
# We ignore the imagrc.toml file by default
#
# That is because we expect the user to put
# this dotfile into his dotfile repository
# and symlink it here.
# If you do not do this, feel free to remove
# this line from the gitignore and add the
# configuration to this git repository.

imagrc.toml
"#;

/// Marker enum for implementing ImagApplication on
///
/// This is used by binaries crates to execute business logic
/// or to build a CLI completion.
pub enum ImagInit {}
impl ImagApplication for ImagInit {
    fn run(_rt: Runtime) -> Result<()> {
        panic!("imag-init needs to be run as a seperate binary, or we'll need to figure something out here!");
    }

    fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
        ui::build_ui(app)
    }

    fn name() -> &'static str {
        env!("CARGO_PKG_NAME")
    }

    fn description() -> &'static str {
        "Intialize the imag store"
    }

    fn version() -> &'static str {
        env!("CARGO_PKG_VERSION")
    }
}

pub fn imag_init() -> Result<()> {
    let version = make_imag_version!();
    let app     = ui::build_ui(Runtime::get_default_cli_builder(
        "imag-init",
        version.as_str(),
        "Intializes the imag store, optionally with git"));
    let matches = app.get_matches();
    let mut out = ::std::io::stdout();

    let path = if let Some(p) = matches.value_of("path") {
        PathBuf::from(String::from(p))
    } else {
        ::std::env::var("HOME")
            .map_err(Error::from)
            .map(PathBuf::from)
            .map(|mut p| { p.push(".imag"); p })
            .and_then(|path| if path.exists() {
                Err(anyhow!("Cannot continue: Path '{}' already exists", path.display()))
            } else {
                Ok(path)
            })
            .map_err(|_| anyhow!("Failed to retrieve/build path for imag directory."))?
    };

    {
        let mut store_path = path.clone();
        store_path.push("store");
        println!("Creating {}", store_path.display());

        ::std::fs::create_dir_all(store_path).context("Failed to create directory")?;
    }

    let config_path = {
        let mut config_path = path.clone();
        config_path.push("imagrc.toml");
        config_path
    };

    OpenOptions::new()
        .write(true)
        .create(true)
        .open(config_path)
        .map_err(Error::from)
        .and_then(|mut f| {
            let content = if matches.is_present("devel") {
                get_config_devel()
            } else {
                get_config()
            };

            f.write_all(content.as_bytes())
                .context("Failed to write complete config to file")
                .map_err(Error::from)
        })
        .context("Failed to open new configuration file")?;

    if find_command("git").is_some() && !matches.is_present("nogit") {
        // we initialize a git repository
        writeln!(out, "Going to initialize a git repository in the imag directory...")?;

        let gitignore_path = {
            let mut gitignore_path = path.clone();
            gitignore_path.push(".gitignore");
            gitignore_path.to_str().map(String::from)
        }.ok_or_else(|| anyhow!("Cannot convert path to string"))?;

        OpenOptions::new()
            .write(true)
            .create(true)
            .open(gitignore_path.clone())
            .map_err(Error::from)
            .and_then(|mut f| {
                f.write_all(GITIGNORE_STR.as_bytes())
                    .context("Failed to write complete gitignore to file")
                    .map_err(Error::from)
            })
            .context("Failed to open new configuration file")?;

        let path_str = path.to_str().map(String::from).ok_or_else(|| anyhow!("Cannot convert path to string"))?;
        let worktree = format!("--work-tree={}", path_str);
        let gitdir   = format!("--git-dir={}/.git", path_str);

        {
            let output = Command::new("git")
                .args(&[&worktree, &gitdir, "--no-pager", "init"])
                .output()
                .context("Calling 'git init' failed")?;

            if output.status.success() {
                writeln!(out, "{}", String::from_utf8(output.stdout).expect("No UTF-8 output"))?;
                writeln!(out, "'git {} {} --no-pager init' succeeded", worktree, gitdir)?;
            } else {
                writeln!(out, "{}", String::from_utf8(output.stderr).expect("No UTF-8 output"))?;
                if !output.status.success() {
                    return Err(anyhow!("Failed to execute git command"));
                }
            }
        }

        {
            let output = Command::new("git")
                .args(&[&worktree, &gitdir, "--no-pager", "add", &gitignore_path])
                .output()
                .context("Calling 'git add' failed")?;

            if output.status.success() {
                writeln!(out, "{}", String::from_utf8(output.stdout).expect("No UTF-8 output"))?;
                writeln!(out, "'git {} {} --no-pager add {}' succeeded", worktree, gitdir, gitignore_path)?;
            } else {
                writeln!(out, "{}", String::from_utf8(output.stderr).expect("No UTF-8 output"))?;
                if !output.status.success() {
                    return Err(anyhow!("Failed to execute git command"));
                }
            }
        }

        {
            let output = Command::new("git")
                .args(&[&worktree, &gitdir, "--no-pager", "commit", &gitignore_path, "-m", "'Initial import'"])
                .output()
                .context("Calling 'git commit' failed")?;
            if output.status.success() {
                writeln!(out, "{}", String::from_utf8(output.stdout).expect("No UTF-8 output"))?;
                writeln!(out, "'git {} {} --no-pager commit {} -m 'Initial import'' succeeded", worktree, gitdir, gitignore_path)?;
            } else {
                writeln!(out, "{}", String::from_utf8(output.stderr).expect("No UTF-8 output"))?;
                if !output.status.success() {
                    return Err(anyhow!("Failed to execute git command"));
                }
            }
        }

        writeln!(out, "git stuff finished!")?;
    } else {
        writeln!(out, "No git repository will be initialized")?;
    }

    writeln!(out, "Ready. Have fun with imag!").map_err(Error::from)
}

fn get_config() -> String {
    get_config_devel()
        .replace(
            r#"level = "debug""#,
            r#"level = "info""#
        )
}

fn get_config_devel() -> String {
    String::from(CONFIGURATION_STR)
}

fn find_command<P: AsRef<Path>>(exe_name: P) -> Option<PathBuf> {
    ::std::env::var_os("PATH")
        .and_then(|paths| {
            ::std::env::split_paths(&paths)
                .filter_map(|dir| {
                    let full_path = dir.join(&exe_name);
                    if full_path.is_file() {
                        Some(full_path)
                    } else {
                        None
                    }
                })
                .next()
        })
}

#[cfg(test)]
mod tests {
    use toml::from_str;
    use toml::Value;
    use super::get_config;
    use super::get_config_devel