wizard: gracefully handle non-writable ~/.zshrc

This commit is contained in:
romkatv 2020-02-16 10:30:40 +01:00
parent ffb6bf8fd7
commit 1a9f317650
2 changed files with 41 additions and 11 deletions

View File

@ -27,14 +27,6 @@ function _p9k_can_configure() {
$0_error "$__p9k_cfg_path_u is a special file" $0_error "$__p9k_cfg_path_u is a special file"
return 1 return 1
} }
[[ -r $__p9k_root_dir/config/p10k-lean.zsh ]] || {
$0_error "cannot read $__p9k_root_dir_u/config/p10k-lean.zsh"
return 1
}
[[ -r $__p9k_root_dir/config/p10k-classic.zsh ]] || {
$0_error "cannot read $__p9k_root_dir_u/config/p10k-classic.zsh"
return 1
}
[[ ! -e $__p9k_zshrc || -f $__p9k_zshrc || -h $__p9k_zshrc ]] || { [[ ! -e $__p9k_zshrc || -f $__p9k_zshrc || -h $__p9k_zshrc ]] || {
$0_error "$__p9k_zshrc_u a special file" $0_error "$__p9k_zshrc_u a special file"
return 1 return 1
@ -43,6 +35,13 @@ function _p9k_can_configure() {
$0_error "$__p9k_zshrc_u is not readable" $0_error "$__p9k_zshrc_u is not readable"
return 1 return 1
} }
local style
for style in lean lean-8colors classic rainbow pure; do
[[ -r $__p9k_root_dir/config/p10k-$style.zsh ]] || {
$0_error "$__p9k_root_dir_u/config/p10k-$style.zsh is not readable"
return 1
}
done
(( LINES >= __p9k_wizard_lines && COLUMNS >= __p9k_wizard_columns )) || { (( LINES >= __p9k_wizard_lines && COLUMNS >= __p9k_wizard_columns )) || {
$0_error "terminal size too small; must be at least $__p9k_wizard_columns x $__p9k_wizard_lines" $0_error "terminal size too small; must be at least $__p9k_wizard_columns x $__p9k_wizard_lines"

View File

@ -1491,7 +1491,25 @@ function ask_zshrc_edit() {
clear clear
flowing -c "%BApply changes to %b%2F${__p9k_zshrc_u//\\/\\\\}%f%B?%b" flowing -c "%BApply changes to %b%2F${__p9k_zshrc_u//\\/\\\\}%f%B?%b"
print -P "" print -P ""
print -P "%B(y) Yes (recommended).%b" local modifiable=y
if [[ -e $__p9k_zshrc && ! -w $__p9k_zshrc ]]; then
local -a stat
zstat -A stat +uid -- $__p9k_zshrc || quit -c
if (( stat[1] == EUID )); then
flowing -c %3FNOTE:%f %2F${__p9k_zshrc_u//\\/\\\\}%f %3Fis readonly.%f
else
modifiable=
flowing -c \
%3FWARNING:%f %2F${__p9k_zshrc_u//\\/\\\\}%f %3Fis readonly and \
not owned by the user. Cannot modify it.%f
fi
print -P ""
fi
if [[ $modifiable == y ]]; then
print -P "%B(y) Yes (recommended).%b"
else
print -P "%1F(y) Yes (disabled).%f"
fi
print -P "" print -P ""
print -P "%B(n) No. I know which changes to apply and will do it myself.%b" print -P "%B(n) No. I know which changes to apply and will do it myself.%b"
print -P "" print -P ""
@ -1500,18 +1518,24 @@ function ask_zshrc_edit() {
print -P "" print -P ""
local key= local key=
read -k key${(%):-"?%BChoice [ynrq]: %b"} || quit -c read -k key${(%):-"?%BChoice [${modifiable}nrq]: %b"} || quit -c
case $key in case $key in
q) quit;; q) quit;;
r) return 1;; r) return 1;;
n) return 0;; n) return 0;;
y) y)
[[ ! -e $__p9k_zshrc || -w $__p9k_zshrc ]] || (echo "Zshrc is readonly ignoring..."; return 1) [[ $modifiable == y ]] || continue
write_zshrc=1 write_zshrc=1
if [[ -n $zshrc_content ]]; then if [[ -n $zshrc_content ]]; then
zshrc_backup="$(mktemp ${TMPDIR:-/tmp}/.zshrc.XXXXXXXXXX)" || quit -c zshrc_backup="$(mktemp ${TMPDIR:-/tmp}/.zshrc.XXXXXXXXXX)" || quit -c
cp -p $__p9k_zshrc $zshrc_backup || quit -c cp -p $__p9k_zshrc $zshrc_backup || quit -c
local -i writable=1
if [[ ! -w $zshrc_backup ]]; then
chmod u+w -- $zshrc_backup || quit -c
writable=0
fi
print -r -- $zshrc_content >$zshrc_backup || quit -c print -r -- $zshrc_content >$zshrc_backup || quit -c
(( writable )) || chmod u-w -- $zshrc_backup || quit -c
zshrc_backup_u=${${TMPDIR:+\$TMPDIR}:-/tmp}/${(q-)zshrc_backup:t} zshrc_backup_u=${${TMPDIR:+\$TMPDIR}:-/tmp}/${(q-)zshrc_backup:t}
fi fi
break break
@ -1755,6 +1779,12 @@ function change_zshrc() {
[[ ! -e $__p9k_zshrc ]] || cp -p $__p9k_zshrc $tmp || return [[ ! -e $__p9k_zshrc ]] || cp -p $__p9k_zshrc $tmp || return
{ {
local -i writable=1
if [[ ! -w $tmp ]]; then
chmod u+w -- $tmp || return
writable=0
fi
print -n >$tmp || return print -n >$tmp || return
if (( !zshrc_has_instant_prompt )); then if (( !zshrc_has_instant_prompt )); then
@ -1774,6 +1804,7 @@ fi" || return
# To customize prompt, run \`p10k configure\` or edit ${(%)__p9k_cfg_path_u}. # To customize prompt, run \`p10k configure\` or edit ${(%)__p9k_cfg_path_u}.
[[ ! -f ${(%)__p9k_cfg_path_u} ]] || source ${(%)__p9k_cfg_path_u}" || return [[ ! -f ${(%)__p9k_cfg_path_u} ]] || source ${(%)__p9k_cfg_path_u}" || return
fi fi
(( writable )) || chmod u-w -- $tmp || return
zf_mv -f -- $tmp $__p9k_zshrc || return zf_mv -f -- $tmp $__p9k_zshrc || return
} always { } always {
zf_rm -f -- $tmp zf_rm -f -- $tmp