fix(encode64): support BSD base64 and quote arguments (#14269)

* encode64: support BSD base64 and quote arguments

* chore: remove pr-body.md and pr-title.txt from branch

---------

Co-authored-by: pei711 <pei711@users.noreply.github.com>
Co-authored-by: pei711 <3922088406@qq.com>
This commit is contained in:
authored and GitHub committed 2026-11-24 11:28:08 -08:00
1 parent 109270911c
commit 27506acc8a
3 files changed
+101 -11

No files matched your search

+21 -8
View File
@@ -2,24 +2,37 @@ encode64() {
if [[ $# -eq 0 ]]; then
cat | base64
else
printf '%s' $1 | base64
printf '%s' "$1" | base64
fi
}
encodefile64() {
if [[ $# -eq 0 ]]; then
echo "You must provide a filename"
else
base64 $1 > $1.txt
echo "${1}'s content encoded in base64 and saved as ${1}.txt"
if [[ $# -ne 1 || ! -f "$1" || ! -r "$1" ]]; then
print -u2 -- 'Usage: encodefile64 <file>'
return 1
fi
local file="$1" output="${1}.txt" tmp_file
[[ $output == /* ]] || output="./$output"
tmp_file=$(mktemp "${output}.XXXXXX") || return 1
if ! base64 < "$file" > "$tmp_file"; then
command rm -f "$tmp_file"
return 1
fi
if ! command mv "$tmp_file" "$output"; then
command rm -f "$tmp_file"
return 1
fi
print -- "$file's content encoded in base64 and saved as ${file}.txt"
}
decode64() {
if [[ $# -eq 0 ]]; then
cat | base64 --decode
cat | base64 -d
else
printf '%s' $1 | base64 --decode
printf '%s' "$1" | base64 -d
fi
}
alias e64=encode64