373 lines
12 KiB
Rust
373 lines
12 KiB
Rust
//! `http-client.env.json` and `http-client.private.env.json`.
|
|
//!
|
|
//! Both files have the shape `{ "env": { "var": "value" } }`. The special
|
|
//! `$shared` environment applies to every environment (JetBrains convention).
|
|
//! Non-string values (numbers, booleans, objects such as JetBrains'
|
|
//! `SSLConfiguration`) are kept and written back unchanged.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use serde_json::{Map, Value};
|
|
|
|
use crate::template::{Resolver, VarSource};
|
|
|
|
pub const PUBLIC_FILE: &str = "http-client.env.json";
|
|
pub const PRIVATE_FILE: &str = "http-client.private.env.json";
|
|
pub const SHARED_ENV: &str = "$shared";
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum EnvError {
|
|
#[error("{path}: {source}")]
|
|
Io { path: PathBuf, source: std::io::Error },
|
|
#[error("{path}: {source}")]
|
|
Json { path: PathBuf, source: serde_json::Error },
|
|
#[error("{path}: expected an object of environments")]
|
|
Shape { path: PathBuf },
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct EnvVar {
|
|
pub name: String,
|
|
pub value: String,
|
|
/// The original JSON value when it was not a plain string.
|
|
original: Option<Value>,
|
|
}
|
|
|
|
impl EnvVar {
|
|
pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
|
|
Self {
|
|
name: name.into(),
|
|
value: value.into(),
|
|
original: None,
|
|
}
|
|
}
|
|
|
|
/// Objects and arrays aren't variables; they're shown read-only.
|
|
pub fn is_structured(&self) -> bool {
|
|
matches!(self.original, Some(Value::Object(_) | Value::Array(_)))
|
|
}
|
|
|
|
fn to_json(&self) -> Value {
|
|
match &self.original {
|
|
Some(v) if scalar_string(v) == self.value || self.is_structured() => v.clone(),
|
|
_ => Value::String(self.value.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn scalar_string(v: &Value) -> String {
|
|
match v {
|
|
Value::String(s) => s.clone(),
|
|
Value::Null => String::new(),
|
|
other => other.to_string(),
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
|
pub struct Environment {
|
|
pub name: String,
|
|
pub vars: Vec<EnvVar>,
|
|
}
|
|
|
|
impl Environment {
|
|
pub fn get(&self, name: &str) -> Option<&str> {
|
|
self.vars
|
|
.iter()
|
|
.find(|v| v.name == name && !v.is_structured())
|
|
.map(|v| v.value.as_str())
|
|
}
|
|
|
|
pub fn set(&mut self, name: &str, value: impl Into<String>) {
|
|
let value = value.into();
|
|
match self.vars.iter_mut().find(|v| v.name == name) {
|
|
Some(v) => v.value = value,
|
|
None => self.vars.push(EnvVar::new(name, value)),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The contents of one env file.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
|
pub struct EnvFile {
|
|
pub envs: Vec<Environment>,
|
|
}
|
|
|
|
impl EnvFile {
|
|
pub fn parse(text: &str, path: &Path) -> Result<Self, EnvError> {
|
|
let value: Value = serde_json::from_str(text).map_err(|source| EnvError::Json {
|
|
path: path.to_path_buf(),
|
|
source,
|
|
})?;
|
|
let Value::Object(map) = value else {
|
|
return Err(EnvError::Shape {
|
|
path: path.to_path_buf(),
|
|
});
|
|
};
|
|
let mut envs = Vec::new();
|
|
for (name, vars) in map {
|
|
let Value::Object(vars) = vars else {
|
|
return Err(EnvError::Shape {
|
|
path: path.to_path_buf(),
|
|
});
|
|
};
|
|
envs.push(Environment {
|
|
name,
|
|
vars: vars
|
|
.into_iter()
|
|
.map(|(k, v)| EnvVar {
|
|
name: k,
|
|
value: scalar_string(&v),
|
|
original: (!v.is_string()).then_some(v),
|
|
})
|
|
.collect(),
|
|
});
|
|
}
|
|
Ok(Self { envs })
|
|
}
|
|
|
|
pub fn to_json(&self) -> String {
|
|
let mut root = Map::new();
|
|
for env in &self.envs {
|
|
let mut vars = Map::new();
|
|
for v in &env.vars {
|
|
vars.insert(v.name.clone(), v.to_json());
|
|
}
|
|
root.insert(env.name.clone(), Value::Object(vars));
|
|
}
|
|
let mut s = serde_json::to_string_pretty(&Value::Object(root)).unwrap();
|
|
s.push('\n');
|
|
s
|
|
}
|
|
|
|
pub fn env(&self, name: &str) -> Option<&Environment> {
|
|
self.envs.iter().find(|e| e.name == name)
|
|
}
|
|
|
|
pub fn env_mut(&mut self, name: &str) -> &mut Environment {
|
|
if let Some(i) = self.envs.iter().position(|e| e.name == name) {
|
|
return &mut self.envs[i];
|
|
}
|
|
self.envs.push(Environment {
|
|
name: name.to_string(),
|
|
vars: Vec::new(),
|
|
});
|
|
self.envs.last_mut().unwrap()
|
|
}
|
|
|
|
/// Variables of `env` with `$shared` underneath.
|
|
fn vars_for(&self, env: &str) -> Vec<(String, String)> {
|
|
let mut out: Vec<(String, String)> = Vec::new();
|
|
for e in [SHARED_ENV, env] {
|
|
if let Some(e) = self.env(e) {
|
|
for v in e.vars.iter().filter(|v| !v.is_structured()) {
|
|
out.retain(|(n, _)| n != &v.name);
|
|
out.push((v.name.clone(), v.value.clone()));
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
}
|
|
|
|
/// Both env files of a collection.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
|
pub struct Environments {
|
|
pub public: EnvFile,
|
|
pub private: EnvFile,
|
|
}
|
|
|
|
impl Environments {
|
|
/// Loads the env files in `dir`. Missing files are treated as empty.
|
|
pub fn load(dir: &Path) -> Result<Self, EnvError> {
|
|
let read = |name: &str| -> Result<EnvFile, EnvError> {
|
|
let path = dir.join(name);
|
|
match std::fs::read_to_string(&path) {
|
|
Ok(text) if text.trim().is_empty() => Ok(EnvFile::default()),
|
|
Ok(text) => EnvFile::parse(&text, &path),
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(EnvFile::default()),
|
|
Err(source) => Err(EnvError::Io { path, source }),
|
|
}
|
|
};
|
|
Ok(Self {
|
|
public: read(PUBLIC_FILE)?,
|
|
private: read(PRIVATE_FILE)?,
|
|
})
|
|
}
|
|
|
|
/// Environment names from both files, in file order, without `$shared`.
|
|
pub fn names(&self) -> Vec<String> {
|
|
let mut names: Vec<String> = Vec::new();
|
|
for e in self.public.envs.iter().chain(&self.private.envs) {
|
|
if e.name != SHARED_ENV && !names.contains(&e.name) {
|
|
names.push(e.name.clone());
|
|
}
|
|
}
|
|
names
|
|
}
|
|
|
|
pub fn is_private(&self, env: &str, var: &str) -> bool {
|
|
[env, SHARED_ENV]
|
|
.iter()
|
|
.any(|e| self.private.env(e).and_then(|e| e.get(var)).is_some())
|
|
}
|
|
|
|
/// Builds a resolver for `env` (or no environment) plus file variables.
|
|
pub fn resolver(&self, env: Option<&str>, file_vars: &[(String, String)]) -> Resolver {
|
|
let env = env.unwrap_or(SHARED_ENV);
|
|
Resolver::new()
|
|
.with_layer(VarSource::PrivateEnv, self.private.vars_for(env))
|
|
.with_layer(VarSource::PublicEnv, self.public.vars_for(env))
|
|
.with_layer(VarSource::File, file_vars.iter().cloned())
|
|
}
|
|
}
|
|
|
|
/// The directory whose env files apply to a `.http` file in `start`: the
|
|
/// nearest of `start` and its ancestors that has an env file.
|
|
pub fn find_env_dir(start: &Path) -> Option<PathBuf> {
|
|
start
|
|
.ancestors()
|
|
.find(|d| d.join(PUBLIC_FILE).is_file() || d.join(PRIVATE_FILE).is_file())
|
|
.map(Path::to_path_buf)
|
|
}
|
|
|
|
/// Whether a `.gitignore` in `dir` or one of its parents (up to the
|
|
/// repository root) ignores the private env file.
|
|
pub fn private_file_ignored(dir: &Path) -> bool {
|
|
let mut cur = Some(dir);
|
|
while let Some(d) = cur {
|
|
if let Ok(text) = std::fs::read_to_string(d.join(".gitignore")) {
|
|
if text.lines().any(|l| gitignore_matches(l.trim(), PRIVATE_FILE)) {
|
|
return true;
|
|
}
|
|
}
|
|
if d.join(".git").exists() {
|
|
break;
|
|
}
|
|
cur = d.parent();
|
|
}
|
|
false
|
|
}
|
|
|
|
fn gitignore_matches(pattern: &str, file: &str) -> bool {
|
|
if pattern.is_empty() || pattern.starts_with('#') || pattern.starts_with('!') {
|
|
return false;
|
|
}
|
|
let p = pattern.trim_start_matches("**/").trim_start_matches('/');
|
|
glob(p, file)
|
|
}
|
|
|
|
/// Minimal glob with `*` and `?`.
|
|
fn glob(p: &str, s: &str) -> bool {
|
|
let (p, s) = (p.as_bytes(), s.as_bytes());
|
|
let (mut pi, mut si, mut star, mut mark) = (0, 0, None, 0);
|
|
while si < s.len() {
|
|
if pi < p.len() && (p[pi] == b'?' || p[pi] == s[si]) {
|
|
pi += 1;
|
|
si += 1;
|
|
} else if pi < p.len() && p[pi] == b'*' {
|
|
star = Some(pi);
|
|
mark = si;
|
|
pi += 1;
|
|
} else if let Some(st) = star {
|
|
pi = st + 1;
|
|
mark += 1;
|
|
si = mark;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
p[pi..].iter().all(|&c| c == b'*')
|
|
}
|
|
|
|
/// Text to append to a `.gitignore` so the private env file is ignored.
|
|
pub fn gitignore_addition(existing: &str) -> String {
|
|
let mut s = String::new();
|
|
if !existing.is_empty() && !existing.ends_with('\n') {
|
|
s.push('\n');
|
|
}
|
|
s.push_str(PRIVATE_FILE);
|
|
s.push('\n');
|
|
s
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
const PUBLIC: &str = r#"{
|
|
"$shared": { "version": "v1", "base_url": "http://shared" },
|
|
"dev": { "base_url": "http://localhost:8080", "port": 8080, "SSLConfiguration": { "verifyHostCertificate": false } },
|
|
"prod": { "base_url": "https://api.example.com" }
|
|
}"#;
|
|
const PRIVATE: &str = r#"{ "dev": { "token": "dev-secret" }, "prod": { "token": "prod-secret" } }"#;
|
|
|
|
fn envs() -> Environments {
|
|
Environments {
|
|
public: EnvFile::parse(PUBLIC, Path::new("pub")).unwrap(),
|
|
private: EnvFile::parse(PRIVATE, Path::new("priv")).unwrap(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn names_skip_shared() {
|
|
assert_eq!(envs().names(), vec!["dev", "prod"]);
|
|
}
|
|
|
|
#[test]
|
|
fn resolves_with_shared_and_private() {
|
|
let r = envs().resolver(Some("dev"), &[("file_var".into(), "{{version}}".into())]);
|
|
assert_eq!(
|
|
r.resolve("{{base_url}}/{{version}}/{{port}}?t={{token}}&f={{file_var}}")
|
|
.unwrap(),
|
|
"http://localhost:8080/v1/8080?t=dev-secret&f=v1"
|
|
);
|
|
assert!(r.resolve("{{SSLConfiguration}}").is_err());
|
|
assert!(envs().is_private("dev", "token"));
|
|
assert!(!envs().is_private("dev", "base_url"));
|
|
}
|
|
|
|
#[test]
|
|
fn write_keeps_non_string_values() {
|
|
let mut f = EnvFile::parse(PUBLIC, Path::new("pub")).unwrap();
|
|
let again = EnvFile::parse(&f.to_json(), Path::new("pub")).unwrap();
|
|
assert_eq!(f, again);
|
|
assert!(f.to_json().contains("\"port\": 8080"));
|
|
f.env_mut("dev").set("port", "9090");
|
|
assert!(f.to_json().contains("\"port\": \"9090\""));
|
|
f.env_mut("staging").set("base_url", "http://staging");
|
|
assert!(f.to_json().contains("\"staging\": {"));
|
|
}
|
|
|
|
#[test]
|
|
fn gitignore_patterns() {
|
|
for p in [
|
|
"http-client.private.env.json",
|
|
"/http-client.private.env.json",
|
|
"**/http-client.private.env.json",
|
|
"*.private.env.json",
|
|
"*private*",
|
|
] {
|
|
assert!(gitignore_matches(p, PRIVATE_FILE), "{p}");
|
|
}
|
|
for p in [
|
|
"# http-client.private.env.json",
|
|
"*.http",
|
|
"!http-client.private.env.json",
|
|
"",
|
|
] {
|
|
assert!(!gitignore_matches(p, PRIVATE_FILE), "{p}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn gitignore_lookup_walks_up_to_repo_root() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
std::fs::create_dir(dir.path().join(".git")).unwrap();
|
|
let sub = dir.path().join("api");
|
|
std::fs::create_dir(&sub).unwrap();
|
|
assert!(!private_file_ignored(&sub));
|
|
std::fs::write(dir.path().join(".gitignore"), "target\n*.private.env.json\n").unwrap();
|
|
assert!(private_file_ignored(&sub));
|
|
}
|
|
}
|