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
mod api;
mod command;
mod data;
mod services;

use super::jormungandr::JormungandrProcess;
use crate::testing::configuration;
use api::{Address, Certificate, Genesis, Key, Rest, Transaction, Votes};
pub use command::JCliCommand;
pub use data::{Witness, WitnessData, WitnessType};
use jormungandr_lib::crypto::hash::Hash;
pub use services::{
    CertificateBuilder, Error, FragmentCheck, FragmentSender, FragmentsCheck, TransactionBuilder,
};
use std::{
    path::{Path, PathBuf},
    process::Command,
};

#[derive(Clone, Debug)]
pub struct JCli {
    exe: PathBuf,
}

impl Default for JCli {
    fn default() -> Self {
        Self::new(configuration::get_jcli_app())
    }
}

impl JCli {
    pub fn new(exe: PathBuf) -> Self {
        Self { exe }
    }

    pub fn path(&self) -> &Path {
        self.exe.as_path()
    }

    pub fn genesis(&self) -> Genesis {
        let command = Command::new(self.exe.clone());
        let jcli_command = JCliCommand::new(command);
        Genesis::new(jcli_command.genesis())
    }

    pub fn key(&self) -> Key {
        let command = Command::new(self.exe.clone());
        let jcli_command = JCliCommand::new(command);
        Key::new(jcli_command.key())
    }

    pub fn address(&self) -> Address {
        let command = Command::new(self.exe.clone());
        let jcli_command = JCliCommand::new(command);
        Address::new(jcli_command.address())
    }

    pub fn rest(&self) -> Rest {
        let command = Command::new(self.exe.clone());
        let jcli_command = JCliCommand::new(command);
        Rest::new(jcli_command.rest())
    }

    pub fn transaction(&self) -> Transaction {
        let command = Command::new(self.exe.clone());
        let jcli_command = JCliCommand::new(command);
        Transaction::new(jcli_command.transaction())
    }

    pub fn certificate(&self) -> Certificate {
        let command = Command::new(self.exe.clone());
        let jcli_command = JCliCommand::new(command);
        Certificate::new(jcli_command.certificate())
    }

    pub fn votes(&self) -> Votes {
        let command = Command::new(self.exe.clone());
        let jcli_command = JCliCommand::new(command);
        Votes::new(jcli_command.votes())
    }

    pub fn fragment_sender<'a>(&self, jormungandr: &'a JormungandrProcess) -> FragmentSender<'a> {
        FragmentSender::new(self.clone(), jormungandr)
    }

    pub fn transaction_builder(&self, genesis_hash: Hash) -> TransactionBuilder {
        TransactionBuilder::new(self.clone(), genesis_hash)
    }

    pub fn certificate_builder(&self) -> CertificateBuilder {
        CertificateBuilder::new(self.clone())
    }

    pub fn fragments_checker<'a>(&self, jormungandr: &'a JormungandrProcess) -> FragmentsCheck<'a> {
        FragmentsCheck::new(self.clone(), jormungandr)
    }
}