From 43a6b813082bce92e25569cb53cd840e3878544e Mon Sep 17 00:00:00 2001 From: Hyeon Kim Date: Wed, 30 Dec 2020 16:51:20 +0900 Subject: [PATCH 1/2] Add 'mssh' completion mssh is a Python client for accessing EC2 instances via AWS EC2 Instance Connect. References: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-methods.html#ec2-instance-connect-connecting-ec2-cli https://github.com/aws/aws-ec2-instance-connect-cli https://pypi.org/project/ec2instanceconnectcli/ --- src/_mssh | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/_mssh diff --git a/src/_mssh b/src/_mssh new file mode 100644 index 0000000..ee6b763 --- /dev/null +++ b/src/_mssh @@ -0,0 +1,52 @@ +#compdef mssh + +# mssh is a Python client for accessing EC2 instances via AWS EC2 Instance +# Connect. +# +# References: +# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-methods.html#ec2-instance-connect-connecting-ec2-cli +# https://github.com/aws/aws-ec2-instance-connect-cli +# https://pypi.org/project/ec2instanceconnectcli/ + +# Define function only when it doesn't exist +(( $+functions[_mssh_cache_policy] )) || _mssh_cache_policy() { + # Cache invalidates after 30 seconds + # + # Reference: + # http://zsh.sourceforge.net/Doc/Release/Expansion.html#index-globbing_002c-qualifiers + local -a oldp + oldp=( "$1"(ms+30) ) + (( $#oldp )) +} + +# Unless user explicitly turned off caching, enable caching just for this context +local existing_setting +zstyle -s ":completion:${curcontext}:" use-cache existing_setting +if [[ -z "${existing_setting}" ]]; then + zstyle ":completion:${curcontext}:" use-cache on +fi + +# Update cache policy only when there was no existing policy +local existing_policy +zstyle -s ":completion:${curcontext}:" cache-policy existing_policy +if [[ -z "${existing_policy}" ]]; then + zstyle ":completion:${curcontext}:" cache-policy _mssh_cache_policy +fi + +local -a instances +if _cache_invalid mssh_instances || ! _retrieve_cache mssh_instances; then + # Cache is invalid or caching feature is disabled + IFS=$'\n\t' instances=($( + aws ec2 describe-instances \ + --query 'Reservations[].Instances[] | [?State.Name == `running`].join(`:`, [InstanceId, Tags[?Key == `Name`].Value | [0]])' \ + --output text + )) + + _store_cache mssh_instances instances +fi + +_describe 'command' instances + +# Reference: +# http://zsh.sourceforge.net/Doc/Release/Completion-System.html +# https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org From 7a4574cd45d77cf369fe4a5c05ca7217d95b66d4 Mon Sep 17 00:00:00 2001 From: Hyeon Kim Date: Wed, 30 Dec 2020 20:03:56 +0900 Subject: [PATCH 2/2] _mssh: Add a license header --- src/_mssh | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/_mssh b/src/_mssh index ee6b763..553d944 100644 --- a/src/_mssh +++ b/src/_mssh @@ -1,5 +1,28 @@ #compdef mssh - +# ------------------------------------------------------------------------------ +# Copyright (c) 2020 Hyeon Kim +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. +# ------------------------------------------------------------------------------ +# Description +# ----------- # mssh is a Python client for accessing EC2 instances via AWS EC2 Instance # Connect. # @@ -7,6 +30,14 @@ # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-methods.html#ec2-instance-connect-connecting-ec2-cli # https://github.com/aws/aws-ec2-instance-connect-cli # https://pypi.org/project/ec2instanceconnectcli/ +# +# ------------------------------------------------------------------------------ +# Authors +# ------- +# +# * Hyeon Kim (https://hyeon.me/) +# +# ------------------------------------------------------------------------------ # Define function only when it doesn't exist (( $+functions[_mssh_cache_policy] )) || _mssh_cache_policy() {