Skip to main content

soroban_cli/commands/
version.rs

1use clap::Parser;
2use std::fmt::Debug;
3
4#[derive(Parser, Debug, Clone)]
5#[group(skip)]
6pub struct Cmd {
7    /// Print only the version.
8    #[arg(long, group = "only")]
9    only_version: bool,
10    /// Print only the major version.
11    #[arg(long, group = "only")]
12    only_version_major: bool,
13    /// Print only the commit sha.
14    #[arg(long, group = "only")]
15    only_commit: bool,
16}
17
18impl Cmd {
19    #[allow(clippy::unused_self)]
20    pub fn run(&self) {
21        if self.only_version {
22            println!("{}", pkg());
23        } else if self.only_version_major {
24            println!("{}", major());
25        } else if self.only_commit {
26            println!("{}", git());
27        } else {
28            println!("stellar {}", long());
29        }
30    }
31}
32
33pub fn pkg() -> &'static str {
34    env!("CARGO_PKG_VERSION")
35}
36
37pub fn major() -> &'static str {
38    env!("CARGO_PKG_VERSION_MAJOR")
39}
40
41pub fn git() -> &'static str {
42    env!("GIT_REVISION")
43}
44
45pub fn long() -> String {
46    let xdr = stellar_xdr::VERSION;
47    let git_rev = if git().is_empty() {
48        String::new()
49    } else {
50        format!(" ({})", git())
51    };
52
53    [
54        format!("{}{git_rev}", pkg()),
55        format!(
56            "stellar-xdr {} ({})
57xdr ({})",
58            xdr.pkg, xdr.rev, xdr.xdr,
59        ),
60    ]
61    .join("\n")
62}
63
64pub fn one_line() -> String {
65    let pkg = pkg();
66    let git = git();
67    format!("{pkg}#{git}")
68}