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() line.to_string()
} else if let Some(cur_sections) = current_sections.clone() { } else if let Some(cur_sections) = current_sections.clone() {
let trimmed_line = line.trim_start_matches(['#', ' ']); 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) String::from(trimmed_line)
} else if should_disable_variable(&choose_sections, &cur_sections) { } else if should_disable_variable(&choose_sections, &cur_sections) {
format!("# {}", trimmed_line) format!("# {}", trimmed_line)
@ -87,6 +89,15 @@ where
writer.flush().map_err(|_| Error::WriteData) 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 { fn is_section_end(line: &str) -> bool {
line.trim().is_empty() line.trim().is_empty()
} }
@ -274,5 +285,20 @@ mod tests {
&[Section::with_namespace("debug", "off")] &[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_ENABLED=
VITE_SENTRY_DSN= VITE_SENTRY_DSN=
### local,staging ### local,staging
# staging/production
VITE_SENTRY_ENV=staging VITE_SENTRY_ENV=staging
### prod ### prod
VITE_SENTRY_ENV=production VITE_SENTRY_ENV=production

View file

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

View file

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

View file

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

View file

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

View file

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