ggen_cli_lib/cmds/
utils.rs1use clap_noun_verb::Result;
6use clap_noun_verb_macros::verb;
7use serde::Serialize;
8use std::collections::HashMap;
9
10#[derive(Serialize)]
15struct EnvOutput {
16 variables: HashMap<String, String>,
17 total: usize,
18}
19
20#[derive(Serialize)]
22#[allow(dead_code)]
23struct EnvSetOutput {
24 key: String,
25 value: String,
26 success: bool,
27}
28
29#[verb]
35fn env(list: bool, get: Option<String>, set: Option<String>, system: bool) -> Result<EnvOutput> {
36 let _ = system; let variables = run_env(list, get.as_deref(), set.as_deref());
38 let total = variables.len();
39 Ok(EnvOutput { variables, total })
40}
41
42fn run_env(list: bool, get: Option<&str>, set: Option<&str>) -> HashMap<String, String> {
43 let mut variables = HashMap::new();
44
45 if let Some(key) = get {
46 if let Ok(value) = std::env::var(key) {
47 variables.insert(key.to_string(), value);
48 }
49 } else if let Some(kv) = set {
50 if let Some((key, value)) = kv.split_once('=') {
51 std::env::set_var(key, value);
52 variables.insert(key.to_string(), value.to_string());
53 }
54 }
55
56 if list || (get.is_none() && set.is_none()) {
57 collect_ggen_env_vars(&mut variables);
58 }
59
60 variables
61}
62
63fn collect_ggen_env_vars(vars: &mut HashMap<String, String>) {
64 for (key, value) in std::env::vars() {
65 if key.starts_with("GGEN_") || key.starts_with("RUST_") || key == "HOME" || key == "PATH" {
66 vars.insert(key, value);
67 }
68 }
69}