Skip to content

Commit 82d3061

Browse files
authored
Merge pull request #30 from lamhaison/feature/add-more-funtions-to-support-working
Feature/add more funtions to support working
2 parents 3b1cac4 + 5fc09b3 commit 82d3061

32 files changed

+1491
-410
lines changed

common/common.sh

+44-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ aws_run_commandline() {
6464
}
6565

6666
function aws_commandline_logging() {
67+
local log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log
6768
local aws_commandline_logging=$(echo ${1:?'aws_commandline is unset or empty'} | tr -d '\t' | tr -d '\n' | tr -s ' ')
6869

6970
if [ "$aws_show_commandline" = "true" ]; then
70-
echo "Running commandline [ ${aws_commandline_logging} ]"
71+
echo "Running commandline [ ${aws_commandline_logging} ]" | tee -a ${log_file_path}
7172
fi
7273
}
7374

@@ -97,3 +98,45 @@ aws_run_commandline_with_logging() {
9798
echo $aws_commandline_result | eval $tee_command
9899
echo "------------------------------FINISHED-$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null
99100
}
101+
102+
# Waiting function
103+
function aws_wait() { # To run commandline and wait the status
104+
105+
echo "Accessing the waiting status function"
106+
107+
local commandline=$1
108+
# The status that you want to wait
109+
local waiting_status=$2
110+
local max_retry_times=${3:-'30'}
111+
local sleep_time=${3:-'60'}
112+
113+
# Check input invalid
114+
if [[ -z "$commandline" ]]; then return 1; fi
115+
if [[ -z "$waiting_status" ]]; then return 1; fi
116+
117+
# Initialize the counter variable
118+
local counter=0
119+
local current_status
120+
121+
# Start the while loop
122+
while [[ $counter -lt $max_retry_times ]]; do
123+
current_status=$(eval "${commandline}")
124+
125+
echo "[ Iteration: $counter ] - The expected status ${waiting_status}, Current status is ${current_status}"
126+
127+
if [[ "$current_status" == "${waiting_status}" ]]; then
128+
echo "${current_status}"
129+
return 0
130+
fi
131+
132+
sleep "${sleep_time}"
133+
# Increment the counter inside a subshell and ignore failure
134+
((counter = counter + 1))
135+
136+
done
137+
138+
# Can't successful during waiting
139+
echo "The expected status ${waiting_status}, Current status is ${current_status}"
140+
exit 1
141+
142+
}

common/peco.sh

+48-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ peco_commandline_input() {
4242
local result_cached=$2
4343
local input_expired_time="${3:-$peco_input_expired_time}"
4444

45+
if [ "${peco_aws_disable_input_cached}" = "0" ]; then
46+
input_expired_time=0
47+
fi
48+
4549
local md5_hash=$(echo $commandline | md5)
4650
local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:-NOTSET}"
4751
mkdir -p ${input_folder}
@@ -193,8 +197,17 @@ peco_aws_iam_list_attached_policies() {
193197

194198
# EC2 Instance
195199
peco_aws_ec2_list() {
200+
local instance_state=${1:-'running'}
201+
202+
commandline="aws ec2 describe-instances \
203+
--filters Name=instance-state-name,Values=${instance_state} \
204+
--query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId,PrivateIpAddress:PrivateIpAddress}' \
205+
--output text | tr -s '\t' '_'"
206+
peco_commandline_input ${commandline} 'true'
207+
}
196208

197-
commandline="aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId}' --output text | tr -s '\t' '_'"
209+
peco_aws_ec2_list_all() {
210+
commandline="aws ec2 describe-instances --query 'Reservations[].Instances[].{Name: Tags[?Key==\`Name\`].Value | [0],InstanceId:InstanceId.PrivateIpAddress:PrivateIpAddress}' --output text | tr -s '\t' '_'"
198211
peco_commandline_input ${commandline} 'true'
199212
}
200213

@@ -208,3 +221,37 @@ peco_aws_ssm_list_parameters() {
208221
"
209222
peco_commandline_input ${commandline} 'true'
210223
}
224+
225+
peco_aws_dynamodb_list_tables() {
226+
peco_aws_input "aws dynamodb list-tables --query 'TableNames[]'" 'true'
227+
}
228+
229+
peco_aws_sqs_list() {
230+
peco_aws_input 'aws sqs list-queues --query "*[]"' 'true'
231+
}
232+
233+
peco_aws_eks_list_clusters() {
234+
peco_aws_input 'aws eks list-clusters --query "*[]"' 'true'
235+
}
236+
237+
peco_aws_cloudformation_list_stacks() {
238+
peco_aws_input 'aws cloudformation list-stacks --query "*[].StackName"' 'true'
239+
}
240+
241+
peco_aws_imagebuilder_list() {
242+
peco_aws_input 'aws imagebuilder list-image-pipelines --query "imagePipelineList[*].arn"' 'true'
243+
}
244+
245+
peco_aws_imagebuilder_list_recipes() {
246+
peco_aws_input 'aws imagebuilder list-image-recipes --query "*[].arn"' 'true'
247+
}
248+
249+
peco_aws_budgets_list() {
250+
aws_assume_role_get_aws_account_id
251+
peco_aws_input 'aws budgets describe-budgets --account-id=${AWS_ACCOUNT_ID} --query "*[].BudgetName"' 'true'
252+
}
253+
254+
peco_aws_secretmanager_list() {
255+
peco_aws_input 'aws secretsmanager list-secrets --query "*[].Name"' 'true'
256+
257+
}

main.sh

+14-8
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
# $2: Where do you want to save logs?
88
# $3: Do you want to set the bind key?
99

10+
DEFAULT_AWS_CLI_SOURCE_SCRIPTS='/opt/lamhaison-tools/aws-cli-utils'
11+
DEFAULT_AWS_CLI_DATA='/opt/lamhaison-tools/aws-cli-utils'
12+
1013
AWS_CLI_SOURCE_SCRIPTS=$1
1114

1215
if [[ -z "${AWS_CLI_SOURCE_SCRIPTS}" ]]; then
1316
LOCAL_AWS_CLI_SOURCE_SCRIPTS=$(dirname -- "$0")
14-
if [[ "${LOCAL_AWS_CLI_SOURCE_SCRIPTS}" = "." ]]; then
15-
DEFAULT_AWS_CLI_SOURCE_SCRIPTS='/opt/lamhaison-tools/aws-cli-utils'
17+
if [[ "${LOCAL_AWS_CLI_SOURCE_SCRIPTS}" == "." ]]; then
18+
LOCAL_AWS_CLI_SOURCE_SCRIPTS="${DEFAULT_AWS_CLI_SOURCE_SCRIPTS}"
1619
fi
1720

1821
export AWS_CLI_SOURCE_SCRIPTS="${LOCAL_AWS_CLI_SOURCE_SCRIPTS:-${DEFAULT_AWS_CLI_SOURCE_SCRIPTS}}"
@@ -23,15 +26,16 @@ fi
2326
AWS_CLI_DATA=$2
2427
if [[ -z "${AWS_CLI_DATA}" ]]; then
2528
LOCAL_AWS_CLI_DATA=$(dirname -- "$0")
26-
if [[ "${LOCAL_AWS_CLI_DATA}" = "." ]]; then
27-
DEFAULT_AWS_CLI_DATA='/opt/lamhaison-tools/aws-cli-utils'
29+
if [[ "${LOCAL_AWS_CLI_DATA}" == "." ]]; then
30+
LOCAL_AWS_CLI_DATA="${DEFAULT_AWS_CLI_DATA}"
2831
fi
2932

3033
export AWS_CLI_DATA="${LOCAL_AWS_CLI_DATA:-${DEFAULT_AWS_CLI_DATA}}"
3134
else
3235
export AWS_CLI_DATA=${AWS_CLI_DATA}
3336
fi
3437

38+
# shellcheck disable=SC2155
3539
export assume_role_password_encrypted="$(cat ~/.password_assume_role_encrypted)"
3640
export tmp_credentials="/tmp/aws_temporary_credentials"
3741

@@ -67,20 +71,20 @@ mkdir -p ${aws_cli_list_commands_folder}
6771
export AWS_DEFAULT_OUTPUT="json"
6872

6973
# add some help aliases
70-
alias get-account-alias='aws iam list-account-aliases'
74+
alias get-account-alias='aws iam list-account-aliases --query "*[0]" --output text'
7175
alias get-account-id='echo AccountId $(aws sts get-caller-identity --query "Account" --output text)'
7276

7377
# Import sub-commandlines.
7478
for script in $(
7579
find ${AWS_CLI_SOURCE_SCRIPTS} -type f -name '*.sh' |
76-
grep -v main.sh | grep -v main.sh | grep -v test.sh | grep -v temp.sh | grep -v aws-cli-utils.sh
80+
grep -v -E '.*(main.sh|test.sh|temp.sh|aws-cli-utils.sh)$'
7781
); do
7882
source $script
7983
done
8084

8185
# Reuse session in the new terminal
8286
export aws_cli_current_assume_role_name="/tmp/aws_cli_current_assume_role_SW7DNb48oQB57"
83-
export aws_cli_load_current_assume_role=false
87+
export aws_cli_load_current_assume_role=true
8488
# If the file is not empty
8589
# TODO Later (To check if the credential is expired, don't autoload credential)
8690
if [ "true" = "${aws_cli_load_current_assume_role}" ] && [ -s "${aws_cli_current_assume_role_name}" ]; then
@@ -89,7 +93,7 @@ fi
8993

9094
LHS_BIND_KEY=${3:-'True'}
9195

92-
if [[ "${LHS_BIND_KEY}" = "True" ]]; then
96+
if [[ ${LHS_BIND_KEY} == "True" && "$(which zle)" != "" ]]; then
9397
# Add hot-keys
9498
# zle -N aws_help
9599
zle -N aws_main_function
@@ -99,6 +103,8 @@ if [[ "${LHS_BIND_KEY}" = "True" ]]; then
99103
# Hotkey: Option + a + c
100104
bindkey 'åç' aws_get_command
101105

106+
bindkey '' aws_get_command
107+
102108
zle -N aws_history
103109
# Hotkey Option + ah
104110
bindkey '˙' aws_history

0 commit comments

Comments
 (0)