From e43133d4e745d5d1bd6b6e379771f9011519cc2b Mon Sep 17 00:00:00 2001 From: "son.lam" Date: Fri, 7 Nov 2025 11:05:17 +0700 Subject: [PATCH 1/2] Add - implement caching mechanism for command line inputs and enhance EC2 instance selection --- common/peco.sh | 60 +++++++++++++++++++++++++++-------------- services/assume_role.sh | 3 ++- services/ec2/ec2.sh | 20 +++++--------- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/common/peco.sh b/common/peco.sh index 0443bed..312b316 100644 --- a/common/peco.sh +++ b/common/peco.sh @@ -36,6 +36,22 @@ function peco_aws_input() { peco_commandline_input "${1} --output text" $2 } +function peco_commandline_get_cache_file_name() { + local commandline="${1}" + local md5_hash=$(echo "$commandline" | md5) + echo "${md5_hash}" +} + +function peco_comandline_input_clear_cache() { + local commandline="${1}" + local md5_hash=$(peco_commandline_get_cache_file_name "${commandline}") + local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:-NOTSET}" + local input_file_path="${input_folder}/${md5_hash}.txt" + if [ -f "${input_file_path}" ]; then + rm -f "${input_file_path}" + fi +} + function peco_commandline_input() { local commandline="${1}" @@ -46,7 +62,7 @@ function peco_commandline_input() { input_expired_time=0 fi - local md5_hash=$(echo $commandline | md5) + local md5_hash=$(peco_commandline_get_cache_file_name "${commandline}") local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:-NOTSET}" mkdir -p ${input_folder} local input_file_path="${input_folder}/${md5_hash}.txt" @@ -198,30 +214,34 @@ function peco_aws_iam_list_attached_policies() { # EC2 Instance function peco_aws_ec2_list() { local instance_state=${1:-'running'} + local refresh=${2:-'false'} + local commandline - commandline="aws ec2 describe-instances \ - --filters Name=instance-state-name,Values=${instance_state} \ - --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \ - --output text | tr -s '\t' '_'" - peco_commandline_input ${commandline} 'true' -} + # set -x -function peco_aws_ec2_list_all() { - commandline="aws ec2 describe-instances \ - --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \ - --output text | tr -s '\t' '_'" - peco_commandline_input ${commandline} 'true' + if [[ "${instance_state}" = "all" ]]; then + commandline="aws ec2 describe-instances \ + --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \ + --output text | tr -s '\t' '_'" + else + commandline="aws ec2 describe-instances \ + --filters Name=instance-state-name,Values=${instance_state} \ + --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \ + --output text | tr -s '\t' '_'" + fi + + if [ "${refresh}" = "true" ]; then + peco_comandline_input_clear_cache "${commandline}" + fi + + peco_commandline_input "${commandline}" 'true' + + # set +x } function peco_aws_ssm_list_parameters() { - commandline=" \ - aws ssm get-parameters-by-path \ - --path "/" \ - --recursive \ - --query 'Parameters[*].Name' \ - | jq -r '.[]' - " - peco_commandline_input ${commandline} 'true' + commandline="aws ssm get-parameters-by-path --path "/" --recursive --query 'Parameters[*].Name' | jq -r '.[]'" + peco_commandline_input "${commandline}" 'true' } function peco_aws_dynamodb_list_tables() { diff --git a/services/assume_role.sh b/services/assume_role.sh index a163b2e..250c813 100644 --- a/services/assume_role.sh +++ b/services/assume_role.sh @@ -268,7 +268,8 @@ aws_assume_role_get_tmp_credentials_for_new_members() { local tmp_credentials_file="${tmp_credentials}/${ASSUME_ROLE}" aws_assume_role_set_name_with_hint aws_assume_role_unzip_tmp_credential $assume_role - cat ${tmp_credentials_file} && rm -rf ${tmp_credentials_file} + cat ${tmp_credentials_file} | grep "export" | grep -v "ASSUMED_ROLE" && rm -rf ${tmp_credentials_file} + echo "export AWS_REGION=$AWS_REGION" } diff --git a/services/ec2/ec2.sh b/services/ec2/ec2.sh index bbeb37e..9489bff 100644 --- a/services/ec2/ec2.sh +++ b/services/ec2/ec2.sh @@ -1,20 +1,14 @@ #!/bin/bash -# TODO local_aws_ec2_instance_id_peco_menu create ec2 instance_id list -# @param filter by state (running, stopped, all) -# @return ec2_instance_id that you choose -# local_aws_ec2_instance_id_peco_menu() { local instance_state=${1:-'running'} + local refresh_caching=${2:-'false'} + local aws_ec2_instance_id - if [[ "${instance_state}" = "all" ]]; then - local aws_ec2_instance_id=$(peco_create_menu 'peco_aws_ec2_list_all') - else - local aws_ec2_instance_id=$(peco_create_menu "peco_aws_ec2_list ${instance_state}") - fi + aws_ec2_instance_id=$(peco_create_menu "peco_aws_ec2_list '${instance_state}' '${refresh_caching}'") aws_ec2_instance_id=$(echo "${aws_ec2_instance_id}" | awk -F "_" '{print $1}') - echo ${aws_ec2_instance_id} + echo "${aws_ec2_instance_id}" } # AWS ec2 @@ -75,7 +69,7 @@ aws_ec2_stop() { } aws_ec2_stop_with_hint() { - aws_ec2_stop $(local_aws_ec2_instance_id_peco_menu) + aws_ec2_stop $(local_aws_ec2_instance_id_peco_menu 'running' 'true') } aws_ec2_start() { @@ -85,8 +79,8 @@ aws_ec2_start() { " } -aws_ec2_start_with_hint() { - aws_ec2_start $(local_aws_ec2_instance_id_peco_menu 'stopped') +aws_ec2_start_with_hint() { + aws_ec2_start $(local_aws_ec2_instance_id_peco_menu 'stopped' 'true') } aws_ec2_rm_instruction() { From d04dcf90bf190c7f867f2dcf8bc25b67f6574900 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Fri, 7 Nov 2025 11:17:30 +0700 Subject: [PATCH 2/2] Fix aws_ec2_get_with_hint to pass parameters for instance selection --- services/ec2/ec2.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/ec2/ec2.sh b/services/ec2/ec2.sh index 9489bff..707df1b 100644 --- a/services/ec2/ec2.sh +++ b/services/ec2/ec2.sh @@ -51,7 +51,7 @@ aws_ec2_get() { } aws_ec2_get_with_hint() { - aws_ec2_get $(local_aws_ec2_instance_id_peco_menu) + aws_ec2_get "$(local_aws_ec2_instance_id_peco_menu 'all' 'true')" } aws_ec2_reboot() {