generated from Chemaclass/bash-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Purpose-Green/feat/ask-extra-confirmation
Ask extra confirmation
- Loading branch information
Showing
5 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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,38 @@ | ||
#!/bin/bash | ||
# shellcheck disable=SC2155 | ||
set -euo pipefail | ||
|
||
# Render the value str where the key is matching on the given json | ||
# | ||
# $1 full json as str | ||
# $2 the filepath | ||
# | ||
# Example: '{"src/dev": "are you sure you want to change the files in this dir?"}' | ||
function json::parse_text() { | ||
local json_str="$1" | ||
local filepath="$2" | ||
|
||
# Start with the full path and progressively reduce it to parent directories | ||
local dir_path="$filepath" | ||
|
||
while [[ -n "$dir_path" ]]; do | ||
# Escape special characters in the directory path for `sed` | ||
local escaped_dir_path=$(echo "$dir_path" | sed 's/[\/&]/\\&/g') | ||
|
||
# Extract and return the message for the current directory path if it exists in the JSON string | ||
local text=$(echo "$json_str" | sed -n "s/.*\"$escaped_dir_path\":\"\([^\"]*\)\".*/\1/p") | ||
|
||
if [[ -n "$text" ]]; then | ||
echo "$text" | ||
return | ||
fi | ||
|
||
# Remove the last directory level | ||
dir_path=$(dirname "$dir_path") | ||
|
||
# Break if we reach the root | ||
if [[ "$dir_path" == "." ]]; then | ||
break | ||
fi | ||
done | ||
} |
This file contains 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