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
use clap::Parser;
use jormungandr_automation::jormungandr::{explorer::Explorer, ExplorerError};
use std::{env, error::Error as _};

fn main() {
    Command::parse().exec().unwrap_or_else(report_error)
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Explorer(#[from] ExplorerError),
    #[error(transparent)]
    Io(#[from] std::io::Error),
}

fn report_error(error: Error) {
    eprintln!("{}", error);
    let mut source = error.source();
    while let Some(sub_error) = source {
        eprintln!("  |-> {}", sub_error);
        source = sub_error.source();
    }
    std::process::exit(1)
}

#[derive(Parser)]
#[clap(rename_all = "kebab-case")]
pub struct Command {
    /// display full version details (software version, source version, targets and compiler used)
    #[clap(long = "full-version")]
    full_version: bool,

    /// display the sources version, allowing to check the source's hash used to compile this executable.
    /// this option is useful for scripting retrieving the logs of the version of this application.
    #[clap(long = "source-version")]
    source_version: bool,

    /// explorer address
    #[clap(long = "address")]
    address: String,

    #[clap(subcommand)]
    command: Option<ExplorerClientCommand>,
}

/// Explorer Client CLI toolkit
#[derive(Parser)]
#[clap(rename_all = "kebab-case")]
pub enum ExplorerClientCommand {
    /// Get last block
    LastBlock,
}

impl Command {
    pub fn exec(self) -> Result<(), Error> {
        use std::io::Write as _;
        if self.full_version {
            Ok(writeln!(std::io::stdout(), "{}", env!("FULL_VERSION"))?)
        } else if self.source_version {
            Ok(writeln!(std::io::stdout(), "{}", env!("SOURCE_VERSION"))?)
        } else if let Some(cmd) = self.command {
            cmd.exec(self.address).map_err(Into::into)
        } else {
            writeln!(std::io::stderr(), "No command, try `--help'")?;
            std::process::exit(1);
        }
    }
}

impl ExplorerClientCommand {
    pub fn exec(self, address: String) -> Result<(), ExplorerError> {
        let mut explorer = Explorer::new(address);
        explorer.disable_logs();
        match self {
            Self::LastBlock => println!("{:#?}", explorer.last_block()?),
        };
        Ok(())
    }
}