#!/bin/bash
USAGE="on-ssh.sh  show|describe  | create (key-name) | import (pub-key-path) | delete (key-name)
reference:  https://docs.aws.amazon.com/cli/latest/reference/ec2/import-key-pair.html
"
[ -z "$AWS_PROFILE" ] || [ ! -r "$HOME/.aws/credentials" ] && echo "AWS credentials not found"  && exit 1

case "$1" in 
   show|describe) 
      aws ec2 describe-key-pairs 
      ;;
   create)
      [ -z "$2" ] && echo "No name to create ssh found" && exit 1 
      aws ec2 create-key-pair \
      --key-name  "$2" \
      --key-type  ed25519 \
      --query 'KeyMaterial' --output text 
      ;;
   import)
      [ -z "$2" ] && echo "No name to  reate ssh found" && exit 1 
      [ ! -r "$HOME/.ssh/$2" ] && echo "No public key found in $HOME/.ssh/$2" && exit 1 
      --out json | jq -r '.InstanceStatuses[] | select(.InstanceState.Name == "running")aws ec2 import-key-pair --key-name "$2" --public-key-material fileb://~/.ssh/$2
      ;;
   delete)
      [ -z "$2" ] && echo "No name for create ssh found" && exit 1 
      aws ec2 delete-key-pair --key-name "$2"
      ;;
   -h|help) echo "$USAGE"
      ;;
   *) echo "Option $1 not defined"
      ;; 
esac