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
//
// 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::{Arg, ArgMatches, Command};
use reedline_repl_rs::Repl;
use s_macro::s;
pub use wildland_cargo_lib::api::CargoLib;

use crate::context::ContextError;
use crate::dropins::cargo_cfg;
use crate::{match_context, Context};

/*
   ===========================
   handler implementations
   ===========================
*/

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_cargo_config_load(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    match_context!(context, cargo_cfg => None)?;
    let config_path = args
        .get_one::<String>("config_path")
        .context("config path not provided")?;
    let config = cargo_cfg::handler::load_json_config_from_file(config_path)
        .context("failed to load config")?;
    context.set_cargo_config(config)?;
    Ok(Some(s!("config loaded")))
}

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

    let local_fsacfg = cargo_cfg.fsa_config.clone();
    let catlib_cfg = cargo_cfg.catlib_config.clone();
    let user_cfg = cargo_cfg.multidevice_state.clone();
    let cargo = CargoLib::new(lss.boxed(), local_fsacfg, catlib_cfg, user_cfg);

    if let Err(e) = context.set_full_cargo(cargo?) {
        match e {
            ContextError::Recoverable(inner) => {
                eprintln!("{}", inner);
            }
            _ => return Err(e.into()),
        }
    }
    Ok(Some(s!("Cargo Loaded, user is restored")))
}

// extend the repl with cargo commands
pub fn extend(repl: Repl<Context, anyhow::Error>) -> Repl<Context, anyhow::Error> {
    repl.with_command(
        Command::new("cargo-config-load")
            .about("Load or replace cargo config from file")
            .arg(Arg::new("config_path").required(true).index(1)),
        h_cargo_config_load,
    )
    .with_command(
        Command::new("cargo-lib-create").about("Create a cargo library"),
        h_cargo_lib_create,
    )
}