Skip to content

Commit b600bfe

Browse files
[Refactor] - refactor aws_get_command to reuse some components
1 parent 4e0c77c commit b600bfe

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

main.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export aws_cli_input_tmp="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/inputs"
2929
export aws_cli_input_folder="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_inputs"
3030
export aws_cli_list_commands_folder="${aws_cli_input_folder}/aws_services_commands"
3131
export aws_tmp_input="/tmp/aws_tmp_input_23647494949484.txt"
32+
export aws_cli_document_root_url="https://awscli.amazonaws.com/v2/documentation/api/latest/reference"
3233
export aws_assume_role_print_account_info="false"
3334
export aws_cli_retry_time=10
3435
export aws_cli_retry_sleep_interval=1

services/help.sh

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,47 @@ aws_main_function() {
3232
aws_get_command() {
3333
function curl_aws_document_and_cut() {
3434
local aws_service_name=$1
35-
local curl_path=$([ -z "$aws_service_name" ] && echo "index.html" || echo "$aws_service/index.html" )
36-
curl -s "https://awscli.amazonaws.com/v2/documentation/api/latest/reference/${curl_path}" \
37-
| grep '<li class="toctree-l1"><a class="reference internal"' \
38-
| awk -F '.html">' '{print $2}' \
39-
| awk -F '</a>' '{print $1}'
40-
}
41-
42-
if [ ! -s ${aws_cli_input_folder}/aws_list_services.txt ]; then
43-
curl_aws_document_and_cut > ${aws_cli_input_folder}/aws_list_services.txt
44-
fi
45-
46-
local aws_service=$(cat ${aws_cli_input_folder}/aws_list_services.txt | peco --prompt "Select service >")
35+
local curl_path="index.html"
36+
local cache_file="${aws_cli_input_folder}/aws_list_services.txt"
4737

48-
if [ -z "$aws_service" ]; then
49-
return
38+
if [ -n "$aws_service_name" ]; then
39+
curl_path="${aws_service_name}/${curl_path}"
40+
local cache_file="${aws_cli_list_commands_folder}/$aws_service.txt"
5041
fi
5142

52-
if [ ! -s ${aws_cli_list_commands_folder}/aws_service.txt ]; then
53-
curl_aws_document_and_cut "$aws_service" > ${aws_cli_list_commands_folder}/$aws_service.txt
43+
if [ ! -s "${cache_file}" ]; then
44+
curl -s "${aws_cli_document_root_url}/${curl_path}" |
45+
grep '<li class="toctree-l1"><a class="reference internal"' |
46+
awk -F '.html">' '{print $2}' |
47+
awk -F '</a>' '{print $1}' >${cache_file}
5448
fi
5549

56-
local aws_command=$(cat ${aws_cli_list_commands_folder}/$aws_service.txt | peco --prompt "aws $aws_service" --on-cancel error)
50+
cat ${cache_file}
51+
52+
}
53+
54+
local aws_service=$(curl_aws_document_and_cut | peco --prompt "Select service >")
55+
56+
if [ -z "$aws_service" ]; then
57+
return
58+
fi
59+
60+
local aws_command=$(curl_aws_document_and_cut "$aws_service" | peco --prompt "aws $aws_service" --on-cancel error)
5761

5862
if [ -z "$aws_command" ]; then
59-
return
60-
fi
63+
return
64+
fi
6165

6266
local final_action=$(echo -e "input\ndocument\nhelp" | peco)
6367

6468
if [ "$final_action" = "input" ]; then
65-
local aws_input_terminal="aws $aws_service $aws_command"
66-
elif [ "$final_action" = "document" ]; then
67-
local aws_input_terminal="open https://awscli.amazonaws.com/v2/documentation/api/latest/reference/$aws_service/$aws_command.html"
68-
else
69-
local aws_input_terminal="aws $aws_service $aws_command help"
70-
fi
69+
local aws_input_terminal="aws $aws_service $aws_command"
70+
elif [ "$final_action" = "document" ]; then
71+
local aws_input_terminal="open ${aws_cli_document_root_url}/${aws_service}/${aws_command}.html"
72+
else
73+
local aws_input_terminal="aws $aws_service $aws_command help"
74+
fi
7175

72-
local BUFFER=$(
73-
echo "$aws_input_terminal" | peco --query "$LBUFFER" --select-1
74-
)
76+
local BUFFER=${aws_input_terminal}
7577
CURSOR=$#BUFFER
7678
}

0 commit comments

Comments
 (0)