Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/pgt_cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
9 changes: 7 additions & 2 deletions crates/pgt_cli/src/commands/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
}
Expand Down
29 changes: 26 additions & 3 deletions crates/pgt_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -92,7 +94,12 @@ pub enum PgtCommand {
log_path: PathBuf,
/// Allows to set a custom file path to the configuration file,
/// or a custom directory path to find `postgrestools.jsonc`
#[bpaf(env("PGT_LOG_PREFIX_NAME"), long("config-path"), argument("PATH"))]
#[bpaf(
env("PGT_LOG_PREFIX_NAME"),
env("PGLS_LOG_PREFIX_NAME"),
long("config-path"),
argument("PATH")
)]
config_path: Option<PathBuf>,
},

Expand All @@ -110,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,
Expand All @@ -120,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,
Expand All @@ -128,7 +137,12 @@ pub enum PgtCommand {
log_path: PathBuf,
/// Allows to set a custom file path to the configuration file,
/// or a custom directory path to find `postgrestools.jsonc`
#[bpaf(env("PGT_CONFIG_PATH"), long("config-path"), argument("PATH"))]
#[bpaf(
env("PGT_CONFIG_PATH"),
env("PGLS_CONFIG_PATH"),
long("config-path"),
argument("PATH")
)]
config_path: Option<PathBuf>,
/// Bogus argument to make the command work with vscode-languageclient
#[bpaf(long("stdio"), hide, hide_usage, switch)]
Expand All @@ -144,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,
Expand All @@ -155,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,
Expand All @@ -165,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"))
Expand All @@ -174,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"))
Expand All @@ -184,7 +202,12 @@ pub enum PgtCommand {
stop_on_disconnect: bool,
/// Allows to set a custom file path to the configuration file,
/// or a custom directory path to find `postgrestools.jsonc`
#[bpaf(env("PGT_CONFIG_PATH"), long("config-path"), argument("PATH"))]
#[bpaf(
env("PGT_CONFIG_PATH"),
env("PGLS_CONFIG_PATH"),
long("config-path"),
argument("PATH")
)]
config_path: Option<PathBuf>,
},
#[bpaf(command("__print_socket"), hide)]
Expand Down
4 changes: 2 additions & 2 deletions crates/pgt_configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
Expand Down
97 changes: 75 additions & 22 deletions crates/pgt_env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Option<&str>> = 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<Option<&str>> =
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") {
Expand All @@ -27,44 +28,67 @@ 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_prefix: PgLSEnvVariable,
pub pgt_config_path: PgLSEnvVariable,
}

pub static PGT_ENV: OnceLock<PgTEnv> = OnceLock::new();
pub static PGT_ENV: OnceLock<PgLSEnv> = 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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the pgt log level is missing, was that intended? not sure if anybody except for me uses it 😆

"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_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.
// This field will be used in the website to automate its generation
description: &'static str,
}

impl PgTEnvVariable {
impl PgLSEnvVariable {
fn new(name: &'static str, description: &'static str) -> Self {
Self { name, description }
}
Expand All @@ -85,12 +109,41 @@ 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! { <Dim>"unset"</Dim> }).fmt(fmt)?;
}
Some(value) => {
KeyValuePair(self.pgls_log_path.name, markup! {{DebugDisplay(value)}}).fmt(fmt)?;
}
};
match self.pgls_log_prefix.value() {
None => {
KeyValuePair(self.pgls_log_prefix.name, markup! { <Dim>"unset"</Dim> }).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! { <Dim>"unset"</Dim> })
.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! { <Dim>"unset"</Dim> }).fmt(fmt)?;
Expand Down
6 changes: 3 additions & 3 deletions crates/pgt_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -187,9 +187,9 @@ pub fn create_config(
configuration.schema = 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)
Expand Down
20 changes: 19 additions & 1 deletion docs/codegen/src/env_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@ 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_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",
Expand Down
18 changes: 15 additions & 3 deletions docs/reference/env_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
[//]: # (BEGIN ENV_VARS)


### `PGT_LOG_PATH`
### `PGLS_LOG_PATH`

The directory where the Daemon logs will be saved.

### `PGT_LOG_PREFIX_NAME`
### `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_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)
Loading