vdev/
util.rs

1#![allow(clippy::print_stderr)]
2#![allow(clippy::print_stdout)]
3
4use std::{
5    collections::BTreeMap,
6    ffi::{OsStr, OsString},
7    fmt::Debug,
8    fs,
9    io::{ErrorKind, IsTerminal},
10    path::Path,
11    process,
12    process::{Command, Output},
13    sync::LazyLock,
14};
15
16use anyhow::{Context as _, Result};
17use serde::Deserialize;
18use serde_json::Value;
19
20pub static IS_A_TTY: LazyLock<bool> = LazyLock::new(|| std::io::stdout().is_terminal());
21
22#[derive(Deserialize)]
23pub struct CargoTomlPackage {
24    pub version: String,
25}
26
27/// The bits of the top-level `Cargo.toml` configuration that `vdev` uses to drive its features.
28#[derive(Deserialize)]
29pub struct CargoToml {
30    pub package: CargoTomlPackage,
31    pub features: BTreeMap<String, Value>,
32}
33
34impl CargoToml {
35    pub fn load() -> Result<CargoToml> {
36        let text = fs::read_to_string("Cargo.toml").context("Could not read `Cargo.toml`")?;
37        toml::from_str::<CargoToml>(&text).context("Invalid contents in `Cargo.toml`")
38    }
39}
40
41/// Read the version string from `Cargo.toml`
42pub fn read_version() -> Result<String> {
43    CargoToml::load().map(|cargo| cargo.package.version)
44}
45
46/// Use the version provided by env vars or default to reading from `Cargo.toml`.
47pub fn get_version() -> Result<String> {
48    std::env::var("VERSION")
49        .or_else(|_| std::env::var("VECTOR_VERSION"))
50        .or_else(|_| read_version())
51}
52
53pub fn git_head() -> Result<Output> {
54    Command::new("git")
55        .args(["describe", "--exact-match", "--tags", "HEAD"])
56        .output()
57        .context("Could not execute `git`")
58}
59
60pub fn get_channel() -> String {
61    std::env::var("CHANNEL").unwrap_or_else(|_| "custom".to_string())
62}
63
64pub fn exists(path: impl AsRef<Path> + Debug) -> Result<bool> {
65    match fs::metadata(path.as_ref()) {
66        Ok(_) => Ok(true),
67        Err(error) if error.kind() == ErrorKind::NotFound => Ok(false),
68        Err(error) => Err(error).context(format!("Could not stat {path:?}")),
69    }
70}
71
72pub trait ChainArgs {
73    fn chain_args<I: Into<OsString>>(&self, args: impl IntoIterator<Item = I>) -> Vec<OsString>;
74}
75
76impl<T: AsRef<OsStr>> ChainArgs for Vec<T> {
77    fn chain_args<I: Into<OsString>>(&self, args: impl IntoIterator<Item = I>) -> Vec<OsString> {
78        self.iter()
79            .map(Into::into)
80            .chain(args.into_iter().map(Into::into))
81            .collect()
82    }
83}
84
85impl<T: AsRef<OsStr>> ChainArgs for [T] {
86    fn chain_args<I: Into<OsString>>(&self, args: impl IntoIterator<Item = I>) -> Vec<OsString> {
87        self.iter()
88            .map(Into::into)
89            .chain(args.into_iter().map(Into::into))
90            .collect()
91    }
92}
93
94pub fn run_command(cmd: &str) -> String {
95    let output = Command::new("sh")
96        .arg("-c")
97        .arg(cmd)
98        .output()
99        .expect("Failed to execute command");
100
101    if !output.status.success() {
102        eprintln!(
103            "Command failed: {cmd} - Error: {}",
104            String::from_utf8_lossy(&output.stderr)
105        );
106        process::exit(1);
107    }
108
109    String::from_utf8_lossy(&output.stdout).to_string()
110}