Skip to content

Commit

Permalink
[Fix] - merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
lamhaison committed Oct 29, 2023
2 parents 19b1388 + cc57fc8 commit 748670d
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 111 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,7 +10,7 @@ brew install glab
export GITLAB_TOKEN=xxxxx
```

### Install Github cli
### Install GitHub cli
```
brew install gh
export GH_TOKEN=xxxx
Expand All @@ -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
Expand All @@ -45,34 +45,41 @@ 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 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
```
4 changes: 0 additions & 4 deletions common/help_ips.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions common/help_menu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
14 changes: 6 additions & 8 deletions common/other.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand All @@ -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
}
14 changes: 10 additions & 4 deletions common/peco/other.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
"

Expand Down
24 changes: 20 additions & 4 deletions common/peco/peco.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<NF; i++) printf \$i \"/\"; print '\n'}'" 'true' '60'
)
input_project=$(echo ${project_list} | peco)
local final_projects=$(
cat <<-__EOF__
${LHS_TOOLS_DIR}
${project_list}
__EOF__
)

input_project=$(echo ${final_projects} | peco)
echo ${input_project}
}

Expand Down Expand Up @@ -56,17 +63,26 @@ 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}
}

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}
Expand All @@ -85,7 +101,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
Expand Down
71 changes: 52 additions & 19 deletions common/utils.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -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 ' '
}
Loading

0 comments on commit 748670d

Please sign in to comment.