From 2cb403e89f618548b340181b5690639768305aad Mon Sep 17 00:00:00 2001 From: SpeakinTelnet <105018871+SpeakinTelnet@users.noreply.github.com> Date: Fri, 2 Aug 2024 10:37:00 -0400 Subject: feat(toolbox): add function to show toolbox name (#12594) Co-authored-by: Carlo Sala --- plugins/toolbox/README.md | 13 ++++++++----- plugins/toolbox/toolbox.plugin.zsh | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'plugins/toolbox') diff --git a/plugins/toolbox/README.md b/plugins/toolbox/README.md index bc04a906b..d957d9bc4 100644 --- a/plugins/toolbox/README.md +++ b/plugins/toolbox/README.md @@ -10,7 +10,8 @@ plugins=(... toolbox) ## Prompt function -This plugins adds `toolbox_prompt_info()` function. Using it in your prompt, it will show the toolbox indicator ⬢ (if you are running in a toolbox container), and nothing if not. +This plugins adds `toolbox_prompt_info()` function. Using it in your prompt, it will show the toolbox +indicator ⬢ (if you are running in a toolbox container), and nothing if not. You can use it by adding `$(toolbox_prompt_info)` to your `PROMPT` or `RPROMPT` variable: @@ -18,9 +19,11 @@ You can use it by adding `$(toolbox_prompt_info)` to your `PROMPT` or `RPROMPT` RPROMPT='$(toolbox_prompt_info)' ``` +In the same way, it adds `toolbox_prompt_name()`, showing the name of the containerized environment. + ## Aliases -| Alias | Command | Description | -|-------|----------------------|----------------------------------------| -| tbe | `toolbox enter` | Enters the toolbox environment | -| tbr | `toolbox run` | Run a command in an existing toolbox | +| Alias | Command | Description | +| ----- | --------------- | ------------------------------------ | +| tbe | `toolbox enter` | Enters the toolbox environment | +| tbr | `toolbox run` | Run a command in an existing toolbox | diff --git a/plugins/toolbox/toolbox.plugin.zsh b/plugins/toolbox/toolbox.plugin.zsh index 377e498cd..031c0f754 100644 --- a/plugins/toolbox/toolbox.plugin.zsh +++ b/plugins/toolbox/toolbox.plugin.zsh @@ -2,5 +2,9 @@ function toolbox_prompt_info() { [[ -f /run/.toolboxenv ]] && echo "⬢" } +function toolbox_prompt_name() { + [[ -f /run/.containerenv ]] && cat /run/.containerenv | awk -F\" '/name/ { print$2 }' +} + alias tbe="toolbox enter" alias tbr="toolbox run" -- cgit v1.2.3-70-g09d2 From 3b5f018f7025a347e4888408b566caeaef2fabd7 Mon Sep 17 00:00:00 2001 From: Carlo Sala Date: Fri, 2 Aug 2024 17:00:55 +0200 Subject: fix(toolbox): avoid prompt injection --- plugins/toolbox/toolbox.plugin.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'plugins/toolbox') diff --git a/plugins/toolbox/toolbox.plugin.zsh b/plugins/toolbox/toolbox.plugin.zsh index 031c0f754..b9a594c09 100644 --- a/plugins/toolbox/toolbox.plugin.zsh +++ b/plugins/toolbox/toolbox.plugin.zsh @@ -3,7 +3,9 @@ function toolbox_prompt_info() { } function toolbox_prompt_name() { - [[ -f /run/.containerenv ]] && cat /run/.containerenv | awk -F\" '/name/ { print$2 }' + [[ -f /run/.containerenv ]] || return + local _to_print="$(cat /run/.containerenv | awk -F\" '/name/ { print$2 }')" + echo ${_to_print:gs/%/%%} } alias tbe="toolbox enter" -- cgit v1.2.3-70-g09d2 From 0ffcc3b68a1196e9164ff42fac8a5fdd4804abd0 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Sun, 4 Aug 2024 18:59:42 +0200 Subject: style(toolbox): use oneliner to extract and quote container name --- plugins/toolbox/toolbox.plugin.zsh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'plugins/toolbox') diff --git a/plugins/toolbox/toolbox.plugin.zsh b/plugins/toolbox/toolbox.plugin.zsh index b9a594c09..efe3836f7 100644 --- a/plugins/toolbox/toolbox.plugin.zsh +++ b/plugins/toolbox/toolbox.plugin.zsh @@ -4,8 +4,12 @@ function toolbox_prompt_info() { function toolbox_prompt_name() { [[ -f /run/.containerenv ]] || return - local _to_print="$(cat /run/.containerenv | awk -F\" '/name/ { print$2 }')" - echo ${_to_print:gs/%/%%} + + # This command reads the /run/.containerenv file line by line and extracts the + # container name from it by looking for the `name="..."` line, and uses -F\" to + # split the line by double quotes. Then all % characters are replaced with %% + # to escape them for the prompt. + awk -F\" '/name/ { gsub(/%/, "%%", $2); print $2 }' /run/.containerenv } alias tbe="toolbox enter" -- cgit v1.2.3-70-g09d2