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
//
// 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 anyhow::Context as _;
use reedline_repl_rs::clap::ArgMatches;
use s_macro::s;
pub use wildland_cargo_lib::api::CargoLib;

use crate::{match_context, Context};

#[tracing::instrument(level = "trace", err(Debug), ret, skip(args, ctx))]
pub(crate) fn h_evs_request(args: ArgMatches, ctx: &mut Context) -> anyhow::Result<Option<String>> {
    let path = args
        .get_one::<String>("templatename")
        .context("template name to save template not provided")?;

    let email = args
        .get_one::<String>("email")
        .context("email for the account")?;

    let userapi = match_context!(ctx, cargo_user => Some(_))?;

    let handle = userapi
        .request_free_tier_storage(email.to_string())
        .map_err(|e| anyhow::anyhow!("failed to request storage from EVS {e}"))?;
    println!("process of requesting the storage started, please check your email for the confirmation link");
    println!("once you get the code, insert it here:");
    let mut code = String::new();
    std::io::stdin().read_line(&mut code)?;
    let code = code.trim();
    let template = handle
        .verify_email(code.to_string())
        .map_err(|e| anyhow::anyhow!("failed to verify the email {e}"))?;
    let template = serde_json::to_string_pretty(&template)
        .map_err(|e| anyhow::anyhow!("failed to serialize the template {e}"))?;
    std::fs::write(path, template)?;
    Ok(Some(s!("Ok: Storage is written to {path}")))
}