Skip to content

Instantly share code, notes, and snippets.

@aoyama-val
Last active October 21, 2023 00:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoyama-val/e0646e6d8064a6cb360751d15a4dcad6 to your computer and use it in GitHub Desktop.
Save aoyama-val/e0646e6d8064a6cb360751d15a4dcad6 to your computer and use it in GitHub Desktop.
zsh completion function for jorgebastida/awslogs
#compdef awslogs
_awslogs() {
local context curcontext=$curcontext state line
declare -A opt_args
local ret=1
local -a common_options
_arguments -C \
'(-h --help)'{-h,--help}'[show this help message and exit]' \
--version"[show program's version number and exit]" \
'1: :__awslogs_commands' \
'*:: :->args' \
&& ret=0
common_options=(
--aws-access-key-id'[aws access key id]:AWS_ACCESS_KEY_ID:' \
--aws-secret-access-key'[aws secret access key]:AWS_SECRET_ACCESS_KEY:' \
--aws-session-token'[aws session token]:AWS_SESSION_TOKEN:' \
--profile'[aws profile]:AWS_PROFILE:__awslogs_profiles' \
--aws-region'[aws region]:AWS_REGION:__awslogs_regions' \
--aws-endpoint-url'[aws endpoint url to services such localstack, fakes3, others]:AWS_ENDPOINT_URL:' \
)
case $state in
(args)
case $words[1] in
(get)
_arguments -s : \
"${common_options[@]}" \
'(-f --filter-pattern)'{-f,--filter-pattern}'[A valid CloudWatch Logs filter pattern to use for filtering the response. If not provided, all the events are matched.]:FILTER_PATTERN:' \
'(-w --watch)'{-w,--watch}'[Query for new log lines constantly]' \
'(-i --watch-interval)'{-i,--watch-interval}'[Interval in seconds at which to query for new log lines]:WATCH_INTERVAL' \
'(-G --no-group)'{-G,--no-group}'[Do not display group name]' \
'(-S --no-stream)'{-S,--no-stream}'[Do not display stream name]' \
--timestamp'[Add creation timestamp to the output]' \
--ingestion-time'[Add ingestion time to the output]' \
'(-s --start)'{-s,--start}'[Start time]:START:__awslogs_time' \
'(-e --end)'{-e,--end}'[End time]:END:__awslogs_time' \
--color"[When to color output. WHEN can be 'auto' (default if omitted), 'never', or 'always'. With --color=auto, output is colored only when standard output is connected to a terminal.]:WHEN:(auto never always)" \
'(-q --query)'{-q,--query}'[JMESPath query to use in filtering the response data]:QUERY:' \
'1: :__awslogs_groups' \
&& ret=0
;;
(groups)
_arguments -s -C : \
"${common_options[@]}" \
'(-p --log-group-prefix)'{-p,--log-group-prefix}'[List only groups matching the prefix]:LOG_GROUP_PREFIX:' \
&& ret=0
;;
(streams)
_arguments -s -C : \
"${common_options[@]}" \
'(-s --start)'{-s,--start}'[Start time (default 1h)]:START:__awslogs_time' \
'(-e --end)'{-e,--end}'[End time]:END:__awslogs_time' \
&& ret=0
;;
esac
;;
esac
return ret
}
__awslogs_commands() {
local -a _c
_c=(
'get:Get logs'
'groups:List groups'
'streams:List streams'
)
_describe -t commands Commands _c
}
__awslogs_profiles() {
local _profile_path="${HOME}/.aws/credentials"
local -a _profiles
_profiles=(
${(@f)"$(_call_program profiles \
"grep -E '^\[.*\]$' "$_profile_path" \
| sed -e 's/\[//' -e 's/\]//'")"}
)
_describe -t profiles Profiles _profiles
}
__awslogs_regions() {
local -a _regions
_regions=(af-south-1 ap-east-1 ap-northeast-1 ap-northeast-2 ap-northeast-3 ap-south-1 ap-south-2 ap-southeast-1 ap-southeast-2 ap-southeast-3 ap-southeast-4 ca-central-1 eu-central-1 eu-central-2 eu-north-1 eu-south-1 eu-south-2 eu-west-1 eu-west-2 eu-west-3 il-central-1 me-central-1 me-south-1 sa-east-1 us-east-1 us-east-2 us-west-1 us-west-2)
_describe -t regions Regions _regions
}
__awslogs_time() {
_values 'time' "$(date +'%Y-%m-%d %H\:%M\:%S')" 1m 1h 1d 1w
}
__awslogs_groups() {
local -a _groups
local _cache_ident
local update_policy
_cache_ident='awslogs_groups'
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
if [[ -z $update_policy ]]; then
zstyle ":completion:${curcontext}:" cache-policy __awslogs_caching_policy
fi
if _cache_invalid $_cache_ident || ! _retrieve_cache $_cache_ident; then
_groups=( ${(@f)"$(_call_program log_groups 'awslogs groups')"} )
_store_cache $_cache_ident _groups
fi
_describe -t groups Groups _groups
}
__awslogs_caching_policy() {
local -a oldp
oldp=( "$1"(Nm+1) )
(( $#oldp ))
}
_awslogs "$@"
@aoyama-val
Copy link
Author

aoyama-val commented Oct 19, 2023

To use this, just put the file in your favorite path and add it to your ~/.zshrc:

fpath=(/path/to/the-directory-of-the-file(N) $fpath)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment