Make --config a global CLI option
Assisted-by: GLM-5.3 via pi
change rpurmpxvtsumwzvnklyptrtzwxvlunwy
commit 1a361867cdf830532a98c84476a4ff828705f8dd
author Alpha Chen <alpha@kejadlen.dev>
date
parent nwnklxrv
diff --git a/src/bin/parallax/main.rs b/src/bin/parallax/main.rs
index 2ab0212..c3b3904 100644
--- a/src/bin/parallax/main.rs
+++ b/src/bin/parallax/main.rs
@@ -10,6 +10,8 @@ use tracing_subscriber::EnvFilter;
 use tracing_subscriber::fmt;
 use tracing_subscriber::prelude::*;
 
+use parallax::Parallax;
+
 const VERSION: &str = env!("PARALLAX_VERSION");
 
 #[derive(Parser)]
@@ -19,6 +21,10 @@ const VERSION: &str = env!("PARALLAX_VERSION");
     about = "Run scripted flows against a web app and compare screenshots against a baseline"
 )]
 struct Cli {
+    /// Path to the config file
+    #[arg(long, global = true, default_value = "parallax.fnl")]
+    config: PathBuf,
+
     /// Generate shell completions and exit
     #[arg(long, value_enum)]
     completions: Option<Shell>,
@@ -29,23 +35,15 @@ struct Cli {
 
 #[derive(Subcommand)]
 enum Commands {
-    /// Load a config and run its flows
+    /// Run the parallax's flows
     Run {
-        /// Path to the config file
-        #[arg(default_value = "parallax.fnl")]
-        config: PathBuf,
-
         /// Override the output directory
         #[arg(long)]
         output: Option<PathBuf>,
     },
 
-    /// Load a config and print its flow names in order
-    List {
-        /// Path to the config file
-        #[arg(default_value = "parallax.fnl")]
-        config: PathBuf,
-    },
+    /// Print the parallax's flow names in order
+    List,
 }
 
 #[tokio::main]
@@ -68,9 +66,11 @@ async fn main() -> miette::Result<()> {
         return Ok(());
     };
 
+    let parallax = Parallax::load(&cli.config)?;
+
     match command {
-        Commands::Run { config, output } => parallax::run(&config, output.as_deref()).await?,
-        Commands::List { config } => parallax::list(&config)?,
+        Commands::Run { output } => parallax::run(&parallax, output.as_deref()).await?,
+        Commands::List => parallax::list(&parallax)?,
     }
     Ok(())
 }
diff --git a/src/run.rs b/src/run.rs
index 14f933e..970a73a 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -3,15 +3,13 @@ use std::path::Path;
 use crate::Error;
 use crate::Parallax;
 
-pub fn list(path: &Path) -> Result<(), Error> {
-    let parallax = Parallax::load(path)?;
+pub fn list(parallax: &Parallax) -> Result<(), Error> {
     for name in parallax.flow_names() {
         println!("{name}");
     }
     Ok(())
 }
 
-pub async fn run(path: &Path, _output: Option<&Path>) -> Result<(), Error> {
-    Parallax::load(path)?;
+pub async fn run(_parallax: &Parallax, _output: Option<&Path>) -> Result<(), Error> {
     Err(Error::NotImplemented)
 }
diff --git a/tests/cli.rs b/tests/cli.rs
index e843f68..b56c706 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -30,6 +30,7 @@ fn lists_flow_names_in_order() {
     let (_dir, path) = config_file(VALID);
     cargo_bin_cmd!("parallax")
         .arg("list")
+        .arg("--config")
         .arg(&path)
         .assert()
         .success()
@@ -51,8 +52,9 @@ fn list_defaults_to_parallax_fnl_in_the_current_directory() {
 #[test]
 fn missing_config_file_fails() {
     cargo_bin_cmd!("parallax")
-        .arg("list")
+        .arg("--config")
         .arg("missing.fnl")
+        .arg("list")
         .assert()
         .failure()
         .stderr(predicate::str::contains("missing.fnl"));
@@ -63,6 +65,7 @@ fn syntax_error_points_at_the_config_line() {
     let (_dir, path) = config_file("(local ok 1)\n)\n");
     cargo_bin_cmd!("parallax")
         .arg("list")
+        .arg("--config")
         .arg(&path)
         .assert()
         .failure()
@@ -86,6 +89,7 @@ fn validation_error_points_at_the_parallax_call() {
     let (_dir, path) = config_file(source);
     cargo_bin_cmd!("parallax")
         .arg("list")
+        .arg("--config")
         .arg(&path)
         .assert()
         .failure()
@@ -99,6 +103,7 @@ fn config_not_returning_a_parallax_call_fails() {
     let (_dir, path) = config_file("(local parallax (require :parallax))\n\n{:viewports []}\n");
     cargo_bin_cmd!("parallax")
         .arg("list")
+        .arg("--config")
         .arg(&path)
         .assert()
         .failure()
@@ -110,6 +115,7 @@ fn running_a_config_is_not_implemented_yet() {
     let (_dir, path) = config_file(VALID);
     cargo_bin_cmd!("parallax")
         .arg("run")
+        .arg("--config")
         .arg(&path)
         .assert()
         .failure()