ohmyzsh
This commit is contained in:
82
zsh/.oh-my-zsh/custom/plugins/gcert/README.md
Executable file
82
zsh/.oh-my-zsh/custom/plugins/gcert/README.md
Executable file
@ -0,0 +1,82 @@
|
||||
# Prompt command to track GCert expiration
|
||||
|
||||
## Basics
|
||||
|
||||
This provides a function, useful in `$PROMPT`, for showing the time left until
|
||||
certificate expiration. It monitors your command history, grepping for when you
|
||||
run `gcert`, and updates the file `.gcert_expiration`[^directory] with the
|
||||
expiration time when you do. Then it displays the time remaining before
|
||||
certificate expiration when you run the command.
|
||||
|
||||
[^directory]: The cached expiration timestamp is put in `$ZSH_CACHE_DIR` if that
|
||||
variable is defined (it should be for Oh-My-Zsh). Otherwise if `$XDG_CACHE_HOME`
|
||||
is set and exists, it will use that. If neither variable is set but `$HOME/.cache`
|
||||
exists, it will use that; and as a fallback it will use `$HOME`.
|
||||
|
||||
### With Oh-My-Zsh
|
||||
|
||||
To use it, copy the `gcert` directory
|
||||
(`/google/data/ro/users/mw/mweigel/oh-my-zsh/plugins/gcert`) to your custom
|
||||
plugin directory (by default this would be `~/.oh-my-zsh/custom/plugins`) and
|
||||
add `gcert` to the plugins array in your zshrc file:
|
||||
|
||||
```zsh
|
||||
plugins=(... gcert)
|
||||
```
|
||||
|
||||
### Without Oh-My-Zsh
|
||||
|
||||
If you aren't using Oh-My-Zsh, copy the file `gcert.plugin.zsh`
|
||||
somewhere local on your workstation and source it from your zshrc
|
||||
file. For example if you copied it to `~/.gcert.plugin.zsh` then
|
||||
you might add
|
||||
|
||||
```zsh
|
||||
source ~/.gcert.plugin.zsh
|
||||
```
|
||||
|
||||
To your zshrc.
|
||||
|
||||
### With Powerlevel9k / Powerlevel10k
|
||||
|
||||
Follow the above OMZ/not-OMZ instructions, and add `gcert` to
|
||||
`POWERLEVEL9K_LEFT_PROMPT_ELEMENTS` or `POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS`.
|
||||
|
||||
## Customization
|
||||
|
||||
### For Normal PROMPT
|
||||
|
||||
To see the gcert expiration time in your prompt, add
|
||||
`$(gcert_prompt_time)` somewhere in `$PROMPT`.
|
||||
|
||||
To change the file used for caching expiration time, set
|
||||
`ZSH_THEME_GCERT_EXPIRY_FILE` after the script loads.
|
||||
|
||||
You can set the duration (in hours) at which you want to change the
|
||||
display of the duration by setting the variable
|
||||
`ZSH_THEME_GCERT_PROMPT_WARN_HOURS` (the default is 2, for 2 hours).
|
||||
|
||||
You can decorate the display with `ZSH_THEME_GCERT_PROMPT_PREFIX` and
|
||||
`ZSH_THEME_GCERT_PROMPT_POSTFIX` or, in the warning state, with
|
||||
`ZSH_THEME_GCERT_PROMPT_WARN_PREFIX` and
|
||||
`ZSH_THEME_GCERT_PROMPT_WARN_POSTFIX`.
|
||||
|
||||
You can disable checking your history by setting
|
||||
`ZSH_THEME_GCERT_PROMPT_PARANOID` to "true". If you do that, you can
|
||||
still trigger an update to the file by running `gcert_update_expiry`.
|
||||
|
||||
### State for Powerlevel9k/Powerlevel10k
|
||||
|
||||
The `gcert` prompt segment has EXPIRED, LOW, and NORMAL states. You can set
|
||||
`POWERLEVEL9K_GCERT_LOW_THRESHOLD` to the number of hours left that triggers the
|
||||
LOW state instead of NORMAL. You can set foreground/background colors as usual
|
||||
for a segment with states: `POWERLEVEL9K_GCERT_{STATE}_(FOREGROUND,BACKGROUND)`.
|
||||
|
||||
You can also customize the messages with `POWERLEVEL9K_GCERT_{STATE}_MESSAGE`.
|
||||
|
||||
## Changes
|
||||
|
||||
The code for this plugin resides in
|
||||
[google3/experimental/users/mweigel/oh-my-zsh/plugins/gcert/](http://google3/experimental/users/mweigel/oh-my-zsh/plugins/gcert/). CLs
|
||||
are gratefully reviewed, just add reviewer:
|
||||
[mweigel](http://who/mweigel)
|
95
zsh/.oh-my-zsh/custom/plugins/gcert/gcert.plugin.zsh
Executable file
95
zsh/.oh-my-zsh/custom/plugins/gcert/gcert.plugin.zsh
Executable file
@ -0,0 +1,95 @@
|
||||
# track and calculate lifetime of gcert certificate
|
||||
|
||||
_gcert_expiry_cache_path() {
|
||||
local directory
|
||||
directory=${ZSH_CACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}}
|
||||
if [[ ! -d "$directory" ]]; then
|
||||
directory="$HOME"
|
||||
fi
|
||||
echo "$directory/.gcert_expiration"
|
||||
}
|
||||
|
||||
if [[ -z "$EXPIRY_FILE" ]]; then
|
||||
EXPIRY_FILE=$(_gcert_expiry_cache_path)
|
||||
fi
|
||||
|
||||
_gcert_expiry_clear_old_cache() {
|
||||
# migrate to Epoch-seconds format, from YYYY-MM-DD HH:MM format
|
||||
# not used yet
|
||||
if \grep -q -- - "$1"; then
|
||||
rm "$1"
|
||||
fi
|
||||
}
|
||||
|
||||
_gcert_expiration_from_epoch() {
|
||||
strftime -r "%Y-%m-%d %R" "$1" 2>/dev/null
|
||||
if [[ $? != 0 ]]; then
|
||||
echo "0"
|
||||
fi
|
||||
}
|
||||
|
||||
gcert_expiry_update() {
|
||||
local expiration_text expiration_time
|
||||
expiration_text="$(gcertstatus -show_expiration_time 2>/dev/null)"
|
||||
if [[ "$?" -ne 0 ]]; then
|
||||
echo "0" > $EXPIRY_FILE
|
||||
else
|
||||
expiration_time=$(\grep "LOAS2" <<< $expiration_text | sed "s/LOAS2 expires at //")
|
||||
_gcert_expiration_from_epoch $expiration_time > $EXPIRY_FILE
|
||||
fi
|
||||
}
|
||||
|
||||
_gcert_expiry_check_update() {
|
||||
if [[ "${ZSH_THEME_GCERT_PROMPT_PARANOID:=false}" != true ]]; then
|
||||
if [[ ! -f "$EXPIRY_FILE" ]]; then
|
||||
gcert_expiry_update
|
||||
elif builtin fc -lm '*gcert*' -1 &>/dev/null; then
|
||||
gcert_expiry_update
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
prompt_gcert() {
|
||||
emulate -L zsh
|
||||
_gcert_expiry_check_update
|
||||
local -i expiration_time remaining_hours remaining_minutes
|
||||
local gcert_state glyph fg bg msg
|
||||
expiration_time=$(<$EXPIRY_FILE)
|
||||
remaining_hours=$(( ($expiration_time - $EPOCHSECONDS) / 3600 ))
|
||||
remaining_minutes=$(( (($expiration_time - $EPOCHSECONDS) / 60) % 60 ))
|
||||
if [[ $remaining_hours -lt 0 || $remaining_minutes -lt 0 ]]; then
|
||||
gcert_state=EXPIRED
|
||||
fg=red
|
||||
bg=black
|
||||
msg=${POWERLEVEL9K_GCERT_EXPIRED_MESSAGE:-Expired}
|
||||
elif [[ $remaining_hours -lt ${POWERLEVEL9K_GCERT_LOW_THRESHOLD=2} ]]; then
|
||||
gcert_state=LOW
|
||||
fg=yellow
|
||||
bg=black
|
||||
msg=${POWERLEVEL9K_GCERT_LOW_MESSAGE:-${remaining_hours}h ${remaining_minutes}m}
|
||||
else
|
||||
gcert_state=NORMAL
|
||||
fg=green
|
||||
bg=black
|
||||
msg=${POWERLEVEL9K_GCERT_NORMAL_MESSAGE:-${remaining_hours}h ${remaining_minutes}m}
|
||||
fi
|
||||
# glyph=$'\uf623'
|
||||
|
||||
p10k segment -s ${gcert_state} -i "$glyph" +r -f $fg -b $bg -t "$msg"
|
||||
}
|
||||
|
||||
gcert_prompt_time() {
|
||||
emulate -L zsh
|
||||
_gcert_expiry_check_update
|
||||
local -i expiration_time remaining_hours remaining_minutes
|
||||
expiration_time=$(<$EXPIRY_FILE)
|
||||
remaining_hours=$(( ($expiration_time - $EPOCHSECONDS) / 3600 ))
|
||||
remaining_minutes=$(( (($expiration_time - $EPOCHSECONDS) / 60) % 60 ))
|
||||
if [[ $remaining_hours -lt 0 || $remaining_minutes -lt 0 ]]; then
|
||||
echo "${ZSH_THEME_GCERT_PROMPT_EXPIRED=expired}"
|
||||
elif [[ $remaining_hours -lt ${ZSH_THEME_GCERT_PROMPT_WARN_HOURS=2} ]]; then
|
||||
echo "${ZSH_THEME_GCERT_PROMPT_WARN_PREFIX}${remaining_hours}h ${remaining_minutes}m${ZSH_THEME_GCERT_PROMPT_WARN_POSTFIX}"
|
||||
else
|
||||
echo "${ZSH_THEME_GCERT_PROMPT_PREFIX}${remaining_hours}h ${remaining_minutes}m${ZSH_THEME_GCERT_PROMPT_POSTFIX}"
|
||||
fi
|
||||
}
|
Reference in New Issue
Block a user