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
/*
 * Vento, a CLI inventory for your files.
 * Copyright (C) 2023 Lux Aliaga
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

use crate::{
    common,
    message::{append_emoji, EmojiType},
};
use anyhow::Result;
use colored::Colorize;
use std::{fs::File, path::PathBuf};
use tar::Archive;
use xz2::read::XzDecoder;
use xz2::write::XzEncoder;

/// Exports an inventory slot into an xz tarball
pub fn export_inv(slot: &str, output: PathBuf, message: bool) -> Result<()> {
    let slotdir: PathBuf = match slot {
        "active" | "a" => common::env_config()?.active_dir,
        "inactive" | "i" => common::env_config()?.inactive_dir,
        _ => PathBuf::new(),
    };

    let archive = File::create(&output)?;
    let enc = XzEncoder::new(archive, 9);
    let mut tar = tar::Builder::new(enc);
    tar.append_dir_all("", slotdir)?;

    if message {
        println!(
            "{}{} {} {} {}",
            append_emoji(EmojiType::Success)?,
            "Exported".green(),
            match slot {
                "a" | "active" => "active".green(),
                "i" | "inactive" => "inactive".blue(),
                _ => slot.red(),
            }
            .bold(),
            "slot into".green(),
            &output.to_str().unwrap()
        );
    };
    Ok(())
}

/// Exports the Vento directory into an xz tarball
pub fn export_dir(output: PathBuf, message: bool) -> Result<()> {
    let dir: PathBuf = common::env_config()?.vento_dir;

    let archive = File::create(&output)?;
    let enc = XzEncoder::new(archive, 9);
    let mut tar = tar::Builder::new(enc);
    tar.append_dir_all("", dir)?;

    if message {
        println!(
            "{}{} {}",
            append_emoji(EmojiType::Success)?,
            "Exported Vento directory into".green(),
            &output.to_str().unwrap()
        );
    };
    Ok(())
}

/// Imports an xz tarball into an inventory slot
pub fn import_inv(input: PathBuf, slot: &str, message: bool) -> Result<()> {
    let slotdir: PathBuf = match slot {
        "active" | "a" => common::env_config()?.active_dir,
        "inactive" | "i" => common::env_config()?.inactive_dir,
        _ => PathBuf::new(),
    };

    let tar_xz = File::open(&input)?;
    let tar = XzDecoder::new(tar_xz);
    let mut archive = Archive::new(tar);
    archive.unpack(&slotdir)?;

    if message {
        println!(
            "{}{} {} {} {} {}",
            append_emoji(EmojiType::Success)?,
            "Imported".green(),
            &input.to_str().unwrap(),
            "into".green(),
            match slot {
                "a" | "active" => "active".green(),
                "i" | "inactive" => "inactive".blue(),
                _ => slot.red(),
            }
            .bold(),
            "slot".green()
        );
    };
    Ok(())
}

/// Imports an xz tarball into the Vento directory
pub fn import_dir(input: PathBuf, message: bool) -> Result<()> {
    let dir: PathBuf = common::env_config()?.vento_dir;

    let tar_xz = File::open(&input)?;
    let tar = XzDecoder::new(tar_xz);
    let mut archive = Archive::new(tar);
    archive.unpack(&dir)?;

    if message {
        println!(
            "{}{} {} {}",
            append_emoji(EmojiType::Success)?,
            "Imported".green(),
            &input.to_str().unwrap(),
            "into Vento directory".green(),
        );
    };
    Ok(())
}