add support of comments

Closes #5
This commit is contained in:
Dmitriy Pleshevskiy 2022-08-01 23:54:37 +03:00
parent c6a7217ebd
commit 8f786cfd36
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
7 changed files with 33 additions and 1 deletions

View File

@ -68,7 +68,9 @@ where
line.to_string()
} else if let Some(cur_sections) = current_sections.clone() {
let trimmed_line = line.trim_start_matches(['#', ' ']);
if should_enable_variable(&choose_sections, &cur_sections) {
if !is_variables(trimmed_line) {
line.to_string()
} else if should_enable_variable(&choose_sections, &cur_sections) {
String::from(trimmed_line)
} else if should_disable_variable(&choose_sections, &cur_sections) {
format!("# {}", trimmed_line)
@ -87,6 +89,15 @@ where
writer.flush().map_err(|_| Error::WriteData)
}
fn is_variables(trimmed_line: &str) -> bool {
trimmed_line
.chars()
.filter(|ch| (*ch != '_' && ch.is_ascii_punctuation()) || ch.is_whitespace())
.enumerate()
.find(|(_, ch)| *ch == '=')
.map_or(false, |(i, _)| i == 0)
}
fn is_section_end(line: &str) -> bool {
line.trim().is_empty()
}
@ -274,5 +285,20 @@ mod tests {
&[Section::with_namespace("debug", "off")]
));
}
#[test]
fn should_check_line_on_variable() {
let test_cases = [
("VAR=10", true),
("THIS_IS_MY_VAR='hello world'", true),
("hello world", false),
("staging/production", false),
("staging/production=value", false),
];
for (input, expected) in test_cases {
assert_eq!(is_variables(input), expected);
}
}
}
}

View File

@ -2,6 +2,7 @@ VITE_STRIPE_PK=
VITE_SENTRY_ENABLED=
VITE_SENTRY_DSN=
### local,staging
# staging/production
VITE_SENTRY_ENV=staging
### prod
VITE_SENTRY_ENV=production

View File

@ -2,6 +2,7 @@ VITE_STRIPE_PK=
VITE_SENTRY_ENABLED=
VITE_SENTRY_DSN=
### local,staging
# staging/production
# VITE_SENTRY_ENV=staging
### prod
# VITE_SENTRY_ENV=production

View File

@ -2,6 +2,7 @@ VITE_STRIPE_PK=
VITE_SENTRY_ENABLED=
VITE_SENTRY_DSN=
### local,staging
# staging/production
VITE_SENTRY_ENV=staging
### prod
# VITE_SENTRY_ENV=production

View File

@ -2,6 +2,7 @@ VITE_STRIPE_PK=
VITE_SENTRY_ENABLED=
VITE_SENTRY_DSN=
### local,staging
# staging/production
VITE_SENTRY_ENV=staging
### prod
# VITE_SENTRY_ENV=production

View File

@ -2,6 +2,7 @@ VITE_STRIPE_PK=
VITE_SENTRY_ENABLED=
VITE_SENTRY_DSN=
### local,staging
# staging/production
VITE_SENTRY_ENV=staging
### prod
# VITE_SENTRY_ENV=production

View File

@ -2,6 +2,7 @@ VITE_STRIPE_PK=
VITE_SENTRY_ENABLED=
VITE_SENTRY_DSN=
### local,staging
# staging/production
VITE_SENTRY_ENV=staging
### prod
# VITE_SENTRY_ENV=production