From 36cc21708a5614e77db56f798509c6f628ec994d Mon Sep 17 00:00:00 2001 From: lamhaison Date: Fri, 28 Apr 2023 22:18:24 +0700 Subject: [PATCH 01/26] [Add] - function lhs_docker_install_aws_linux_2_instruction --- services/docker.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/services/docker.sh b/services/docker.sh index 6cd7237..b3a628c 100644 --- a/services/docker.sh +++ b/services/docker.sh @@ -6,6 +6,21 @@ # ## +function lhs_docker_install_aws_linux_2_instruction() { + cat <<-__EOF__ + sudo amazon-linux-extras install -y docker + sudo service docker start + sudo usermod -a -G docker ec2-user + sudo chkconfig docker on + sudo yum install -y git + sudo curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-\$(uname -s)-\$(uname -m) -o /usr/local/bin/docker-compose + sudo chmod +x /usr/local/bin/docker-compose + echo 'export PATH="/usr/local/bin:\$PATH"' >> ~/.bash_profile + source ~/.bash_profile + docker-compose version + __EOF__ +} + function lhs_docker_run_mongodb_client() { echo "\ docker run -ti --rm mongo:5.0.10 bash From 88d9c53eca171826823f69779d8b507fdf447eaf Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 6 May 2023 22:08:36 +0700 Subject: [PATCH 02/26] [Remove] - function lhs_help_hotkey_sublime_search_files_instruction --- common/other.sh | 7 ------- 1 file changed, 7 deletions(-) diff --git a/common/other.sh b/common/other.sh index d5ba581..55d4d9c 100644 --- a/common/other.sh +++ b/common/other.sh @@ -15,13 +15,6 @@ function lhs_help_install_macos_peco_instruction() { } -function lhs_help_hotkey_sublime_search_files_instruction() { - cat <<-_EOF_ - # Search file in sublimetext on macos - ⌘(Command) + P - _EOF_ -} - lhs_help_create_os_user_instruction() { echo ' From c2267e44118d530c66777ede957be460f9e0d473 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Mon, 8 May 2023 21:52:27 +0700 Subject: [PATCH 03/26] [Improvement] - (Change to use the default ~/.zsh_history for history file) for using with zsh autocomplete --- main.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.sh b/main.sh index 37e87de..41d4623 100644 --- a/main.sh +++ b/main.sh @@ -29,7 +29,7 @@ fi LHS_CHANGE_HISTORY_SETTINGS=${3:-'True'} if [[ "${LHS_CHANGE_HISTORY_SETTINGS}" = "True" ]]; then - export HISTFILE="$HOME/.zhistory" + export HISTFILE="$HOME/.zsh_history" export HISTSIZE=10000 export SAVEHIST=10000 # Ignore duplicates in command history and increase From 79c13cbdafd0c14cd9b36acc951621d30a50655d Mon Sep 17 00:00:00 2001 From: lamhaison Date: Wed, 10 May 2023 22:53:13 +0700 Subject: [PATCH 04/26] [Add] - function for converting epoch time to human reable format time --- common/utils.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/utils.sh b/common/utils.sh index 870afcd..0009686 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -9,6 +9,15 @@ function lhs_date_get_with_format_yyyymmdd() { lhs_date_get_with_format "%Y%m%d" } +# TODO functilhs_time_convert_echo_to_human_readable_formaton_name to convert epoch time to human readable formnat. +# @param $1: the value of epoch time and $2: the formant(default is %Y-%m-%d-%H-%M-%S) +# @return +# +function lhs_time_convert_echo_to_human_readable_format() { + local default_date_format=+${2:-'%Y-%m-%d-%H-%M-%S'} + date -r ${1:?'epoch_value is unset or empty'} ${default_date_format} +} + # Password generate function lhs_password_generate() { # openssl rand -base64 10 | tr -d '=' From 06a51c2f76b6ee6c32b49786914cedeff18214ce Mon Sep 17 00:00:00 2001 From: lamhaison Date: Wed, 10 May 2023 22:56:28 +0700 Subject: [PATCH 05/26] [Chore] - to fix typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f65246..4219e11 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # helpful-commandlines -This is the repo to collect helpful commandlines that is used for mac and linux os. +This is the repo to collect helpful commandlines that is used for mac and Linux OS. ## Setup dependencies @@ -11,7 +11,7 @@ brew install glab export GITLAB_TOKEN=xxxxx ``` -### Install Github cli +### Install GitHub cli ``` brew install gh export GH_TOKEN=xxxx From b4c917bf8dbd4c827bd4ff0ebb6e6e8664976531 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Wed, 10 May 2023 23:06:42 +0700 Subject: [PATCH 06/26] [Improvement] - change the bindkey of search history by peco and change to use Option + R (Ctrl + R) is reserved for sh-history-substring-search --- README.md | 1 + main.sh | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4219e11..818b7a5 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ echo "source /opt/lamhaison-tools/helpful-commandlines/main.sh" >> ~/.bashrc # How to search commandline ``` Ctrl + H or lhs_help_helpful +Option + R for search history commandline(As the same with sh-history-substring-search) ``` diff --git a/main.sh b/main.sh index 41d4623..37f918c 100644 --- a/main.sh +++ b/main.sh @@ -79,7 +79,10 @@ LHS_BIND_KEY=${2:-'True'} if [[ "${LHS_BIND_KEY}" = "True" ]]; then # Add hot-keys zle -N lhs_peco_select_history - bindkey '^r' lhs_peco_select_history + + # Using zsh-history-substring-search reserved + # bindkey '^r' lhs_peco_select_history + # Option + r bindkey '®' lhs_peco_select_history From d702205c31c0543cb4a3f9b0b8ca81d03eaca3fa Mon Sep 17 00:00:00 2001 From: lamhaison Date: Mon, 22 May 2023 21:42:19 +0700 Subject: [PATCH 07/26] [Add] - note for how to build image optimized --- services/docker.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/services/docker.sh b/services/docker.sh index b3a628c..66d988e 100644 --- a/services/docker.sh +++ b/services/docker.sh @@ -81,3 +81,35 @@ function lhs_docker_build_git_secret_image() { EOF } + +function lhs_docker_docs_build_image_optimized_instruction() { + + local lhs_docs=$( + cat <<-__EOF__ + Use minimal base images + Use multistage builds + Use Dockerignore + Double-check the dependencies + Minimize the image layers + __EOF__ + ) + + echo "${lhs_docs}" +} + +function lhs_docker_analyze_docker_image_instruction() { + + local lhs_docs=$( + cat <<-__EOF__ + brew install dive # Install dive to analyze docker image + open "https://github.com/wagoodman/dive" # Open dive document + dive # Analyze docker image + __EOF__ + ) + echo "${lhs_docs}" +} + +function lhs_docker_docs_all() { + lhs_docs_add_prefix 'lhs_docker_docs_build_image_optimized_instruction' 'image' + lhs_docs_add_prefix 'lhs_docker_install_aws_linux_2_instruction' 'install' +} From 4c59da3eb7df25bc17192582a90dda1b5d1dfefd Mon Sep 17 00:00:00 2001 From: lamhaison Date: Mon, 5 Jun 2023 12:17:03 +0700 Subject: [PATCH 08/26] [Add] - add note for upgrading docker in ubuntu and installing telnet in docker alpine --- services/docker.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/services/docker.sh b/services/docker.sh index 66d988e..931b636 100644 --- a/services/docker.sh +++ b/services/docker.sh @@ -21,6 +21,15 @@ function lhs_docker_install_aws_linux_2_instruction() { __EOF__ } +function lhs_docker_upgrade_ubuntu_instruction() { + local lhs_docs=$( + cat <<-__EOF__ + https://docs.docker.com/engine/install/ubuntu/ + __EOF__ + ) + echo "$lhs_docs" +} + function lhs_docker_run_mongodb_client() { echo "\ docker run -ti --rm mongo:5.0.10 bash @@ -113,3 +122,15 @@ function lhs_docker_docs_all() { lhs_docs_add_prefix 'lhs_docker_docs_build_image_optimized_instruction' 'image' lhs_docs_add_prefix 'lhs_docker_install_aws_linux_2_instruction' 'install' } + +function lhs_docker_alpine_install_telnet_instruction() { + + local lhs_docs=$( + cat <<-__EOF__ + apk update + apk add busybox-extras + __EOF__ + ) + + echo "$lhs_docs" +} From 39f8f5ce07f7b3e1d315ccea19db24ee69e95183 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 10 Jun 2023 14:03:02 +0700 Subject: [PATCH 09/26] [Refactor] - function name --- common/help_ips.sh | 4 ---- common/peco/peco.sh | 2 +- common/utils.sh | 42 ++++++++++++++++++------------------- services/console_editor.sh | 12 ----------- services/curl.sh | 2 +- services/docker.sh | 4 ++-- services/name_convention.sh | 2 +- services/other.sh | 7 ------- terraform/terraform.sh | 4 ++-- 9 files changed, 28 insertions(+), 51 deletions(-) diff --git a/common/help_ips.sh b/common/help_ips.sh index 6bad5ca..00cdf80 100644 --- a/common/help_ips.sh +++ b/common/help_ips.sh @@ -9,10 +9,6 @@ function lhs_network_get_public_ip() { dig +short myip.opendns.com @resolver1.opendns.com } -function lhs_what_is_my_ip() { - lhs_network_get_public_ip -} - function lhs_network_tcp_traceroute() { # https://www.redhat.com/sysadmin/traceroute-finding-meaning diff --git a/common/peco/peco.sh b/common/peco/peco.sh index bf94d42..8536f68 100644 --- a/common/peco/peco.sh +++ b/common/peco/peco.sh @@ -85,7 +85,7 @@ function lhs_peco_commandline_input() { local format_text=$(lhs_peco_format_output_text $commandline_result) if [ -n "${format_text}" ]; then - commandline=$(lhs_util_format_commandline_one_line ${commandline}) + commandline=$(local_lhs_util_format_commandline_one_line ${commandline}) echo "******** [ ${commandline} ] ********" >${input_file_path} echo ${format_text} | tee -a ${input_file_path} else diff --git a/common/utils.sh b/common/utils.sh index 0009686..8efaecd 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -1,25 +1,25 @@ #!/bin/bash # Get DateTime -function lhs_date_get_with_format() { +function lhs_cmd_date_get_with_format() { echo $(date "+"${1:-"%Y-%m-%d-%H-%M-%S"}) } -function lhs_date_get_with_format_yyyymmdd() { - lhs_date_get_with_format "%Y%m%d" +function lhs_cmd_date_get_with_format_yyyymmdd() { + lhs_cmd_date_get_with_format "%Y%m%d" } -# TODO functilhs_time_convert_echo_to_human_readable_formaton_name to convert epoch time to human readable formnat. +# TODO function lhs_cmd_time_convert_echo_to_human_readable_formaton_name to convert epoch time to human readable formnat. # @param $1: the value of epoch time and $2: the formant(default is %Y-%m-%d-%H-%M-%S) # @return # -function lhs_time_convert_echo_to_human_readable_format() { +function lhs_cmd_time_convert_echo_to_human_readable_format() { local default_date_format=+${2:-'%Y-%m-%d-%H-%M-%S'} date -r ${1:?'epoch_value is unset or empty'} ${default_date_format} } # Password generate -function lhs_password_generate() { +function lhs_cmd_password_generate() { # openssl rand -base64 10 | tr -d '=' cd /tmp >/dev/null @@ -28,7 +28,7 @@ function lhs_password_generate() { } # Get DateTime -function lhs_file_name_get_random_name() { +function lhs_cmd_file_name_get_random_name() { local file_name=${1:-'FILENAME'} cd /tmp >/dev/null mktemp ${file_name}-XXXXXXXXXXXXXX @@ -53,7 +53,7 @@ function lhs_file_name_get_random_name() { # set +x # } -function lhs_run_commandline_with_retry() { +function local_local_lhs_run_commandline_with_retry() { local lhs_commandline=$1 local silent_mode=$2 local retry_counter=0 @@ -84,25 +84,25 @@ function lhs_run_commandline_with_retry() { } -function lhs_run_commandline() { - lhs_run_commandline=$1 - lhs_run_commandline="${lhs_run_commandline:?'lhs_run_commandline is unset or empty'}" - lhs_run_commandline_with_logging "${lhs_run_commandline}" +function local_lhs_run_commandline() { + local_lhs_run_commandline=$1 + local_lhs_run_commandline="${local_lhs_run_commandline:?'local_lhs_run_commandline is unset or empty'}" + local_local_lhs_run_commandline_with_logging "${local_lhs_run_commandline}" } -function lhs_commandline_logging() { +function local_lhs_commandline_logging() { local eval_commandline=${2:-'False'} - lhs_commandline_logging=$(echo ${1:?'lhs_commandline is unset or empty'} | tr -d '\t' | tr -d '\n') + local_lhs_commandline_logging=$(echo ${1:?'lhs_commandline is unset or empty'} | tr -d '\t' | tr -d '\n') if [[ "${eval_commandline}" == "True" ]]; then - echo "${lhs_commandline_logging}" + echo "${local_lhs_commandline_logging}" else - echo "Running commandline [ ${lhs_commandline_logging} ]" + echo "Running commandline [ ${local_lhs_commandline_logging} ]" fi } -function lhs_run_commandline_with_logging() { +function local_local_lhs_run_commandline_with_logging() { lhs_commandline=$1 if [ "$lhs_show_log_uploaded" = "true" ]; then local tee_command="tee -a ${lhs_cli_log_file_path} ${lhs_cli_log_uploaded_file_path}" @@ -117,18 +117,18 @@ function lhs_run_commandline_with_logging() { fi echo "------------------------------STARTED--$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null - lhs_commandline_logging $1 | eval $detail_commandline_tee_command - lhs_commandline_result=$(lhs_run_commandline_with_retry "${lhs_commandline}" "${ignored_error_when_retry}") + local_lhs_commandline_logging $1 | eval $detail_commandline_tee_command + lhs_commandline_result=$(local_local_lhs_run_commandline_with_retry "${lhs_commandline}" "${ignored_error_when_retry}") echo $lhs_commandline_result | eval $tee_command echo "------------------------------FINISHED-$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null } -function lhs_util_rm_space() { +function local_lhs_util_rm_space() { # echo "${1}" | sed 's/[[:space:]]//g' # https://stackoverflow.com/questions/13659318/how-to-remove-space-from-string echo "${1//+([[:space:]])/}" } -function lhs_util_format_commandline_one_line() { +function local_lhs_util_format_commandline_one_line() { echo ${1} | tr -d '\t' | tr -d '\n' | tr -s ' ' } diff --git a/services/console_editor.sh b/services/console_editor.sh index fa993da..b1f3023 100644 --- a/services/console_editor.sh +++ b/services/console_editor.sh @@ -16,15 +16,3 @@ function lhs_editor_with_tree() { fi } - -function lhs_vi() { - lhs_editor_with_tree "$@" -} - -function lhs_vim() { - lhs_editor_with_tree "$@" -} - -function lhs_view() { - lhs_editor_with_tree "" "view" -} diff --git a/services/curl.sh b/services/curl.sh index 62e218d..758a39c 100644 --- a/services/curl.sh +++ b/services/curl.sh @@ -1,6 +1,6 @@ #!/bin/bash -function lhs_curl_get_response_headers_only() { +function lhs_cmd_curl_get_response_headers_only() { lhs_curl_url=${1:-'https://devopsmountain.com'} echo "\ diff --git a/services/docker.sh b/services/docker.sh index 931b636..393278f 100644 --- a/services/docker.sh +++ b/services/docker.sh @@ -119,8 +119,8 @@ function lhs_docker_analyze_docker_image_instruction() { } function lhs_docker_docs_all() { - lhs_docs_add_prefix 'lhs_docker_docs_build_image_optimized_instruction' 'image' - lhs_docs_add_prefix 'lhs_docker_install_aws_linux_2_instruction' 'install' + local_lhs_docs_add_prefix 'lhs_docker_docs_build_image_optimized_instruction' 'image' + local_lhs_docs_add_prefix 'lhs_docker_install_aws_linux_2_instruction' 'install' } function lhs_docker_alpine_install_telnet_instruction() { diff --git a/services/name_convention.sh b/services/name_convention.sh index 8e40530..6742de9 100644 --- a/services/name_convention.sh +++ b/services/name_convention.sh @@ -1,5 +1,5 @@ #!/bin/bash -function lhs_name_convention_git_branch_name() { +function lhs_nameConvention_git_branch_name() { echo "develop staging master" } diff --git a/services/other.sh b/services/other.sh index 114250c..32ab08f 100644 --- a/services/other.sh +++ b/services/other.sh @@ -14,10 +14,3 @@ function lhs_code_function_name_instruction() { add: to add an item. __EOF__ } - -function lhs_vi_set_commandlines_instruction() { - cat <<-_EOF_ - : set nu - : set nu! or :set nonu - _EOF_ -} diff --git a/terraform/terraform.sh b/terraform/terraform.sh index 35f0c75..0fb4bff 100644 --- a/terraform/terraform.sh +++ b/terraform/terraform.sh @@ -1,9 +1,9 @@ #!/bin/bash -function lhs_tf_enable_debug_mode() { +function lhs_iac_tf_enable_debug_mode() { export TF_LOG=TRACE } -function lhs_tf_disable_debug_mode() { +function lhs_iac_tf_disable_debug_mode() { unset TF_LOG } From 47abb52039554c049d5cff97a960600c6cd1130a Mon Sep 17 00:00:00 2001 From: lamhaison Date: Tue, 20 Jun 2023 14:45:40 +0700 Subject: [PATCH 10/26] [Improvement] - add path of lhs tools when search dir path (for both git repo and lhs tools) --- common/peco/peco.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/common/peco/peco.sh b/common/peco/peco.sh index 8536f68..aa5a1ce 100644 --- a/common/peco/peco.sh +++ b/common/peco/peco.sh @@ -26,7 +26,14 @@ function lhs_peco_repo_list() { find ${LHS_PROJECTS_DIR} -type d -name '.git' -maxdepth 6 \ | awk -F '/' '{for (i=1; i Date: Wed, 26 Jul 2023 21:18:14 +0700 Subject: [PATCH 11/26] [Add] - comment for do later (fixing the bug of function lhs_cmd_time_convert_echo_to_human_readable_format) --- common/utils.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/utils.sh b/common/utils.sh index 8efaecd..1f0d68c 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -14,6 +14,8 @@ function lhs_cmd_date_get_with_format_yyyymmdd() { # @return # function lhs_cmd_time_convert_echo_to_human_readable_format() { + + # TODO Later (it didn't work rightnow) local default_date_format=+${2:-'%Y-%m-%d-%H-%M-%S'} date -r ${1:?'epoch_value is unset or empty'} ${default_date_format} } From 5ad0492a4d7248e395210a4cc1d10e186ad4d62c Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 29 Jul 2023 17:04:07 +0700 Subject: [PATCH 12/26] [Improvement] - optimize source sh files --- main.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.sh b/main.sh index 37f918c..1fc6f14 100644 --- a/main.sh +++ b/main.sh @@ -61,7 +61,7 @@ export LHS_HELPFUL_LOOKUP_CACHED=true # https://thevaluable.dev/zsh-line-editor-configuration-mouseless/ for script in $( find ${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS} -type f -name '*.sh' | - grep -v main.sh | grep -v test.sh | grep -v temp.sh | grep -v helpful-commandlines.sh + grep -v -E '.*(main.sh|test.sh|temp.sh|helpful-commandlines.sh)$' ); do source $script done From 594520ea65e08491d5d3775b8194209a91a04356 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 29 Jul 2023 17:59:21 +0700 Subject: [PATCH 13/26] [Fix] - can not search aws history commandline with aws_ec2_connect or check cw logs --- common/utils.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/common/utils.sh b/common/utils.sh index 1f0d68c..e0a5a26 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -93,13 +93,18 @@ function local_lhs_run_commandline() { } function local_lhs_commandline_logging() { + + local log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log + local tee_command="tee -a ${lhs_cli_log_file_path}" + local eval_commandline=${2:-'False'} - local_lhs_commandline_logging=$(echo ${1:?'lhs_commandline is unset or empty'} | tr -d '\t' | tr -d '\n') + + local local_lhs_commandline_logging=$(echo ${1:?'lhs_commandline is unset or empty'} | tr -d '\t' | tr -d '\n') if [[ "${eval_commandline}" == "True" ]]; then echo "${local_lhs_commandline_logging}" else - echo "Running commandline [ ${local_lhs_commandline_logging} ]" + echo "Running commandline [ ${local_lhs_commandline_logging} ]" | eval $tee_command fi } From cada3866d27dc1b5e9539b3e5e734480259ace53 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sun, 30 Jul 2023 10:06:57 +0700 Subject: [PATCH 14/26] [Improvement] - optimize when finding file --- common/peco/other.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/common/peco/other.sh b/common/peco/other.sh index 365a377..baa9f54 100644 --- a/common/peco/other.sh +++ b/common/peco/other.sh @@ -3,7 +3,10 @@ function lhs_peco_function_list() { # Ignore private function local lhs_function_list_cmd=" - find ${LHS_HELPFUL_LOOKUP} -type f -name '*.sh' | grep -v main.sh | xargs cat | grep -v '^function.*private.*' \ + find ${LHS_HELPFUL_LOOKUP} -type f -name '*.sh' \ + -not -path \"${LHS_HELPFUL_LOOKUP}/private-helpful-commandlines/tmp/*\" \ + -not -path \"${LHS_HELPFUL_LOOKUP}/dotfiles/*\" \ + | grep -v main.sh | xargs cat | grep -v '^function.*private.*' \ | grep -e '^function.*\(.+*\)' -e '^aws*\(.+*\)' -e '^peco*\(.+*\)' -e '^lhs*\(.+*\)' \ | tr -d '(){' | awk -F ' ' '{ print (\$1==\"function\") ? \$2 : \$1}' | sort " From f94897badc0094cf8285afecc62f3fd3d709407c Mon Sep 17 00:00:00 2001 From: lamhaison Date: Tue, 20 Jun 2023 14:45:40 +0700 Subject: [PATCH 15/26] [Improvement] - add path of lhs tools when search dir path (for both git repo and lhs tools) [Add] - function for getting epoch time in milisecon time for cw logs --- common/utils.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/utils.sh b/common/utils.sh index e0a5a26..5b3d5fc 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -9,6 +9,15 @@ function lhs_cmd_date_get_with_format_yyyymmdd() { lhs_cmd_date_get_with_format "%Y%m%d" } +function lhs_cmd_date_get_with_format_cw_log() { + lhs_cmd_date_get_with_format "%Y-%m-%d %H:%M:%S" +} + +function lhs_cmd_date_get_epoch_time() { + # unix-time-in-milliseconds + date -d '0 hour ago' +%s%N | cut -b1-13 +} + # TODO function lhs_cmd_time_convert_echo_to_human_readable_formaton_name to convert epoch time to human readable formnat. # @param $1: the value of epoch time and $2: the formant(default is %Y-%m-%d-%H-%M-%S) # @return From 331293da082982f79ed7cdf086a77a914041623b Mon Sep 17 00:00:00 2001 From: lamhaison Date: Fri, 4 Aug 2023 10:46:47 +0700 Subject: [PATCH 16/26] [Improvement] - ignore search tmp folders --- common/peco/other.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/common/peco/other.sh b/common/peco/other.sh index baa9f54..4252d12 100644 --- a/common/peco/other.sh +++ b/common/peco/other.sh @@ -17,7 +17,10 @@ function lhs_peco_function_list() { function lhs_peco_helpful_function_list() { local lhs_function_list_cmd=" - find ${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS} -type f -name '*.sh' | grep -v main.sh | xargs cat | grep -e '^function.*\(.+*\)' -e '^aws*\(.+*\)' -e '^peco*\(.+*\)' -e '^lhs*\(.+*\)' \ + find ${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS} -type f -name '*.sh' \ + -not -path \"${LHS_HELPFUL_LOOKUP}/private-helpful-commandlines/tmp/*\" \ + -not -path \"${LHS_HELPFUL_LOOKUP}/dotfiles/*\" \ + | grep -v main.sh | xargs cat | grep -e '^function.*\(.+*\)' -e '^aws*\(.+*\)' -e '^peco*\(.+*\)' -e '^lhs*\(.+*\)' \ | tr -d '(){' | awk -F ' ' '{ print (\$1==\"function\") ? \$2 : \$1}' | sort " From 1d62aa768d33643df6208690b6ba2b9c77e1cf2f Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 5 Aug 2023 15:42:08 +0700 Subject: [PATCH 17/26] [Fix] disable cached for lhs cli results --- common/peco/peco.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/common/peco/peco.sh b/common/peco/peco.sh index aa5a1ce..d0a9049 100644 --- a/common/peco/peco.sh +++ b/common/peco/peco.sh @@ -10,7 +10,7 @@ function lhs_peco_select_history() { fi BUFFER=$(history -n 1 | uniq | eval $tac | - peco --query "$LBUFFER") + peco --query "$LBUFFER" --initial-filter Regexp) # Move the cursor at then end of the input($#variable_name is to get the length itself) CURSOR=$#BUFFER # zle clear-screen @@ -70,10 +70,15 @@ function lhs_peco_run_command_to_get_input() { function lhs_peco_commandline_input() { - commandline="${1}" + local commandline="${1}" local result_cached=${2:-'false'} local input_expired_time="${3:-$lhs_cli_peco_input_expired_time}" + # To disable caching + if [ "$lhs_cli_peco_input_expired_time" = "0" ]; then + input_expired_time=0 + fi + local md5_hash=$(echo $commandline | md5) local input_folder="${lhs_cli_input:-/tmp/inputs}" mkdir -p ${input_folder} From e0398def6c8ad5e10cb2584a4e0d9aefcfd6204b Mon Sep 17 00:00:00 2001 From: lamhaison Date: Fri, 11 Aug 2023 10:43:00 +0700 Subject: [PATCH 18/26] [Add] function lhs_cmd_date_get_month for only get month of that time --- common/utils.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/utils.sh b/common/utils.sh index 5b3d5fc..668a7de 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -1,6 +1,10 @@ #!/bin/bash # Get DateTime + +function lhs_cmd_date_get_month() { + date +%m +} function lhs_cmd_date_get_with_format() { echo $(date "+"${1:-"%Y-%m-%d-%H-%M-%S"}) } From a37c33f242ddad0cb08fdfe2104b59b3eebaf246 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Fri, 11 Aug 2023 10:56:13 +0700 Subject: [PATCH 19/26] [Improvement] - add cached for lhs_cmd_search function more longer from setting main.sh --- common/peco/other.sh | 4 ++-- main.sh | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/common/peco/other.sh b/common/peco/other.sh index 4252d12..410c836 100644 --- a/common/peco/other.sh +++ b/common/peco/other.sh @@ -10,8 +10,8 @@ function lhs_peco_function_list() { | grep -e '^function.*\(.+*\)' -e '^aws*\(.+*\)' -e '^peco*\(.+*\)' -e '^lhs*\(.+*\)' \ | tr -d '(){' | awk -F ' ' '{ print (\$1==\"function\") ? \$2 : \$1}' | sort " - # Cache in 1 minute - lhs_peco_commandline_input "${lhs_function_list_cmd}" "${LHS_HELPFUL_LOOKUP_CACHED}" "1" + # Cache in LHS_HELPFUL_LOOKUP_FUNCTIONS_CACHED_EXPIRED_TIME setting + lhs_peco_commandline_input "${lhs_function_list_cmd}" "${LHS_HELPFUL_LOOKUP_CACHED}" "${LHS_HELPFUL_LOOKUP_FUNCTIONS_CACHED_EXPIRED_TIME}" } diff --git a/main.sh b/main.sh index 1fc6f14..20975c3 100644 --- a/main.sh +++ b/main.sh @@ -55,6 +55,8 @@ export LHS_PROJECTS_DIR=~/projects # Get all history from folder /opt/lamhaison-tools export LHS_HELPFUL_LOOKUP="${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS}/.." export LHS_HELPFUL_LOOKUP_CACHED=true +# Time for caching function suggestion menu in minutes +export LHS_HELPFUL_LOOKUP_FUNCTIONS_CACHED_EXPIRED_TIME=$((60 * 8)) # Import sub-commandline. # https://yukimemi.netlify.app/all-you-need-is-peco/ From 6363b63a7adc5c517d382f9a83b881fc4f0866cb Mon Sep 17 00:00:00 2001 From: lamhaison Date: Thu, 24 Aug 2023 09:23:39 +0700 Subject: [PATCH 20/26] [Add] - add function lhs_help_refesh for getting the menu function up-to-date --- common/help_menu.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/common/help_menu.sh b/common/help_menu.sh index 5d3f4b7..19f3cbc 100644 --- a/common/help_menu.sh +++ b/common/help_menu.sh @@ -17,3 +17,11 @@ function lhs_help_all() { CURSOR=$#BUFFER } + +function lhs_help_refresh() { + lhs_peco_disable_input_cached + lhs_peco_function_list >>/dev/null + lhs_peco_helpful_function_list >>/dev/null + lhs_peco_enable_input_cached + +} From 42bf5a64959df298b56a9ba7beda75bdeedef299 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Thu, 24 Aug 2023 09:25:21 +0700 Subject: [PATCH 21/26] [Add] - funtion for enable peco input cached --- common/peco/peco.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/peco/peco.sh b/common/peco/peco.sh index d0a9049..effa944 100644 --- a/common/peco/peco.sh +++ b/common/peco/peco.sh @@ -63,6 +63,10 @@ function lhs_peco_disable_input_cached() { export lhs_cli_peco_input_expired_time=0 } +function lhs_peco_enable_input_cached() { + export lhs_cli_peco_input_expired_time=10 +} + function lhs_peco_run_command_to_get_input() { peco_commandline=$1 eval ${peco_commandline} From a9c6179a3e7a03f20cbb44677586b2d2631ced38 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sun, 3 Sep 2023 09:02:52 +0700 Subject: [PATCH 22/26] [Add] - lhs_cmd_date_get_year function --- common/utils.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/utils.sh b/common/utils.sh index 668a7de..ec172a6 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -5,6 +5,10 @@ function lhs_cmd_date_get_month() { date +%m } + +function lhs_cmd_date_get_year() { + date +%Y +} function lhs_cmd_date_get_with_format() { echo $(date "+"${1:-"%Y-%m-%d-%H-%M-%S"}) } From 427403ce63ef5cd18c90dc2b33ab3978c0a28790 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sat, 16 Sep 2023 15:00:31 +0700 Subject: [PATCH 23/26] [Improvement] - max size of history increased more --- main.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.sh b/main.sh index 20975c3..fea24d1 100644 --- a/main.sh +++ b/main.sh @@ -30,8 +30,8 @@ LHS_CHANGE_HISTORY_SETTINGS=${3:-'True'} if [[ "${LHS_CHANGE_HISTORY_SETTINGS}" = "True" ]]; then export HISTFILE="$HOME/.zsh_history" - export HISTSIZE=10000 - export SAVEHIST=10000 + export HISTSIZE=1048576 + export SAVEHIST=1048576 # Ignore duplicates in command history and increase export HISTCONTROL=ignoredups From 3a8756ce6d249067c216a18bba09412291c82238 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Tue, 19 Sep 2023 10:56:20 +0700 Subject: [PATCH 24/26] [Add] - disable cache all (easy to use) --- common/other.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/common/other.sh b/common/other.sh index 55d4d9c..32e201e 100644 --- a/common/other.sh +++ b/common/other.sh @@ -38,3 +38,11 @@ lhs_help_create_os_user_instruction() { ' } + +function lhs_help_cache_disabled_all() { + lhs_peco_disable_input_cached + peco_aws_disable_input_cached +} + +function lhs_help_rm_caching_all() { +} From 2058371f8c7cd34215e29734f8db59c9cb626482 Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sun, 29 Oct 2023 17:26:35 +0700 Subject: [PATCH 25/26] [Improvement] - check before binding key and set history option --- common/other.sh | 5 +---- main.sh | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/common/other.sh b/common/other.sh index 32e201e..f0ce969 100644 --- a/common/other.sh +++ b/common/other.sh @@ -15,7 +15,7 @@ function lhs_help_install_macos_peco_instruction() { } -lhs_help_create_os_user_instruction() { +function lhs_help_create_os_user_instruction() { echo ' @@ -43,6 +43,3 @@ function lhs_help_cache_disabled_all() { lhs_peco_disable_input_cached peco_aws_disable_input_cached } - -function lhs_help_rm_caching_all() { -} diff --git a/main.sh b/main.sh index fea24d1..237ac44 100644 --- a/main.sh +++ b/main.sh @@ -28,7 +28,7 @@ fi LHS_CHANGE_HISTORY_SETTINGS=${3:-'True'} -if [[ "${LHS_CHANGE_HISTORY_SETTINGS}" = "True" ]]; then +if [[ "${LHS_CHANGE_HISTORY_SETTINGS}" = "True" && "$(which setopt)" != "" ]]; then export HISTFILE="$HOME/.zsh_history" export HISTSIZE=1048576 export SAVEHIST=1048576 @@ -78,7 +78,7 @@ export lhs_cli_log_uploaded_file_path="${lhs_cli_logs}/lhs-cli-uploaded.log" LHS_BIND_KEY=${2:-'True'} -if [[ "${LHS_BIND_KEY}" = "True" ]]; then +if [[ ${LHS_BIND_KEY} == "True" && "$(which zle)" != "" ]]; then # Add hot-keys zle -N lhs_peco_select_history From cc57fc87f61ed747e12c5c2b69edda55e8c5cacd Mon Sep 17 00:00:00 2001 From: lamhaison Date: Sun, 29 Oct 2023 17:34:21 +0700 Subject: [PATCH 26/26] [Fix] - fixing bug when installing by home-brew [Improvement] - update the document to make it more clear for how to use it [Improvement] - to comply shellcheck standard --- README.md | 30 ++++++++++++++-------- main.sh | 75 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 818b7a5..091b3c6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # helpful-commandlines -This is the repo to collect helpful commandlines that is used for mac and Linux OS. - +This is the repo to collect helpful commandlines that is used for MAC OS. ## Setup dependencies Notes: This document is for macos environment. @@ -37,6 +36,7 @@ jq-1.6 ## Setup lhs-helpful-commandlines ### Setup from homebrew +**It is easy to setup and run and don't want to change or optimize it** #### Install ``` brew tap lamhaison/formulae @@ -45,34 +45,42 @@ brew install lamhaison/formulae/lhs-helpful-commandlines ## Load when start an Iterm terminal Add these lines to ~/.bashrc or ~/.zshrc or ~/.bash_profile ``` -source "$(which lhs-helpful-commandlines.sh)" "/opt/homebrew/Cellar/lhs-helpful-commandlines/$(brew info lhs-helpful-commandlines | head -1 | awk -F "stable " '{print $2}')" "${HOME}" "True" - +source "$(which lhs-helpful-commandlines.sh)" "/opt/homebrew/Cellar/lhs-helpful-commandlines/$(brew info lhs-helpful-commandlines | head -1 | awk -F "stable " '{print $2}')" "True" "True" ``` -### Re-install the latest version +### Re-install the latest version(If there are new versions) ``` brew uninstall lhs-helpful-commandlines brew untap lamhaison/formulae brew tap lamhaison/formulae brew install lamhaison/formulae/lhs-helpful-commandlines +``` +## Settings when open terminal (I am using Iterm2) +**It is easy for you to custom your scripting to fix with your style** -## Settings when open terminal (I am using iterm) ``` mkdir -p /opt/lamhaison-tools && cd /opt/lamhaison-tools git clone https://github.com/lamhaison/helpful-commandlines.git echo "source /opt/lamhaison-tools/helpful-commandlines/main.sh" >> ~/.bashrc ``` -# How to search commandline +# How to use it? + +## How to search helpful commandline +``` +Ctrl + h: to and choose the commandline that you want to run(searching and enter to auto fill it to your terminal) +lhs_help_helpful cmd: only for searching, it will not automatically fill in to your terminal ``` -Ctrl + H or lhs_help_helpful -Option + R for search history commandline(As the same with sh-history-substring-search) +## How to search your history commandlines +``` +Option + r: to select the history commandline that you wan to re-run(searching and enter to auto fill it to your terminal) +search history commandline: only for searching history, it will not automatically fill in to your termial ``` -# How to enable git commit suggestions +## How to enable git commit suggestions ``` -Option + gc or lhs_git_commit_suggestions | peco +lhs_git_commit_suggestions: only for searching the list commit message pattern ``` diff --git a/main.sh b/main.sh index 237ac44..e4d23ab 100644 --- a/main.sh +++ b/main.sh @@ -21,36 +21,6 @@ else export HELPFUL_COMMANDLINES_SOURCE_SCRIPTS=${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS} fi -# Extend for poco-select-history function -# export HISTSIZE=20000 -# export SAVEHIST=15000 -# https://github.com/mattjj/my-oh-my-zsh/blob/master/history.zsh - -LHS_CHANGE_HISTORY_SETTINGS=${3:-'True'} - -if [[ "${LHS_CHANGE_HISTORY_SETTINGS}" = "True" && "$(which setopt)" != "" ]]; then - export HISTFILE="$HOME/.zsh_history" - export HISTSIZE=1048576 - export SAVEHIST=1048576 - # Ignore duplicates in command history and increase - export HISTCONTROL=ignoredups - - setopt BANG_HIST # Treat the '!' character specially during expansion. - setopt EXTENDED_HISTORY # Write the history file in the ":start:elapsed;command" format. - setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits. - setopt SHARE_HISTORY # Share history between all sessions. - setopt HIST_EXPIRE_DUPS_FIRST # Expire duplicate entries first when trimming history. - - setopt HIST_IGNORE_DUPS # Don't record an entry that was just recorded again. - setopt HIST_IGNORE_ALL_DUPS # Delete old recorded entry if new entry is a duplicate. - setopt HIST_FIND_NO_DUPS # Do not display a line previously found. - setopt HIST_IGNORE_SPACE # Don't record an entry starting with a space. - setopt HIST_SAVE_NO_DUPS # Don't write duplicate entries in the history file. - setopt HIST_REDUCE_BLANKS # Remove superfluous blanks before recording entry. - setopt HIST_VERIFY # Don't execute immediately upon history expansion. - setopt HIST_BEEP # Beep when accessing nonexistent history. -fi - export LHS_PROJECTS_DIR=~/projects # Get all history from folder /opt/lamhaison-tools export LHS_HELPFUL_LOOKUP="${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS}/.." @@ -62,20 +32,29 @@ export LHS_HELPFUL_LOOKUP_FUNCTIONS_CACHED_EXPIRED_TIME=$((60 * 8)) # https://yukimemi.netlify.app/all-you-need-is-peco/ # https://thevaluable.dev/zsh-line-editor-configuration-mouseless/ for script in $( - find ${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS} -type f -name '*.sh' | + find "${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS}" -type f -name '*.sh' | grep -v -E '.*(main.sh|test.sh|temp.sh|helpful-commandlines.sh)$' ); do - source $script + + # shellcheck disable=SC1090 + source "${script}" done export lhs_cli_peco_input_expired_time=10 export lhs_cli_show_commandline=true export lhs_cli_input=/tmp/lhs/inputs export lhs_cli_logs=/tmp/lhs/logs -mkdir -p ${lhs_cli_input} ${lhs_cli_logs} export lhs_cli_log_file_path="${lhs_cli_logs}/lhs-cli.log" export lhs_cli_log_uploaded_file_path="${lhs_cli_logs}/lhs-cli-uploaded.log" +folder_list=("${lhs_cli_input}" "${lhs_cli_logs}") +for folder in "${folder_list[@]}"; do + if [ ! -d "$folder" ]; then + mkdir -p "${folder}" + fi +done + +# Setup binding keys LHS_BIND_KEY=${2:-'True'} if [[ ${LHS_BIND_KEY} == "True" && "$(which zle)" != "" ]]; then @@ -96,3 +75,33 @@ if [[ ${LHS_BIND_KEY} == "True" && "$(which zle)" != "" ]]; then # Hotkey: Option + gc bindkey '©ç' lhs_git_commit_suggestions_with_hint fi + +# Setup for history commandlines feature +# Extend for poco-select-history function +# export HISTSIZE=20000 +# export SAVEHIST=15000 +# https://github.com/mattjj/my-oh-my-zsh/blob/master/history.zsh +LHS_CHANGE_HISTORY_SETTINGS=${3:-'True'} + +if [[ "${LHS_CHANGE_HISTORY_SETTINGS}" = "True" && "$(which setopt)" != "" ]]; then + export HISTFILE="$HOME/.zsh_history" + export HISTSIZE=1048576 + export SAVEHIST=1048576 + # Ignore duplicates in command history and increase + export HISTCONTROL=ignoredups + + setopt BANG_HIST # Treat the '!' character specially during expansion. + setopt EXTENDED_HISTORY # Write the history file in the ":start:elapsed;command" format. + setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits. + setopt SHARE_HISTORY # Share history between all sessions. + setopt HIST_EXPIRE_DUPS_FIRST # Expire duplicate entries first when trimming history. + + setopt HIST_IGNORE_DUPS # Don't record an entry that was just recorded again. + setopt HIST_IGNORE_ALL_DUPS # Delete old recorded entry if new entry is a duplicate. + setopt HIST_FIND_NO_DUPS # Do not display a line previously found. + setopt HIST_IGNORE_SPACE # Don't record an entry starting with a space. + setopt HIST_SAVE_NO_DUPS # Don't write duplicate entries in the history file. + setopt HIST_REDUCE_BLANKS # Remove superfluous blanks before recording entry. + setopt HIST_VERIFY # Don't execute immediately upon history expansion. + setopt HIST_BEEP # Beep when accessing nonexistent history. +fi