oh-my-fish / theme-bobthefish

A Powerline-style, Git-aware fish theme optimized for awesome.
MIT License
1.44k stars 223 forks source link

aws_vault prompt not working on macOS: date does not accept the options #310

Open krissrex opened 2 years ago

krissrex commented 2 years ago

Environment: macOS Big Sur 11.6 Tl;dr: macOS has Unix date, bobthefish assumes Linux date.


There 2 problems: https://github.com/oh-my-fish/theme-bobthefish/blob/626bd39b002535d69e56adba5b58a1060cfb6d7b/functions/fish_prompt.fish#L666 https://github.com/oh-my-fish/theme-bobthefish/blob/626bd39b002535d69e56adba5b58a1060cfb6d7b/functions/fish_prompt.fish#L667

Enabling the theme_display_aws_vault_profile results in:

date: illegal option -- -
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

This is because date on macOS (unix) behaves differently from Linux:

date --utc +%s
date: illegal option -- -
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Potential solution: I assume the -u flag works on a regular linux install. It also works on macOS:

date -u +%s
1633680739

https://github.com/oh-my-fish/theme-bobthefish/blob/626bd39b002535d69e56adba5b58a1060cfb6d7b/functions/fish_prompt.fish#L667

Second problem: date does not like the -d flag.

date -d 2021-10-08T09:15:15Z +%s
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

man date says the following (unix date):

-d dst Set the kernel's value for daylight saving time. If dst is non-zero, future calls to gettimeofday(2) will return a non-zero for tz_dsttime.

I did not find an appropriat option for this for mac's date. However, macOS has gdate if coreutils are installed (brew install coreutils), and this works like linux's date.

So the fix may be to use gdate on macOS.

set -l now (gdate --utc +%s)
set -l expiry (gdate -d "$AWS_SESSION_EXPIRATION" +%s)

The best solution would not require installing anything extra.


Relevant on this topic: https://www.shell-tips.com/linux/how-to-format-date-and-time-in-linux-macos-and-bash/