diff --git a/README.md b/README.md index 7e3e9d6..11baf25 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. @@ -11,7 +10,7 @@ brew install glab export GITLAB_TOKEN=xxxxx ``` -### Install Github cli +### Install GitHub cli ``` brew install gh export GH_TOKEN=xxxx @@ -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,11 +45,10 @@ 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 @@ -57,22 +56,30 @@ brew tap lamhaison/formulae brew install lamhaison/formulae/lhs-helpful-commandlines ``` -## Settings when open terminal (I am using iterm) +## Settings when open terminal (I am using Iterm2) +**It is easy for you to custom your scripting to fix with your style** + ``` 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 -``` -Ctrl + H for search list commandline -Option + R for search history +# 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 ``` -# How to enable git commit suggestions +## How to search your history commandlines ``` -Option + gc or lhs_git_commit_suggestions | peco +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 +``` +lhs_git_commit_suggestions: only for searching the list commit message pattern +``` \ No newline at end of file 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/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 + +} diff --git a/common/other.sh b/common/other.sh index d5ba581..f0ce969 100644 --- a/common/other.sh +++ b/common/other.sh @@ -15,14 +15,7 @@ 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() { +function lhs_help_create_os_user_instruction() { echo ' @@ -45,3 +38,8 @@ lhs_help_create_os_user_instruction() { ' } + +function lhs_help_cache_disabled_all() { + lhs_peco_disable_input_cached + peco_aws_disable_input_cached +} diff --git a/common/peco/other.sh b/common/peco/other.sh index 365a377..410c836 100644 --- a/common/peco/other.sh +++ b/common/peco/other.sh @@ -3,18 +3,24 @@ 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 " - # 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}" } 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 " diff --git a/common/peco/peco.sh b/common/peco/peco.sh index bf94d42..effa944 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 @@ -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${input_file_path} echo ${format_text} | tee -a ${input_file_path} else diff --git a/common/utils.sh b/common/utils.sh index 870afcd..ec172a6 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -1,16 +1,44 @@ #!/bin/bash # Get DateTime -function lhs_date_get_with_format() { + +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"}) } -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" +} + +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 +# +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} } # Password generate -function lhs_password_generate() { +function lhs_cmd_password_generate() { # openssl rand -base64 10 | tr -d '=' cd /tmp >/dev/null @@ -19,7 +47,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 @@ -44,7 +72,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 @@ -75,25 +103,30 @@ 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 log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log + local tee_command="tee -a ${lhs_cli_log_file_path}" + local eval_commandline=${2:-'False'} - 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 "${lhs_commandline_logging}" + echo "${local_lhs_commandline_logging}" else - echo "Running commandline [ ${lhs_commandline_logging} ]" + echo "Running commandline [ ${local_lhs_commandline_logging} ]" | eval $tee_command 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}" @@ -108,18 +141,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/main.sh b/main.sh index 37e87de..e4d23ab 100644 --- a/main.sh +++ b/main.sh @@ -21,65 +21,49 @@ 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" ]]; then - export HISTFILE="$HOME/.zhistory" - export HISTSIZE=10000 - export SAVEHIST=10000 - # 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}/.." 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/ # 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 + 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" ]]; then +if [[ ${LHS_BIND_KEY} == "True" && "$(which zle)" != "" ]]; 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 @@ -91,3 +75,33 @@ if [[ "${LHS_BIND_KEY}" = "True" ]]; 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 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 6cd7237..393278f 100644 --- a/services/docker.sh +++ b/services/docker.sh @@ -6,6 +6,30 @@ # ## +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_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 @@ -66,3 +90,47 @@ 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() { + 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() { + + local lhs_docs=$( + cat <<-__EOF__ + apk update + apk add busybox-extras + __EOF__ + ) + + echo "$lhs_docs" +} 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 }