diff --git a/crates/pgt_cli/src/commands/clean.rs b/crates/pgt_cli/src/commands/clean.rs index 90bfb915c..7f69fda64 100644 --- a/crates/pgt_cli/src/commands/clean.rs +++ b/crates/pgt_cli/src/commands/clean.rs @@ -1,12 +1,12 @@ use crate::commands::daemon::default_pgt_log_path; use crate::{CliDiagnostic, CliSession}; -use pgt_env::pgt_env; +use pgt_env::pgls_env; use std::fs::{create_dir, remove_dir_all}; use std::path::PathBuf; /// Runs the clean command pub fn clean(_cli_session: CliSession) -> Result<(), CliDiagnostic> { - let logs_path = pgt_env() + let logs_path = pgls_env() .pgt_log_path .value() .map_or(default_pgt_log_path(), PathBuf::from); diff --git a/crates/pgt_cli/src/commands/daemon.rs b/crates/pgt_cli/src/commands/daemon.rs index 988286f46..f64e5b25d 100644 --- a/crates/pgt_cli/src/commands/daemon.rs +++ b/crates/pgt_cli/src/commands/daemon.rs @@ -5,7 +5,7 @@ use crate::{ use pgt_console::{ConsoleExt, markup}; use pgt_lsp::ServerFactory; use pgt_workspace::{TransportError, WorkspaceError, workspace::WorkspaceClient}; -use std::{env, path::PathBuf}; +use std::path::PathBuf; use tokio::io; use tokio::runtime::Runtime; use tracing::subscriber::Interest; @@ -234,7 +234,12 @@ fn setup_tracing_subscriber( } pub fn default_pgt_log_path() -> PathBuf { - match env::var_os("PGT_LOG_PATH") { + let env = pgt_env::pgls_env(); + match env + .pgls_log_path + .value() + .or_else(|| env.pgt_log_path.value()) + { Some(directory) => PathBuf::from(directory), None => pgt_fs::ensure_cache_dir().join("pgt-logs"), } diff --git a/crates/pgt_cli/src/commands/mod.rs b/crates/pgt_cli/src/commands/mod.rs index 1457cf77e..d66b27ba8 100644 --- a/crates/pgt_cli/src/commands/mod.rs +++ b/crates/pgt_cli/src/commands/mod.rs @@ -73,6 +73,7 @@ pub enum PgtCommand { /// Allows to change the prefix applied to the file name of the logs. #[bpaf( env("PGT_LOG_PREFIX_NAME"), + env("PGLS_LOG_PREFIX_NAME"), long("log-prefix-name"), argument("STRING"), hide_usage, @@ -84,6 +85,7 @@ pub enum PgtCommand { /// Allows to change the folder where logs are stored. #[bpaf( env("PGT_LOG_PATH"), + env("PGLS_LOG_PATH"), long("log-path"), argument("PATH"), hide_usage, @@ -115,6 +117,7 @@ pub enum PgtCommand { /// Allows to change the prefix applied to the file name of the logs. #[bpaf( env("PGT_LOG_PREFIX_NAME"), + env("PGLS_LOG_PREFIX_NAME"), long("log-prefix-name"), argument("STRING"), hide_usage, @@ -125,6 +128,7 @@ pub enum PgtCommand { /// Allows to change the folder where logs are stored. #[bpaf( env("PGT_LOG_PATH"), + env("PGLS_LOG_PATH"), long("log-path"), argument("PATH"), hide_usage, @@ -154,6 +158,7 @@ pub enum PgtCommand { /// Allows to change the prefix applied to the file name of the logs. #[bpaf( env("PGT_LOG_PREFIX_NAME"), + env("PGLS_LOG_PREFIX_NAME"), long("log-prefix-name"), argument("STRING"), hide_usage, @@ -165,6 +170,7 @@ pub enum PgtCommand { /// Allows to change the folder where logs are stored. #[bpaf( env("PGT_LOG_PATH"), + env("PGLS_LOG_PATH"), long("log-path"), argument("PATH"), hide_usage, @@ -175,6 +181,7 @@ pub enum PgtCommand { /// Allows to change the log level. Default is debug. This will only affect "pgt*" crates. All others are logged with info level. #[bpaf( env("PGT_LOG_LEVEL"), + env("PGLS_LOG_LEVEL"), long("log-level"), argument("trace|debug|info|warn|error|none"), fallback(String::from("debug")) @@ -184,6 +191,7 @@ pub enum PgtCommand { /// Allows to change the logging format kind. Default is hierarchical. #[bpaf( env("PGT_LOG_KIND"), + env("PGLS_LOG_KIND"), long("log-kind"), argument("hierarchical|bunyan"), fallback(String::from("hierarchical")) diff --git a/crates/pgt_configuration/src/lib.rs b/crates/pgt_configuration/src/lib.rs index c5e8b9f1a..4dde88a37 100644 --- a/crates/pgt_configuration/src/lib.rs +++ b/crates/pgt_configuration/src/lib.rs @@ -33,7 +33,7 @@ use files::{FilesConfiguration, PartialFilesConfiguration, partial_files_configu use migrations::{ MigrationsConfiguration, PartialMigrationsConfiguration, partial_migrations_configuration, }; -use pgt_env::PGT_WEBSITE; +use pgt_env::PGLS_WEBSITE; use plpgsql_check::{ PartialPlPgSqlCheckConfiguration, PlPgSqlCheckConfiguration, partial_pl_pg_sql_check_configuration, @@ -103,7 +103,7 @@ impl PartialConfiguration { /// Returns the initial configuration. pub fn init() -> Self { Self { - schema: Some(format!("{}/schemas/{VERSION}/schema.json", PGT_WEBSITE)), + schema: Some(format!("{}/schemas/{VERSION}/schema.json", PGLS_WEBSITE)), extends: Some(StringSet::default()), files: Some(PartialFilesConfiguration { ignore: Some(Default::default()), diff --git a/crates/pgt_env/src/lib.rs b/crates/pgt_env/src/lib.rs index 918a6d1cd..3b2f50fcf 100644 --- a/crates/pgt_env/src/lib.rs +++ b/crates/pgt_env/src/lib.rs @@ -1,4 +1,4 @@ -//! Environment variables and configuration constants for Postgres Tools. +//! Environment variables and configuration constants for Postgres Language Server. //! //! This module provides: //! - Environment variable definitions for runtime configuration @@ -10,13 +10,14 @@ use pgt_console::{DebugDisplay, KeyValuePair, markup}; use std::env; use std::sync::{LazyLock, OnceLock}; -/// Returns `true` if this is an unstable build of Postgres Tools +/// Returns `true` if this is an unstable build of Postgres Language Server pub fn is_unstable() -> bool { VERSION == "0.0.0" } -/// The internal version of Postgres Tools. This is usually supplied during the CI build -pub static PGT_VERSION: LazyLock> = LazyLock::new(|| option_env!("PGT_VERSION")); +/// The internal version of Postgres Language Server. This is usually supplied during the CI build +pub static PGLS_VERSION: LazyLock> = + LazyLock::new(|| option_env!("PGLS_VERSION").or(option_env!("PGT_VERSION"))); /// The version of Postgres Tools with fallback logic pub const VERSION: &str = match option_env!("PGT_VERSION") { @@ -27,36 +28,64 @@ pub const VERSION: &str = match option_env!("PGT_VERSION") { }, }; -pub static PGT_WEBSITE: &str = "https://pgtools.dev"; +pub static PGLS_WEBSITE: &str = "https://pgtools.dev"; -pub struct PgTEnv { - pub pgt_log_path: PgTEnvVariable, - pub pgt_log_prefix: PgTEnvVariable, - pub pgt_config_path: PgTEnvVariable, +pub struct PgLSEnv { + pub pgls_log_path: PgLSEnvVariable, + pub pgls_log_level: PgLSEnvVariable, + pub pgls_log_prefix: PgLSEnvVariable, + pub pgls_config_path: PgLSEnvVariable, + + // DEPRECATED + pub pgt_log_path: PgLSEnvVariable, + pub pgt_log_level: PgLSEnvVariable, + pub pgt_log_prefix: PgLSEnvVariable, + pub pgt_config_path: PgLSEnvVariable, } -pub static PGT_ENV: OnceLock = OnceLock::new(); +pub static PGT_ENV: OnceLock = OnceLock::new(); -impl PgTEnv { +impl PgLSEnv { fn new() -> Self { Self { - pgt_log_path: PgTEnvVariable::new( - "PGT_LOG_PATH", + pgls_log_path: PgLSEnvVariable::new( + "PGLS_LOG_PATH", "The directory where the Daemon logs will be saved.", ), - pgt_log_prefix: PgTEnvVariable::new( - "PGT_LOG_PREFIX_NAME", + pgls_log_level: PgLSEnvVariable::new( + "PGLS_LOG_LEVEL", + "Allows to change the log level. Default is debug. This will only affect \"pgt*\" crates. All others are logged with info level.", + ), + pgls_log_prefix: PgLSEnvVariable::new( + "PGLS_LOG_PREFIX_NAME", "A prefix that's added to the name of the log. Default: `server.log.`", ), - pgt_config_path: PgTEnvVariable::new( - "PGT_CONFIG_PATH", + pgls_config_path: PgLSEnvVariable::new( + "PGLS_CONFIG_PATH", "A path to the configuration file", ), + + pgt_log_path: PgLSEnvVariable::new( + "PGT_LOG_PATH", + "The directory where the Daemon logs will be saved. Deprecated, use PGLS_LOG_PATH instead.", + ), + pgt_log_level: PgLSEnvVariable::new( + "PGT_LOG_LEVEL", + "Allows to change the log level. Default is debug. This will only affect \"pgt*\" crates. All others are logged with info level. Deprecated, use PGLS_LOG_LEVEL instead.", + ), + pgt_log_prefix: PgLSEnvVariable::new( + "PGT_LOG_PREFIX_NAME", + "A prefix that's added to the name of the log. Default: `server.log`. Deprecated, use PGLS_LOG_PREFIX_NAME instead.", + ), + pgt_config_path: PgLSEnvVariable::new( + "PGT_CONFIG_PATH", + "A path to the configuration file. Deprecated, use PGLS_CONFIG_PATH instead.", + ), } } } -pub struct PgTEnvVariable { +pub struct PgLSEnvVariable { /// The name of the environment variable name: &'static str, /// The description of the variable. @@ -64,7 +93,7 @@ pub struct PgTEnvVariable { description: &'static str, } -impl PgTEnvVariable { +impl PgLSEnvVariable { fn new(name: &'static str, description: &'static str) -> Self { Self { name, description } } @@ -85,12 +114,49 @@ impl PgTEnvVariable { } } -pub fn pgt_env() -> &'static PgTEnv { - PGT_ENV.get_or_init(PgTEnv::new) +pub fn pgls_env() -> &'static PgLSEnv { + PGT_ENV.get_or_init(PgLSEnv::new) } -impl Display for PgTEnv { +impl Display for PgLSEnv { fn fmt(&self, fmt: &mut Formatter) -> std::io::Result<()> { + match self.pgls_log_path.value() { + None => { + KeyValuePair(self.pgls_log_path.name, markup! { "unset" }).fmt(fmt)?; + } + Some(value) => { + KeyValuePair(self.pgls_log_path.name, markup! {{DebugDisplay(value)}}).fmt(fmt)?; + } + }; + match self.pgls_log_level.value() { + None => { + KeyValuePair(self.pgls_log_level.name, markup! { "unset" }).fmt(fmt)?; + } + Some(value) => { + KeyValuePair(self.pgls_log_level.name, markup! {{DebugDisplay(value)}}).fmt(fmt)?; + } + }; + match self.pgls_log_prefix.value() { + None => { + KeyValuePair(self.pgls_log_prefix.name, markup! { "unset" }).fmt(fmt)?; + } + Some(value) => { + KeyValuePair(self.pgls_log_prefix.name, markup! {{DebugDisplay(value)}}) + .fmt(fmt)?; + } + }; + + match self.pgls_config_path.value() { + None => { + KeyValuePair(self.pgls_config_path.name, markup! { "unset" }) + .fmt(fmt)?; + } + Some(value) => { + KeyValuePair(self.pgls_config_path.name, markup! {{DebugDisplay(value)}}) + .fmt(fmt)?; + } + }; + match self.pgt_log_path.value() { None => { KeyValuePair(self.pgt_log_path.name, markup! { "unset" }).fmt(fmt)?; @@ -99,6 +165,14 @@ impl Display for PgTEnv { KeyValuePair(self.pgt_log_path.name, markup! {{DebugDisplay(value)}}).fmt(fmt)?; } }; + match self.pgt_log_level.value() { + None => { + KeyValuePair(self.pgt_log_level.name, markup! { "unset" }).fmt(fmt)?; + } + Some(value) => { + KeyValuePair(self.pgt_log_level.name, markup! {{DebugDisplay(value)}}).fmt(fmt)?; + } + }; match self.pgt_log_prefix.value() { None => { KeyValuePair(self.pgt_log_prefix.name, markup! { "unset" }).fmt(fmt)?; diff --git a/crates/pgt_workspace/src/configuration.rs b/crates/pgt_workspace/src/configuration.rs index 6bd11078c..8b5b4936a 100644 --- a/crates/pgt_workspace/src/configuration.rs +++ b/crates/pgt_workspace/src/configuration.rs @@ -12,7 +12,7 @@ use pgt_configuration::{ VERSION, diagnostics::CantLoadExtendFile, push_to_analyser_rules, }; use pgt_console::markup; -use pgt_env::PGT_WEBSITE; +use pgt_env::PGLS_WEBSITE; use pgt_fs::{AutoSearchResult, ConfigName, FileSystem, OpenOptions}; use crate::{DynRef, WorkspaceError, settings::Settings}; @@ -200,9 +200,9 @@ pub fn create_config( configuration.schema = postgrestools_node_schema_path.to_str().map(String::from); } else if VERSION == "0.0.0" { // VERSION is 0.0.0 if it has not been explicitly set (e.g local dev, as fallback) - configuration.schema = Some(format!("{}/latest/schema.json", PGT_WEBSITE)); + configuration.schema = Some(format!("{}/latest/schema.json", PGLS_WEBSITE)); } else { - configuration.schema = Some(format!("{}/{VERSION}/schema.json", PGT_WEBSITE)); + configuration.schema = Some(format!("{}/{VERSION}/schema.json", PGLS_WEBSITE)); } let contents = serde_json::to_string_pretty(&configuration) diff --git a/docs/codegen/src/env_variables.rs b/docs/codegen/src/env_variables.rs index 4ff5b412c..0ae77da64 100644 --- a/docs/codegen/src/env_variables.rs +++ b/docs/codegen/src/env_variables.rs @@ -10,16 +10,46 @@ pub fn generate_env_variables(docs_dir: &Path) -> Result<()> { let mut content = vec![]; - let env = pgt_env::pgt_env(); + let env = pgt_env::pgls_env(); writeln!(content, "\n",)?; + writeln!( + content, + "### `{}`\n\n {}\n", + env.pgls_log_path.name(), + env.pgls_log_path.description() + )?; + writeln!( + content, + "### `{}`\n\n {}\n", + env.pgls_log_level.name(), + env.pgls_log_level.description() + )?; + writeln!( + content, + "### `{}`\n\n {}\n", + env.pgls_log_prefix.name(), + env.pgls_log_prefix.description() + )?; + writeln!( + content, + "### `{}`\n\n {}\n", + env.pgls_config_path.name(), + env.pgls_config_path.description() + )?; writeln!( content, "### `{}`\n\n {}\n", env.pgt_log_path.name(), env.pgt_log_path.description() )?; + writeln!( + content, + "### `{}`\n\n {}\n", + env.pgt_log_level.name(), + env.pgt_log_level.description() + )?; writeln!( content, "### `{}`\n\n {}\n", diff --git a/docs/reference/env_variables.md b/docs/reference/env_variables.md index c8f7097a2..b9440f560 100644 --- a/docs/reference/env_variables.md +++ b/docs/reference/env_variables.md @@ -3,17 +3,37 @@ [//]: # (BEGIN ENV_VARS) -### `PGT_LOG_PATH` +### `PGLS_LOG_PATH` The directory where the Daemon logs will be saved. -### `PGT_LOG_PREFIX_NAME` +### `PGLS_LOG_LEVEL` + + Allows to change the log level. Default is debug. This will only affect "pgt*" crates. All others are logged with info level. + +### `PGLS_LOG_PREFIX_NAME` A prefix that's added to the name of the log. Default: `server.log.` -### `PGT_CONFIG_PATH` +### `PGLS_CONFIG_PATH` A path to the configuration file +### `PGT_LOG_PATH` + + The directory where the Daemon logs will be saved. Deprecated, use PGLS_LOG_PATH instead. + +### `PGT_LOG_LEVEL` + + Allows to change the log level. Default is debug. This will only affect "pgt*" crates. All others are logged with info level. Deprecated, use PGLS_LOG_LEVEL instead. + +### `PGT_LOG_PREFIX_NAME` + + A prefix that's added to the name of the log. Default: `server.log`. Deprecated, use PGLS_LOG_PREFIX_NAME instead. + +### `PGT_CONFIG_PATH` + + A path to the configuration file. Deprecated, use PGLS_CONFIG_PATH instead. + [//]: # (END ENV_VARS) diff --git a/xtask/codegen/src/generate_new_analyser_rule.rs b/xtask/codegen/src/generate_new_analyser_rule.rs index 3ad01129d..5730e96f2 100644 --- a/xtask/codegen/src/generate_new_analyser_rule.rs +++ b/xtask/codegen/src/generate_new_analyser_rule.rs @@ -1,7 +1,7 @@ use biome_string_case::Case; use bpaf::Bpaf; use pgt_diagnostics::Severity; -use pgt_env::PGT_WEBSITE; +use pgt_env::PGLS_WEBSITE; use std::str::FromStr; use xtask::project_root; @@ -130,7 +130,7 @@ pub fn generate_new_analyser_rule( let rule_line = match category { Category::Lint => format!( r#" "lint/{group}/{rule_name_camel}": "{}/latest/rules/{kebab_case_rule}","#, - PGT_WEBSITE + PGLS_WEBSITE ), }; let lint_start = match category {