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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// 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 std::path::{Path, PathBuf};

use anyhow::{anyhow, Context as _};
use reedline_repl_rs::clap::{Arg, ArgMatches, Command};
use reedline_repl_rs::Repl;
use s_macro::s;
use wildland_cargo_lib::api::container::{CargoContainer, MountState};
use wildland_cargo_lib::api::CargoLib;
use wildland_corex::dfs::interface::SpaceUsage;
use wildland_corex::StorageTemplate;
use wildland_storage_backend::lfs::template::LocalFilesystemStorageTemplate;

use crate::{match_context, Context};

/*
   ===========================
   helpers
   ===========================
*/

#[tracing::instrument(level = "trace", err(Debug), skip(cargo))]
pub fn aux_load_template_file(cargo: &CargoLib, file: &Path) -> anyhow::Result<StorageTemplate> {
    if !file.exists() || !file.is_file() {
        return Err(anyhow::anyhow!(
            "File {} does not exist or is not a file",
            file.to_str().unwrap()
        ));
    }

    let template_payload = std::fs::read_to_string(file)?.into_bytes();
    cargo
        .storage_template_from_json(template_payload)
        .map_err(Into::into)
}

#[tracing::instrument(skip(container), ret)]
pub fn aux_container_info(container: &CargoContainer) -> String {
    let mut out = format!(
        r#"
    container info:
        name: {}
        uuid: {}
        path: {}
        storages:
    "#,
        container.name().unwrap_or(s!("?no-name?")),
        container.uuid(),
        container.get_path().unwrap_or_default()
    );

    if let Ok(storages) = container.clone().get_storages() {
        for s in storages {
            let data = String::from_utf8(s.data()).unwrap_or("null".into());
            let space_usage = s.get_space_usage().unwrap_or(SpaceUsage::new(0u64, 0u64));
            let template_uuid = match s.template_uuid() {
                Some(template_uuid) => template_uuid.to_string(),
                None => s!("None"),
            };
            let storage_uuid = s.uuid().to_string();
            let backend_type = s.backend_type();
            let name = s.name().unwrap_or("error".into());
            out.push_str(&format!(
                "
\t >> used_space: {space_usage}
\t >> template uuid: {template_uuid}
\t >> storage uuid: {storage_uuid}
\t >> backend type: {backend_type}
\t >> name: {name}
\t >> data: {data}
"
            ));
        }
    }
    out
}
/*
   ===========================
   handler implementations
   ===========================
*/

#[tracing::instrument(level = "trace", err(Debug), err(Debug), skip(_args, context))]
fn h_ct_list(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let (_, cargo_user) = match_context!(context, cargo => Some(_), cargo_user => Some(_))?;
    let containers = cargo_user
        .find_containers(None, MountState::MountedOrUnmounted)
        .context(anyhow!("failed to list containers"))?;
    let containers_len = containers.len();
    let container_list = containers.iter().fold(String::new(), |mut acc, c| {
        acc += &aux_container_info(c);
        acc
    });
    let output = format!(
        r#"
Containers ({containers_len} found):
{container_list}
"#
    );

    Ok(Some(output))
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_ct_create_from_file(
    args: ArgMatches,
    context: &mut Context,
) -> anyhow::Result<Option<String>> {
    let (cargo, cargo_user) = match_context!(context, cargo => Some(_), cargo_user => Some(_))?;
    let path = args
        .get_one::<String>("claim")
        .context(anyhow!("claim path not provided"))?;
    let name = args
        .get_one::<String>("name")
        .context(anyhow!("name not provided"))?;
    let template_file = args
        .get_one::<String>("template_file")
        .context(anyhow!("template filename not provided"))?;
    let is_encrypted = args
        .get_one::<bool>("encrypted")
        .unwrap_or(&false)
        .to_owned();

    let templatefile = PathBuf::from(template_file);
    let cargo_template = aux_load_template_file(cargo, &templatefile)?;
    // i think we can drop the handle, its enough to have it created somewhere
    // future access to the containers is over cargo_user.get_containers()
    let cthandle =
        cargo_user.create_container(s!(name), &cargo_template, s!(path), is_encrypted)?;
    Ok(Some(aux_container_info(&cthandle)))
}

// results in new container of specified name, and type==localfs
// requires name and path
#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_ct_create_lfs(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let (_, cargo_user) = match_context!(context, cargo => Some(_), cargo_user => Some(_))?;
    let path = args
        .get_one::<String>("claim")
        .context("path not provided")?;
    let name = args
        .get_one::<String>("name")
        .context("name not provided")?;
    let local_path = args
        .get_one::<String>("path")
        .context("where locally to bound contaiener to the FS")?;
    let is_encrypted = args
        .get_one::<bool>("encrypted")
        .unwrap_or(&false)
        .to_owned();

    let local_path = PathBuf::from(local_path);
    if !local_path.exists() || !local_path.is_dir() {
        return Err(anyhow!("local directory does not seems to exist"));
    }

    let cargo_template = LocalFilesystemStorageTemplate::new(local_path.clone());
    let cargo_template = StorageTemplate::try_from(cargo_template)?;

    // i think we can drop the handle, its enough to have it created somewhere
    // future access to the containers is over cargo_user.get_containers()
    let cthandle =
        cargo_user.create_container(s!(name), &cargo_template, s!(path), is_encrypted)?;

    let uuid = cthandle.uuid();
    let local_path_full = local_path.join(name).join(uuid.to_string());
    std::fs::create_dir_all(local_path_full).context("container created but i failed to create local directory. File oprations referencing this container may fail")?;

    Ok(Some(aux_container_info(&cthandle)))
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_ct_mount(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let (_, cargo_user) = match_context!(context, cargo => Some(_), cargo_user => Some(_))?;

    let name_or_uuid = args
        .get_one::<String>("name-or-uuid")
        .context("path/uuid not provided")?;

    let containers = cargo_user
        .find_containers(None, MountState::Unmounted)
        .context(anyhow!("failed to find eligible containers"))?;

    let container = if let Ok(uuid) = uuid::Uuid::parse_str(name_or_uuid) {
        tracing::debug!("trying to find container by uuid");
        println!("trying to find container by uuid");
        containers
            .iter()
            .find(|c| c.uuid() == uuid)
            .context("container not found or is already mounted")?
    } else {
        tracing::debug!("failed to parse param as an uuid, trying to find container by name");
        println!("failed to parse param as an uuid, trying to find container by name");
        containers
            .iter()
            .find(|c| c.name() == Ok(name_or_uuid.clone()))
            .context("container not found or is already mounted")?
    };

    let container_info = aux_container_info(container);

    container
        .mount(None)
        .context(s!("failed to mount container: {}", container_info))?;

    Ok(Some(s!("mounted container: {}", container_info)))
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_ct_remove(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let (_, cargo_user) = match_context!(context, cargo => Some(_), cargo_user => Some(_))?;

    let name_or_uuid = args
        .get_one::<String>("name-or-uuid")
        .context("path/uuid not provided")?;

    let containers = cargo_user
        .find_containers(None, MountState::Unmounted)
        .context(anyhow!("failed to find eligible containers"))?;

    let container = if let Ok(uuid) = uuid::Uuid::parse_str(name_or_uuid) {
        tracing::debug!("trying to find container by uuid");
        containers
            .iter()
            .find(|c| c.uuid() == uuid)
            .context("container not found or is mounted")?
    } else {
        println!("failed to parse param as an uuid, trying to find container by name");
        containers
            .iter()
            .find(|c| c.name() == Ok(name_or_uuid.clone()))
            .context("container not found or is mounted")?
    };

    let container_info = aux_container_info(container);

    container
        .remove()
        .context(s!("failed to remove container: {}", container_info))?;

    Ok(Some(s!("removed container {}", container_info)))
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_ct_unmount(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let (_, cargo_user) = match_context!(context, cargo => Some(_), cargo_user => Some(_))?;

    let name_or_uuid = args
        .get_one::<String>("name-or-uuid")
        .context("path/uuid not provided")?;

    let containers = cargo_user
        .find_containers(None, MountState::Mounted)
        .context(anyhow!("failed to find eligible containers"))?;

    let container = if let Ok(uuid) = uuid::Uuid::parse_str(name_or_uuid) {
        tracing::debug!("trying to find container by uuid");
        println!("trying to find container by uuid");
        containers
            .iter()
            .find(|c| c.uuid() == uuid)
            .context("container not found or is already mounted")?
    } else {
        tracing::debug!("failed to parse param as an uuid, trying to find container by name");
        println!("failed to parse param as an uuid, trying to find container by name");
        containers
            .iter()
            .find(|c| c.name() == Ok(name_or_uuid.clone()))
            .context("container not found or is already mounted")?
    };

    let container_info = aux_container_info(container);

    container
        .unmount(None)
        .context(s!("failed to unmount container: {}", container_info))?;

    Ok(Some(s!("unmount container: {}", container_info)))
}

// extend the repl with cargo commands
pub fn extend(repl: Repl<Context, anyhow::Error>) -> Repl<Context, anyhow::Error> {
    repl.with_command(
        Command::new("ct-new-file")
            .about("container->create new from file template")
            .arg(Arg::new("name").required(true).index(1))
            .arg(Arg::new("claim").required(true).index(2))
            .arg(Arg::new("template_file").required(true).index(3))
            .arg(
                Arg::new("encrypted")
                    .short('e')
                    .help("encrypt storage")
                    .action(clap::ArgAction::SetTrue),
            ),
        h_ct_create_from_file,
    )
    .with_command(
        Command::new("ct-new-lfs")
            .about("container->create new local FS storage")
            .arg(Arg::new("name").required(true).index(1))
            .arg(Arg::new("claim").required(true).index(2))
            .arg(Arg::new("path").required(true).index(3))
            .arg(
                Arg::new("encrypted")
                    .short('e')
                    .help("encrypt storage")
                    .action(clap::ArgAction::SetTrue),
            ),
        h_ct_create_lfs,
    )
    .with_command(
        Command::new("ct-mount")
            .about("container->mount by name or uuid")
            .arg(Arg::new("name-or-uuid").required(true).index(1)),
        h_ct_mount,
    )
    .with_command(
        Command::new("ct-unmount")
            .about("container->unmount")
            .arg(Arg::new("name-or-uuid").required(true).index(1)),
        h_ct_unmount,
    )
    .with_command(
        Command::new("ct-list").about("container->list containers"),
        h_ct_list,
    )
    .with_command(
        Command::new("ct-remove")
            .about("container->remove containers")
            .arg(Arg::new("name-or-uuid").required(true).index(1)),
        h_ct_remove,
    )
}