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
//
// 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;
use wildland_corex::LocalSecureStorage;

use super::sledlss::SledLss;
use crate::{match_context, Context};

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_create(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    match_context!(context, lss => None, cargo => None)?;
    let path = args
        .get_one::<String>("path")
        .context("path not provided")?;
    context.set_lss(SledLss::new(s!(path)))?;
    Ok(Some(s!("global lss instance created")))
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_insert(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    let key = args.get_one::<String>("key").context("key not provided")?;
    let value = args
        .get_one::<String>("value")
        .context("value not provided")?;
    lss.insert(s!(key), s!(value))?;
    Ok(Some(s!("key inserted")))
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_get(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    let key = args.get_one::<String>("key").context("key not provided")?;
    let value = lss.get(s!(key))?;
    Ok(value)
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_contains_key(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    let key = args.get_one::<String>("key").context("key not provided")?;
    let value = lss.contains_key(s!(key))?;
    Ok(Some(s!(value)))
}

#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_keys(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    let value = lss.keys()?;
    Ok(Some(value.join(",")))
}

#[tracing::instrument(level = "trace", err(Debug), skip(args, context))]
fn h_lss_remove(args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    let key = args.get_one::<String>("key").context("key not provided")?;
    let value = lss.remove(s!(key))?;
    Ok(value)
}

#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_len(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    let value = lss.len()?;
    Ok(Some(s!(value)))
}

#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_wipe(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    for each in lss.keys()? {
        lss.remove(s!(each))?;
    }
    Ok(Some(s!("lss wiped")))
}

#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_is_empty(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    let value = lss.is_empty()?;
    Ok(Some(s!(value)))
}

#[tracing::instrument(level = "trace", err(Debug), skip(_args, context))]
fn h_lss_wipe_clean(_args: ArgMatches, context: &mut Context) -> anyhow::Result<Option<String>> {
    let lss = match_context!(context, lss => Some(_))?;
    tracing::warn!("Wiping clean the local secure storage");
    let keys = lss.keys()?;
    for key in keys {
        lss.remove(s!(key))?;
    }
    Ok(Some(s!()))
}

pub fn extend(repl: Repl<Context, anyhow::Error>) -> Repl<Context, anyhow::Error> {
    repl.with_command(
        Command::new("lss-insert")
            .about("Insert a key/value pair into the local secure storage")
            .arg(Arg::new("key").required(true))
            .arg(Arg::new("value").required(true)),
        h_lss_insert,
    )
    .with_command(
        Command::new("lss-get")
            .about("Get a value from the local secure storage")
            .arg(Arg::new("key").required(true)),
        h_lss_get,
    )
    .with_command(
        Command::new("lss-create")
            .about("Create or load local secure storage")
            .arg(Arg::new("path").required(true)),
        h_lss_create,
    )
    .with_command(
        Command::new("lss-contains-key")
            .about("Check if a key exists in the local secure storage")
            .arg(Arg::new("key").required(true)),
        h_lss_contains_key,
    )
    .with_command(
        Command::new("lss-keys").about("Get all keys from the local secure storage"),
        h_lss_keys,
    )
    .with_command(
        Command::new("lss-remove")
            .about("Remove a key from the local secure storage")
            .arg(Arg::new("key").required(true)),
        h_lss_remove,
    )
    .with_command(
        Command::new("lss-len").about("Get the number of keys in the local secure storage"),
        h_lss_len,
    )
    .with_command(
        Command::new("lss-wipe").about("Wipe LSS content"),
        h_lss_wipe,
    )
    .with_command(
        Command::new("lss-is-empty").about("Check if the local secure storage is empty"),
        h_lss_is_empty,
    )
    .with_command(
        Command::new("lss-wipe-clean").about("wipe clean the local secure storage"),
        h_lss_wipe_clean,
    )
}