-
-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Add env var placeholders support #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| use crate::configuration::config::ConfigError; | ||
| use std::borrow::Cow; | ||
|
|
||
| /// Expands environment placeholders in the given text. | ||
| /// | ||
| /// Replaces occurrences of `${ENV_VAR}` with the corresponding value from the | ||
| /// process environment (via `std::env`). If a referenced variable is not set, | ||
| /// returns `ConfigError::MissingEnvVar`. | ||
| /// | ||
| /// The sequence `\${` is treated as an escape and is converted to a literal `${` | ||
| /// without performing substitution. | ||
| /// | ||
| /// On malformed placeholders (e.g. missing closing }, empty/invalid variable | ||
| /// name, or a newline inside the placeholder), returns | ||
| /// `ConfigError::InvalidEnvPlaceholder { line, char }`, where line and char | ||
| /// are 1-based positions of the $ that started the placeholder. | ||
| /// | ||
| /// For efficiency, if the input contains no `${`, the function | ||
| /// returns a `borrowed Cow::Borrowed` without allocating | ||
| pub fn expand_env_placeholders<'a>(input: &'a str) -> Result<Cow<'a, str>, ConfigError> { | ||
| if !input.contains("${") { | ||
| return Ok(Cow::Borrowed(input)); | ||
| } | ||
|
|
||
| fn bump(ch: char, line: &mut usize, col: &mut usize) { | ||
| if ch == '\n' { | ||
| *line += 1; | ||
| *col = 1; | ||
| } else { | ||
| *col += 1; | ||
| } | ||
| } | ||
|
|
||
| fn is_valid_env_name(name: &str) -> bool { | ||
| let mut it = name.chars(); | ||
| let Some(first) = it.next() else { | ||
| return false; | ||
| }; | ||
| if !(first == '_' || first.is_ascii_alphabetic()) { | ||
| return false; | ||
| } | ||
| it.all(|c| c == '_' || c.is_ascii_alphanumeric()) | ||
| } | ||
|
|
||
| let mut out = String::with_capacity(input.len()); | ||
| let mut it = input.char_indices().peekable(); | ||
|
|
||
| let mut line: usize = 1; | ||
| let mut col: usize = 1; | ||
|
|
||
| while let Some((_i, ch)) = it.next() { | ||
| let start_line = line; | ||
| let start_col = col; | ||
|
|
||
| match ch { | ||
| '\\' => { | ||
| let mut look = it.clone(); | ||
| if matches!(look.next(), Some((_, '$'))) && matches!(look.next(), Some((_, '{'))) { | ||
| // съедаем '$' и '{' | ||
Quozul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let (_, d) = it.next().unwrap(); | ||
| let (_j, b) = it.next().unwrap(); | ||
|
|
||
| // учёт позиции | ||
| bump('\\', &mut line, &mut col); | ||
| bump(d, &mut line, &mut col); | ||
| bump(b, &mut line, &mut col); | ||
|
|
||
| out.push_str("${"); | ||
| continue; | ||
| } | ||
|
|
||
| bump('\\', &mut line, &mut col); | ||
| out.push('\\'); | ||
| } | ||
| '$' => { | ||
| if !matches!(it.peek(), Some((_, '{'))) { | ||
| bump('$', &mut line, &mut col); | ||
| out.push('$'); | ||
| continue; | ||
| } | ||
| let (brace_idx, brace) = it.next().unwrap(); | ||
|
|
||
| bump('$', &mut line, &mut col); | ||
| bump(brace, &mut line, &mut col); | ||
| let name_start = brace_idx + brace.len_utf8(); | ||
| let mut name_end: Option<usize> = None; | ||
|
|
||
| while let Some((k, c)) = it.next() { | ||
| match c { | ||
| '}' => { | ||
| name_end = Some(k); | ||
| bump('}', &mut line, &mut col); | ||
| break; | ||
| } | ||
| '\n' => { | ||
| return Err(ConfigError::InvalidEnvPlaceholder { | ||
| line: start_line, | ||
| char: start_col, | ||
| }); | ||
| } | ||
| _ => bump(c, &mut line, &mut col), | ||
| } | ||
| } | ||
|
|
||
| let name_end = name_end.ok_or(ConfigError::InvalidEnvPlaceholder { | ||
| line: start_line, | ||
| char: start_col, | ||
| })?; | ||
|
|
||
| let name = &input[name_start..name_end]; | ||
|
|
||
| if name.is_empty() || !is_valid_env_name(name) { | ||
| return Err(ConfigError::InvalidEnvPlaceholder { | ||
| line: start_line, | ||
| char: start_col, | ||
| }); | ||
| } | ||
|
|
||
| let Some(val) = std::env::var_os(name) else { | ||
| return Err(ConfigError::MissingEnvVar(name.to_string())); | ||
| }; | ||
|
|
||
| out.push_str(&val.to_string_lossy()); | ||
| } | ||
|
|
||
| _ => { | ||
| bump(ch, &mut line, &mut col); | ||
| out.push(ch); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(Cow::Owned(out)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Quozul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| bind = "0.0.0.0:25565" | ||
| welcome_message = "Welcome to PicoLimbo!" | ||
| action_bar = "Welcome to PicoLimbo!" | ||
| default_game_mode = "spectator" | ||
| hardcore = false | ||
| fetch_player_skins = false | ||
| reduced_debug_info = false | ||
| allow_unsupported_versions = false | ||
| allow_flight = false | ||
|
|
||
| [forwarding] | ||
| method = "NONE" | ||
| secret = "${PATH}" | ||
|
|
||
| [world] | ||
| spawn_position = [ | ||
| 0.0, | ||
| 320.0, | ||
| 0.0, | ||
| ] | ||
| spawn_rotation = [ | ||
| 0.0, | ||
| 0.0, | ||
| ] | ||
| dimension = "end" | ||
| time = "day" | ||
|
|
||
| [world.experimental] | ||
| view_distance = 2 | ||
| schematic_file = "" | ||
| lock_time = false | ||
|
|
||
| [world.boundaries] | ||
| enabled = true | ||
| min_y = -64 | ||
| teleport_message = "<red>You have reached the bottom of the world.</red>" | ||
|
|
||
| [server_list] | ||
| reply_to_status = true | ||
| max_players = 20 | ||
| message_of_the_day = "A Minecraft Server" | ||
| show_online_player_count = true | ||
| server_icon = "server-icon.png" | ||
|
|
||
| [compression] | ||
| threshold = -1 | ||
| level = 6 | ||
|
|
||
| [tab_list] | ||
| enabled = true | ||
| header = "<bold>Welcome to PicoLimbo</bold>" | ||
| footer = "<green>Enjoy your stay!</green>" | ||
| player_listed = true | ||
|
|
||
| [boss_bar] | ||
| enabled = false | ||
| title = "<bold>Welcome to PicoLimbo!</bold>" | ||
| health = 1.0 | ||
| color = "pink" | ||
| division = 0 | ||
|
|
||
| [title] | ||
| enabled = false | ||
| title = "<bold>Welcome!</bold>" | ||
| subtitle = "Enjoy your stay" | ||
| fade_in = 10 | ||
| stay = 70 | ||
| fade_out = 20 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.