soroban_cli/commands/env/
mod.rs

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
use crate::{
    commands::global,
    config::locator::{self},
    print::Print,
};
use clap::Parser;

#[derive(Debug, Parser)]
pub struct Cmd {
    #[command(flatten)]
    pub config_locator: locator::Args,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Locator(#[from] locator::Error),
}

impl Cmd {
    pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
        let print = Print::new(global_args.quiet);
        let mut lines: Vec<(String, String)> = Vec::new();

        if let Some(data) = get("STELLAR_NETWORK") {
            lines.push(data);
        }

        if let Some(data) = get("STELLAR_ACCOUNT") {
            lines.push(data);
        }

        if lines.is_empty() {
            print.warnln("No defaults or environment variables set".to_string());
            return Ok(());
        }

        let max_len = lines.iter().map(|l| l.0.len()).max().unwrap_or(0);

        lines.sort();

        for (value, source) in lines {
            println!("{value:max_len$} # {source}");
        }

        Ok(())
    }
}

fn get(env_var: &str) -> Option<(String, String)> {
    // The _SOURCE env var is set from cmd/soroban-cli/src/cli.rs#set_env_value_from_config
    let source = std::env::var(format!("{env_var}_SOURCE"))
        .ok()
        .unwrap_or("env".to_string());

    if let Ok(value) = std::env::var(env_var) {
        return Some((format!("{env_var}={value}"), source));
    }

    None
}