-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
util.rs
86 lines (73 loc) · 2.61 KB
/
util.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::ffi::{OsStr, OsString};
use std::io::IsTerminal;
use std::process::{Command, Output};
use std::{collections::BTreeMap, fmt::Debug, fs, io::ErrorKind, path::Path};
use anyhow::{Context as _, Result};
use once_cell::sync::Lazy;
use serde::Deserialize;
use serde_json::Value;
pub static IS_A_TTY: Lazy<bool> = Lazy::new(|| std::io::stdout().is_terminal());
#[derive(Deserialize)]
pub struct CargoTomlPackage {
pub version: String,
}
/// The bits of the top-level `Cargo.toml` configuration that `vdev` uses to drive its features.
#[derive(Deserialize)]
pub struct CargoToml {
pub package: CargoTomlPackage,
pub features: BTreeMap<String, Value>,
}
impl CargoToml {
pub fn load() -> Result<CargoToml> {
let text = fs::read_to_string("Cargo.toml").context("Could not read `Cargo.toml`")?;
toml::from_str::<CargoToml>(&text).context("Invalid contents in `Cargo.toml`")
}
}
/// Read the version string from `Cargo.toml`
pub fn read_version() -> Result<String> {
CargoToml::load().map(|cargo| cargo.package.version)
}
/// Use the version provided by env vars or default to reading from `Cargo.toml`.
pub fn get_version() -> Result<String> {
std::env::var("VERSION")
.or_else(|_| std::env::var("VECTOR_VERSION"))
.or_else(|_| read_version())
}
pub fn git_head() -> Result<Output> {
Command::new("git")
.args(["describe", "--exact-match", "--tags", "HEAD"])
.output()
.context("Could not execute `git`")
}
pub fn get_channel() -> String {
std::env::var("CHANNEL").unwrap_or_else(|_| "custom".to_string())
}
pub fn exists(path: impl AsRef<Path> + Debug) -> Result<bool> {
match fs::metadata(path.as_ref()) {
Ok(_) => Ok(true),
Err(error) if error.kind() == ErrorKind::NotFound => Ok(false),
Err(error) => Err(error).context(format!("Could not stat {path:?}")),
}
}
pub trait ChainArgs {
fn chain_args<I: Into<OsString>>(&self, args: impl IntoIterator<Item = I>) -> Vec<OsString>;
fn chain_arg(&self, arg: impl Into<OsString>) -> Vec<OsString> {
self.chain_args([arg])
}
}
impl<T: AsRef<OsStr>> ChainArgs for Vec<T> {
fn chain_args<I: Into<OsString>>(&self, args: impl IntoIterator<Item = I>) -> Vec<OsString> {
self.iter()
.map(Into::into)
.chain(args.into_iter().map(Into::into))
.collect()
}
}
impl<T: AsRef<OsStr>> ChainArgs for [T] {
fn chain_args<I: Into<OsString>>(&self, args: impl IntoIterator<Item = I>) -> Vec<OsString> {
self.iter()
.map(Into::into)
.chain(args.into_iter().map(Into::into))
.collect()
}
}