snarkos_node_env/
lib.rs

1// Copyright (C) 2019-2023 Aleo Systems Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{env, process::Command};
16
17use once_cell::sync::OnceCell;
18use serde::Serialize;
19
20// Contains the environment information.
21pub static ENV_INFO: OnceCell<EnvInfo> = OnceCell::new();
22
23// Environment information.
24#[allow(dead_code)]
25#[derive(Debug, Serialize)]
26pub struct EnvInfo {
27    package: String,
28    host: String,
29    rustc: String,
30    args: Vec<String>,
31    repo: String,
32    branch: String,
33    commit: String,
34}
35
36impl EnvInfo {
37    pub fn register() {
38        // A helper function to extract command output.
39        fn command(args: &[&str]) -> String {
40            let mut output = String::from_utf8(
41                Command::new(args[0]).args(&args[1..]).output().map(|out| out.stdout).unwrap_or_default(),
42            )
43            .unwrap_or_default();
44            output.pop(); // Strip the trailing newline.
45
46            output
47        }
48
49        // Process the rustc version information.
50        let rustc_info = command(&["rustc", "--version", "--verbose"]);
51        let rustc_info = rustc_info.split('\n').map(|line| line.split(": "));
52        let mut rustc = String::new();
53        let mut host = String::new();
54        for mut pair in rustc_info {
55            let key = pair.next();
56            let value = pair.next();
57
58            match (key, value) {
59                (Some(key), None) => {
60                    if key.starts_with("rustc ") {
61                        rustc = key.trim_start_matches("rustc ").to_owned();
62                    }
63                }
64                (Some(key), Some(value)) => {
65                    if key == "host" {
66                        host = value.to_string();
67                    }
68                }
69                _ => {}
70            }
71        }
72
73        // Process the runtime arguments, omitting any private keys in the process.
74        let args = env::args().filter(|arg| !arg.starts_with("APrivateKey")).collect::<Vec<_>>();
75
76        // Collect the information.
77        let env_info = EnvInfo {
78            package: env::var("CARGO_PKG_VERSION").unwrap_or_default(),
79            host,
80            rustc,
81            args,
82            repo: env::var("CARGO_PKG_REPOSITORY").unwrap_or_default(),
83            branch: command(&["git", "branch", "--show-current"]),
84            commit: command(&["git", "rev-parse", "HEAD"]),
85        };
86
87        // Set the static containing the information.
88        ENV_INFO.set(env_info).unwrap();
89    }
90}