summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.rs33
-rw-r--r--src/main.rs17
2 files changed, 38 insertions, 12 deletions
diff --git a/src/config.rs b/src/config.rs
index bda99e9..00ac3e4 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -167,8 +167,19 @@ impl Config {
/// `anyhow::Result` here meant two error vocabularies in one crate, and the
/// one thing a caller can actually branch on — "there is no config yet, run
/// init" — arrived as an opaque string.
+ ///
+ /// **The root is the directory the config file was found in.** The `root`
+ /// field inside the file is written for whoever is looking at the tree, not
+ /// as a second source of truth: `load` is only ever handed the directory it
+ /// then reads the config from, so a field that disagreed with its own
+ /// location could only ever contradict it. Honouring a relative field by
+ /// re-joining it onto the config's directory is what turned `--root runtime`
+ /// (which `init` used to save verbatim) into `runtime/runtime/…` — every
+ /// command after `init` ran against a second tree with its own database,
+ /// while `init` had created and migrated the first one.
pub fn load(root: &Path) -> crate::error::Result<Self> {
- let p = root.join("config.json");
+ let dir = absolute(root)?;
+ let p = dir.join("config.json");
if !p.exists() {
return Err(crate::error::Error::not_found(format!(
"no config at {}; run `aur-build init`",
@@ -177,19 +188,20 @@ impl Config {
}
let s = std::fs::read_to_string(&p)?;
let mut cfg: Config = serde_json::from_str(&s)?;
- // root inside the config file is relative to the config's directory
- // unless it is absolute; that keeps a moved tree usable.
- if !cfg.root.is_absolute() {
- cfg.root = root.join(&cfg.root);
- }
+ cfg.root = dir;
Ok(cfg)
}
pub fn save(&self) -> crate::error::Result<()> {
std::fs::create_dir_all(&self.root)?;
+ let mut out = self.clone();
+ // Absolutize on the way out, so a config written by a process that was
+ // handed a relative `--root` can never be misread — by the next command,
+ // or by a person editing it by hand.
+ out.root = absolute(&self.root)?;
std::fs::write(
self.root.join("config.json"),
- serde_json::to_string_pretty(self)? + "\n",
+ serde_json::to_string_pretty(&out)? + "\n",
)?;
Ok(())
}
@@ -319,6 +331,13 @@ impl Config {
}
}
+/// Absolutize a path without resolving symlinks and without requiring it to
+/// exist, both of which `canonicalize` insists on — and `init` saves its config
+/// before the tree under it is complete.
+fn absolute(p: &Path) -> crate::error::Result<PathBuf> {
+ Ok(std::path::absolute(p)?)
+}
+
/// Filesystem-safe slug for a URL, used in cache filenames.
pub fn slug(url: &str) -> String {
url.chars()
diff --git a/src/main.rs b/src/main.rs
index 91c086b..9e3b1e0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -118,12 +118,19 @@ enum Cmd {
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
- let cfg = match &cli.root {
- Some(r) => Config::load(r).unwrap_or_else(|_| Config {
- root: r.clone(),
+ // `init` is the only command that runs before a config exists. For everything
+ // else a missing config is the error it sounds like, not a reason to invent
+ // one: the old unconditional fallback turned a typo'd `--root` into a fresh
+ // empty server at the wrong path, whose commands then "succeeded" against a
+ // database with no subscriptions and no releases in it.
+ let root = cli.root.clone().unwrap_or_else(|| Config::default().root);
+ let cfg = if matches!(cli.cmd, Cmd::Init { .. }) {
+ Config::load(&root).unwrap_or_else(|_| Config {
+ root: root.clone(),
..Config::default()
- }),
- None => Config::load(&Config::default().root)?,
+ })
+ } else {
+ Config::load(&root)?
};
if let Cmd::Serve { bind } = &cli.cmd {
let mut cfg = cfg;