From baf65c08f7b3dfb9d0feca9fafa87f0058499349 Mon Sep 17 00:00:00 2001 From: Razvan Azamfirei Date: Fri, 30 Jun 2023 22:13:58 -0400 Subject: [PATCH 1/3] taplo-cli: add ENV variable support --- crates/taplo-cli/src/args.rs | 2 ++ crates/taplo-cli/src/lib.rs | 10 +++++++++- site/site/cli/usage/configuration.md | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/taplo-cli/src/args.rs b/crates/taplo-cli/src/args.rs index b2d9c8e8d..647abc509 100644 --- a/crates/taplo-cli/src/args.rs +++ b/crates/taplo-cli/src/args.rs @@ -22,6 +22,8 @@ pub struct TaploArgs { #[derive(Clone, Args)] pub struct GeneralArgs { /// Path to the Taplo configuration file. + /// + /// Configuration file can be specified in $TAPLO_CONFIG #[clap(long, short)] pub config: Option, diff --git a/crates/taplo-cli/src/lib.rs b/crates/taplo-cli/src/lib.rs index 2b08a67d5..f1fb6264e 100644 --- a/crates/taplo-cli/src/lib.rs +++ b/crates/taplo-cli/src/lib.rs @@ -2,6 +2,7 @@ use anyhow::{anyhow, Context}; use args::GeneralArgs; use itertools::Itertools; use std::{ + env, path::{Path, PathBuf}, sync::Arc, }; @@ -47,7 +48,14 @@ impl Taplo { if config_path.is_none() && !general.no_auto_config { if let Some(cwd) = self.env.cwd_normalized() { - config_path = self.env.find_config_file_normalized(&cwd).await + config_path = self.env.find_config_file_normalized(&cwd).await; + } + } + + if config_path.is_none() { + tracing::debug!("Looking for config path in the environment variable"); + if let Ok(path) = env::var("TAPLO_CONFIG") { + config_path = Some(path.into()); } } diff --git a/site/site/cli/usage/configuration.md b/site/site/cli/usage/configuration.md index dc3d52c4c..77235488f 100644 --- a/site/site/cli/usage/configuration.md +++ b/site/site/cli/usage/configuration.md @@ -24,3 +24,5 @@ The available log levels: Taplo CLI by default searches for a Taplo config file in the current working directory, this behaviour can be disabled by either supplying `--no-auto-config` or `--config ` flags. + +If a config file is not provided and no file is found in the current working directory, Taplo CLI will try to retrieve the configuration file path from the `TAPLO_CONFIG` environment variable. From 3c8278c790992ff253afcbb62dd8cccbe66d9eae Mon Sep 17 00:00:00 2001 From: Razvan Azamfirei Date: Sun, 16 Jul 2023 06:57:13 -0400 Subject: [PATCH 2/3] taplo-cli: apply code review suggestions Co-authored-by: Julien Cretin --- crates/taplo-cli/src/args.rs | 2 +- crates/taplo-cli/src/lib.rs | 10 +--------- site/site/cli/usage/configuration.md | 4 +--- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/crates/taplo-cli/src/args.rs b/crates/taplo-cli/src/args.rs index 647abc509..f9a29c82e 100644 --- a/crates/taplo-cli/src/args.rs +++ b/crates/taplo-cli/src/args.rs @@ -24,7 +24,7 @@ pub struct GeneralArgs { /// Path to the Taplo configuration file. /// /// Configuration file can be specified in $TAPLO_CONFIG - #[clap(long, short)] + #[clap(long, short, env = "TAPLO_CONFIG")] pub config: Option, /// Set a cache path. diff --git a/crates/taplo-cli/src/lib.rs b/crates/taplo-cli/src/lib.rs index f1fb6264e..2b08a67d5 100644 --- a/crates/taplo-cli/src/lib.rs +++ b/crates/taplo-cli/src/lib.rs @@ -2,7 +2,6 @@ use anyhow::{anyhow, Context}; use args::GeneralArgs; use itertools::Itertools; use std::{ - env, path::{Path, PathBuf}, sync::Arc, }; @@ -48,14 +47,7 @@ impl Taplo { if config_path.is_none() && !general.no_auto_config { if let Some(cwd) = self.env.cwd_normalized() { - config_path = self.env.find_config_file_normalized(&cwd).await; - } - } - - if config_path.is_none() { - tracing::debug!("Looking for config path in the environment variable"); - if let Ok(path) = env::var("TAPLO_CONFIG") { - config_path = Some(path.into()); + config_path = self.env.find_config_file_normalized(&cwd).await } } diff --git a/site/site/cli/usage/configuration.md b/site/site/cli/usage/configuration.md index 77235488f..02e5b51d4 100644 --- a/site/site/cli/usage/configuration.md +++ b/site/site/cli/usage/configuration.md @@ -23,6 +23,4 @@ The available log levels: -Taplo CLI by default searches for a Taplo config file in the current working directory, this behaviour can be disabled by either supplying `--no-auto-config` or `--config ` flags. - -If a config file is not provided and no file is found in the current working directory, Taplo CLI will try to retrieve the configuration file path from the `TAPLO_CONFIG` environment variable. +Taplo CLI, by default, searches the current working directory for a Taplo configuration file. This behaviour can be disabled by either supplying `--no-auto-config` or `--config ` flags. The `TAPLO_CONFIG` environmental variable can also be used to set the configuration, but the `--config` flag will take precedence. From 442f7b72a9345cd5cab7b36fcb7f9d2d88ccd274 Mon Sep 17 00:00:00 2001 From: Razvan Azamfirei Date: Sun, 16 Jul 2023 09:01:04 -0400 Subject: [PATCH 3/3] taplo-cli: update crates and clap description --- crates/taplo-cli/Cargo.toml | 2 +- crates/taplo-cli/src/args.rs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/taplo-cli/Cargo.toml b/crates/taplo-cli/Cargo.toml index 93c202ab5..149c7ebaf 100644 --- a/crates/taplo-cli/Cargo.toml +++ b/crates/taplo-cli/Cargo.toml @@ -20,7 +20,7 @@ toml-test = [] [dependencies] anyhow = { version = "1", features = ["backtrace"] } async-ctrlc = { version = "1.2.0", features = ["stream"], optional = true } -clap = { version = "3.0.0", features = ["derive", "cargo"] } +clap = { version = "3.0.0", features = ["derive", "cargo", "env"] } codespan-reporting = "0.11.1" futures = "0.3" glob = "0.3" diff --git a/crates/taplo-cli/src/args.rs b/crates/taplo-cli/src/args.rs index f9a29c82e..d13bb171f 100644 --- a/crates/taplo-cli/src/args.rs +++ b/crates/taplo-cli/src/args.rs @@ -22,8 +22,6 @@ pub struct TaploArgs { #[derive(Clone, Args)] pub struct GeneralArgs { /// Path to the Taplo configuration file. - /// - /// Configuration file can be specified in $TAPLO_CONFIG #[clap(long, short, env = "TAPLO_CONFIG")] pub config: Option,