Skip to content

Commit e1cf7e3

Browse files
authored
Merge pull request #1 from lamhaison/develop
2 parents a1f152b + 6a3ea53 commit e1cf7e3

24 files changed

Lines changed: 676 additions & 42 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
services/tmp.sh
2+
service/temp.sh

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
# helpful-commandlines
22
This is the repo to collect helpful commandlines that is used for mac and linux os.
33

4+
45
## Setup dependencies
56
Notes: This document is for macos environment.
67

8+
### Install gitlab cli
9+
```
10+
brew install glab
11+
export GITLAB_TOKEN=xxxxx
12+
```
13+
14+
### Install Github cli
15+
```
16+
brew install gh
17+
export GH_TOKEN=xxxx
18+
```
19+
720
### Install peco
821
To allow searching by console.
922
![image](./images/peco_history_menu.png)
@@ -14,8 +27,22 @@ peco --version
1427
peco version v0.5.10 (built with go1.19.2)
1528
```
1629

30+
### Install jq
31+
```
32+
brew install jq
33+
jq --version
34+
jq-1.6
35+
```
36+
1737
## Settings when open terminal (I am using iterm)
1838
```
19-
mkdir -p /opt/lamhaison-tools
20-
git clone git@github.com:lamhaison/helpful-commandlines.git
39+
mkdir -p /opt/lamhaison-tools && cd /opt/lamhaison-tools
40+
git clone https://github.com/lamhaison/helpful-commandlines.git
2141
echo "source /opt/lamhaison-tools/helpful-commandlines/main.sh" >> ~/.bashrc
42+
```
43+
44+
# How to search commandline
45+
```
46+
Ctrl + H or lhs_help_helpful
47+
```
48+

common/help_incident.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function lamhaison_help_incident_report() {
1+
function lhs_help_incident_report() {
22
echo "
33
Reference - https://sysadmincasts.com/episodes/20-how-to-write-an-incident-report-postmortem
44
Reference - https://blog.tolleiv.de/2015/01/post-mortem-documentations-or-how-to-build-knowledge-during-failures/

common/help_ips.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
function lamhaison_help_get_public_ip() {
1+
function lhs_network_get_public_ip_instruction() {
22
echo "
33
dig +short myip.opendns.com @resolver1.opendns.com
44
"
55

66
}
7+
8+
function lhs_network_get_public_ip() {
9+
dig +short myip.opendns.com @resolver1.opendns.com
10+
}
11+
12+
function lhs_what_is_my_ip() {
13+
lhs_network_get_public_ip
14+
}
15+
16+
function lhs_network_tcp_traceroute() {
17+
18+
# https://www.redhat.com/sysadmin/traceroute-finding-meaning
19+
sudo tcptraceroute 8.8.8.8 443
20+
}

common/help_menu.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
lamhaison_help() {
2-
lamhaison_commandline=$(
3-
cat ${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS}/*/*.sh |
4-
grep -e "^function.*\(.+*\)" | tr -d "(){" | cut -d ' ' -f2 | sort | peco
1+
function lhs_help_helpful() {
2+
# Support both function function_name() { or function_name with prefix aws_bla_bla() {
3+
local lhs_functions=$(lhs_peco_helpful_function_list)
4+
BUFFER=$(
5+
echo ${lhs_functions} | peco --query "$LBUFFER"
56
)
6-
echo You can run [ which ${lamhaison_commandline:?"The commandline is unset or empty. Then do nothing"} ] to get more detail
7+
CURSOR=$#BUFFER
8+
9+
}
10+
11+
function lhs_help_all() {
12+
# Support both function function_name() { or function_name with prefix aws_bla_bla() {
13+
local lhs_functions=$(lhs_peco_function_list)
14+
BUFFER=$(
15+
echo ${lhs_functions} | peco --query "$LBUFFER"
16+
)
17+
CURSOR=$#BUFFER
18+
719
}

common/other.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
function lhs_help_install_macos_clipboard_instruction() {
4+
cat <<-_EOF_
5+
Install clipy
6+
Access Shift + command + v
7+
_EOF_
8+
}
9+
10+
function lhs_help_install_macos_peco_instruction() {
11+
cat <<-_EOF_
12+
# Install peco
13+
brew install peco
14+
_EOF_
15+
16+
}
17+
18+
function lhs_help_hotkey_sublime_search_files_instruction() {
19+
cat <<-_EOF_
20+
# Search file in sublimetext on macos
21+
⌘(Command) + P
22+
_EOF_
23+
}
24+
25+
lhs_help_create_os_user_instruction() {
26+
27+
echo '
28+
29+
visudo
30+
son.lam ALL=(ALL) ALL
31+
# Define variable
32+
user_name=son.lam
33+
sudo_password=xxxx
34+
public_key="ssh-rsa AAAAB3Nza... ${user_name}"
35+
useradd ${user_name}
36+
37+
# Sudo to root account
38+
echo -e "${user_name}\n${user_name}" | (passwd ${user_name})
39+
su lhs
40+
mkdir ~/.ssh
41+
chmod 700 ~/.ssh
42+
echo "${public_key}" > ~/.ssh/authorized_keys
43+
chmod 400 -R ~/.ssh/authorized_keys
44+
exit
45+
46+
'
47+
}

common/peco.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

common/peco/other.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
function lhs_peco_function_list() {
4+
# Ignore private function
5+
local lhs_function_list_cmd="
6+
find ${LHS_HELPFUL_LOOKUP} -type f -name '*.sh' | grep -v main.sh | xargs cat | grep -v '^function.*private.*' \
7+
| grep -e '^function.*\(.+*\)' -e '^aws*\(.+*\)' -e '^peco*\(.+*\)' -e '^lhs*\(.+*\)' \
8+
| tr -d '(){' | awk -F ' ' '{ print (\$1==\"function\") ? \$2 : \$1}' | sort
9+
"
10+
# Cache in 1 minute
11+
lhs_peco_commandline_input "${lhs_function_list_cmd}" "${LHS_HELPFUL_LOOKUP_CACHED}" "1"
12+
13+
}
14+
15+
function lhs_peco_helpful_function_list() {
16+
local lhs_function_list_cmd="
17+
find ${HELPFUL_COMMANDLINES_SOURCE_SCRIPTS} -type f -name '*.sh' | grep -v main.sh | xargs cat | grep -e '^function.*\(.+*\)' -e '^aws*\(.+*\)' -e '^peco*\(.+*\)' -e '^lhs*\(.+*\)' \
18+
| tr -d '(){' | awk -F ' ' '{ print (\$1==\"function\") ? \$2 : \$1}' | sort
19+
"
20+
21+
# Cache in 1 minute
22+
lhs_peco_commandline_input "${lhs_function_list_cmd}" "${LHS_HELPFUL_LOOKUP_CACHED}" "1"
23+
24+
}

common/peco/peco.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# brew install peco
2+
# PECO
3+
function lhs_peco_select_history() {
4+
local tac
5+
if which tac >/dev/null; then
6+
tac="tac"
7+
else
8+
# Displays the output from the end of the file in reverse order.
9+
tac="tail -r"
10+
fi
11+
BUFFER=$(history -n 1 | uniq |
12+
eval $tac |
13+
peco --query "$LBUFFER")
14+
# Move the cursor at then end of the input($#variable_name is to get the length itself)
15+
CURSOR=$#BUFFER
16+
# zle clear-screen
17+
}
18+
19+
function lhs_peco_history() {
20+
peco_select_history
21+
}
22+
23+
function lhs_peco_repo_list() {
24+
project_list=$(
25+
lhs_peco_commandline_input "\
26+
find ${LHS_PROJECTS_DIR} -type d -name '.git' -maxdepth 6 \
27+
| awk -F '/' '{for (i=1; i<NF; i++) printf \$i \"/\"; print '\n'}'" 'true' '60'
28+
)
29+
input_project=$(echo ${project_list} | peco)
30+
echo ${input_project}
31+
}
32+
33+
function lhs_peco_format_name_convention_pre_defined() {
34+
local peco_input=$1
35+
echo "${peco_input}" | tr "\t" "\n" | tr -s " " "\n" | tr -s '\n'
36+
}
37+
38+
function lhs_peco_format_output_text() {
39+
local peco_input=$1
40+
echo "${peco_input}" | tr "\t" "\n"
41+
}
42+
43+
function lhs_peco_name_convention_input() {
44+
local text_input=$1
45+
local format_text=$(lhs_peco_format_name_convention_pre_defined $text_input)
46+
echo $format_text
47+
}
48+
49+
function lhs_peco_create_menu_with_array_input() {
50+
local text_input=$1
51+
local format_text=$(lhs_peco_format_name_convention_pre_defined $text_input)
52+
echo $format_text
53+
}
54+
55+
function lhs_peco_disable_input_cached() {
56+
export lhs_cli_peco_input_expired_time=0
57+
}
58+
59+
function lhs_peco_run_command_to_get_input() {
60+
peco_commandline=$1
61+
eval ${peco_commandline}
62+
}
63+
64+
function lhs_peco_commandline_input() {
65+
66+
commandline="${1}"
67+
local result_cached=${2:-'false'}
68+
local input_expired_time="${3:-$lhs_cli_peco_input_expired_time}"
69+
70+
local md5_hash=$(echo $commandline | md5)
71+
local input_folder="${lhs_cli_input:-/tmp/inputs}"
72+
mkdir -p ${input_folder}
73+
local input_file_path="${input_folder}/${md5_hash}.txt"
74+
local empty_file=$(find ${input_folder} -name ${md5_hash}.txt -empty)
75+
local valid_file=$(find ${input_folder} -name ${md5_hash}.txt -mmin +${input_expired_time})
76+
77+
# The file is existed and not empty and the flag result_cached is not empty
78+
if [ -z "${valid_file}" ] && [ -f "${input_file_path}" ] && [ -z "${empty_file}" ] && [ "true" = "${result_cached}" ]; then
79+
# Ignore the first line.
80+
grep -Ev "\*\*\*\*\*\*\*\* \[.*\]" $input_file_path
81+
# cat $input_file_path |
82+
else
83+
local commandline_result=$(lhs_peco_run_command_to_get_input "$commandline")
84+
85+
local format_text=$(lhs_peco_format_output_text $commandline_result)
86+
87+
if [ -n "${format_text}" ]; then
88+
commandline=$(lhs_util_format_commandline_one_line ${commandline})
89+
echo "******** [ ${commandline} ] ********" >${input_file_path}
90+
echo ${format_text} | tee -a ${input_file_path}
91+
else
92+
echo "Can not get the data"
93+
fi
94+
95+
fi
96+
97+
}
98+
99+
function lhs_peco_create_menu() {
100+
local input_function=$1
101+
local peco_options=$2
102+
local peco_command="peco ${peco_options}"
103+
local input_value=$(echo "$(eval $input_function)" | eval ${peco_command})
104+
echo ${input_value:?'Can not get the input from peco menu'}
105+
}

common/peco/peco_git.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
function lhs_peco_git_diff_name_only() {
4+
lhs_peco_commandline_input 'git diff --name-only'
5+
}

0 commit comments

Comments
 (0)