ralph_workflow/diagnostics/
system.rs

1//! System information gathering.
2
3use std::process::Command;
4
5/// System information.
6#[derive(Debug, Clone)]
7pub struct SystemInfo {
8    pub os: String,
9    pub arch: String,
10    pub working_directory: Option<String>,
11    pub shell: Option<String>,
12    pub git_version: Option<String>,
13    pub git_repo: bool,
14    pub git_branch: Option<String>,
15    pub uncommitted_changes: Option<usize>,
16}
17
18impl SystemInfo {
19    /// Gather system information.
20    pub fn gather() -> Self {
21        let os = format!("{} {}", std::env::consts::OS, std::env::consts::ARCH);
22        let working_directory = std::env::current_dir()
23            .ok()
24            .map(|p| p.display().to_string());
25        let shell = std::env::var("SHELL").ok();
26
27        let git_version = Command::new("git")
28            .args(["--version"])
29            .output()
30            .ok()
31            .and_then(|o| String::from_utf8(o.stdout).ok())
32            .map(|s| s.trim().to_string());
33
34        let git_repo = Command::new("git")
35            .args(["rev-parse", "--git-dir"])
36            .output()
37            .map(|o| o.status.success())
38            .unwrap_or(false);
39
40        let git_branch = if git_repo {
41            Command::new("git")
42                .args(["branch", "--show-current"])
43                .output()
44                .ok()
45                .and_then(|o| String::from_utf8(o.stdout).ok())
46                .map(|s| s.trim().to_string())
47        } else {
48            None
49        };
50
51        let uncommitted_changes = if git_repo {
52            Command::new("git")
53                .args(["status", "--porcelain"])
54                .output()
55                .ok()
56                .and_then(|o| String::from_utf8(o.stdout).ok())
57                .map(|s| s.lines().count())
58        } else {
59            None
60        };
61
62        Self {
63            os,
64            working_directory,
65            shell,
66            git_version,
67            git_repo,
68            git_branch,
69            uncommitted_changes,
70            arch: std::env::consts::ARCH.to_string(),
71        }
72    }
73}