docs: files, proxy, string

This commit is contained in:
hophacker 2022-05-06 18:21:18 +08:00 committed by hophacker
parent 5ad80a8617
commit bf83e02431
6 changed files with 153 additions and 166 deletions

View File

@ -21,10 +21,6 @@ python3 -m pip install --user --upgrade pynvim
```sh
bash -c "`curl -fsSL https://raw.githubusercontent.com/ruilisi/dotar/master/install.sh`"
```
Or
```sh
bash -c "`curl -fsSL https://xiemala.com/ruilisi/dotar/raw/branch/master/install.sh`"
```
## Upgrade
```sh
@ -34,35 +30,34 @@ rake update
```
# Modules
## ZSH
* ZSH
* [Prezto - the zsh behind YADR's](http://github.com/sorin-ionescu/prezto)
* [How to add your own ZSH theme](doc/zsh/themes.md)
#### General Commands
- `Replace`
- `Replace s SOURCE_TEXT -d DESTINATION_TEXT -f FILE_REGEX_PATTERH --separator=SEPERATOR`
- By default, `FILE_REGEX_PATTERN` is `.*`, SEPERATOR` is `;`
- Example: `Replace -s "/api/rule" -d "/dashboard/api/rule" -f ".*.ts"`
- `secure_source` -source `~/.yadr/zsh/function.zsh` file, let `function.zsh` modify work immediately in current window
- `pp $1` -useage:`pp xx`, grep process by name
- `set_proxy` -set terminal proxy use http proxy
- `set_vagrant_proxy` -set terminal proxy use vagrant http proxy
- `set_ss_proxy` -set terminal proxy use socks proxy
- `unset_proxy` -cancel all proxy
- `post $1 $2` -curl POST with application/json
- `git-set-remote $1` -set project git remote url
- `gem_source_to_taobao` -set ruby gem source to taobao
- `docker_rm_all` -delete all docker images
- `Replace $1 $2` -replcae all text under current path
- `swap $1 $2` -`swap file1 file2`
- `init_db` -init rails project database
- `kexec` -execute k8s pod by regex pod name
- `klog` -show k8s pod log by regex pod name
- `git_tag_delete` -delete gtihub tag
- `git_tag_add` -add gtihub tag
- `dc` -alias of docker-compose
## Zsh Commands
#### [Files](./zsh/files.zsh)
Command | Args | Description
:--------|:------|:-----------
swap | F1 F2 | swap file F1 with file F2
Replace |[doc](#Replace) |Replace text recursively
#### Editing Commands
<a name="Replace">
Replace
</a>options:
```sh
-f File regex pattern
-s Source pattern
-d Destination pattern
-r Remove line
--regex Match pattern with regex
--seperator= Seperator, # by default
-h Display this message"
```
#### Editing
Shortcut | Alias
:-----------|:--------
Ctrl-R | Vim mode and bash style historical anti-query
@ -70,14 +65,14 @@ ae | Edit alias
ar | Reload alias
ESC C-x C-e |Edit current command line in vim
#### Network tools
#### Network
Command | Description
:-----------|:--------
test-port PORT | test whether PORT is opened
intercept-request-hosts | intercept requests and show hosts
host-ip | show host ip of your system
#### System commands
#### System
Command | Description
:-----------|:--------
yell | print the script name and all arguments to stderr
@ -85,13 +80,14 @@ die | does the same as yell, but exits with a non-0 exit status, which m
try | uses the || (boolean OR), which only evaluates the right side if the left one didnt fail.
list-large-files DIR | list large files sort by reversed order of size and print size in the order of `KB`, `MB`, `GB`
#### String commands
#### String
Command | Description
:------------|:--------
random-hex | print random hex
random-string| print random string of alphabets `a-zA-Z0-9`
contains |str1 str2 ... target |test whether a target string is included in an array of strings
#### Git Commands
#### Git
YADR will take over your ~/.gitconfig, so if you want to store your git username and other settings, please put them in ~/.gitconfig.user
We recommend setting your user information in this file. In addition, you can set your environment variables appropriately in your ~/.secrets.
@ -121,6 +117,21 @@ Command | Alias
`gsp` |`git stash pop`
`gst` |`git stash`
#### Other
- `secure_source` -source `~/.yadr/zsh/function.zsh` file, let `function.zsh` modify work immediately in current window
- `pp $1` -useage:`pp xx`, grep process by name
- `set_proxy` -set terminal proxy use http proxy
- `set_vagrant_proxy` -set terminal proxy use vagrant http proxy
- `set_ss_proxy` -set terminal proxy use socks proxy
- `unset_proxy` -cancel all proxy
- `post $1 $2` -curl POST with application/json
- `docker_rm_all` -delete all docker images
- `kexec` -execute k8s pod by regex pod name
- `klog` -show k8s pod log by regex pod name
- `git_tag_delete` -delete gtihub tag
- `git_tag_add` -add gtihub tag
- `dc` -alias of docker-compose
## [fasd](https://github.com/clvv/fasd)
The name fasd comes from the default suggested aliases f(files), a(files/directories), s(show/search/select), d(directories).

View File

@ -29,7 +29,7 @@ done
if [ ! -d "$HOME/.yadr" ]; then
echo "Installing YADR for the first time"
git clone --depth=1 https://github.com/ruilisi/dotfiles.git "$HOME/.yadr"
git clone --depth=1 https://github.com/ruilisi/dotar.git "$HOME/.yadr"
cd "$HOME/.yadr"
[ "$1" = "ask" ] && export ASK="true"
rake install

78
zsh/files.zsh Normal file
View File

@ -0,0 +1,78 @@
swap() {
if [ $# -ne 2 ]; then
echo "Usage: swap file1 file2"
else
local TMPFILE=$(mktemp)
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
fi
}
# Replace replaces non-regex pattern recursively
# Example: Replace 'ctx.Status(400)' "ctx.Status(http.StatusBadRequest)"
Replace () {
CMD=$0
function usage ()
{
echo "Usage : $CMD [options] [--]
Options:
-f File regex pattern
-s Source pattern
-d Destination pattern
-r Remove line
--regex Match pattern with regex
--seperator= Seperator, # by default
-h Display this message"
}
FILE_REGEX='.*'
SRC=""
DST=""
SEP=";"
DEBUG=false
REMOVE=false
REGEX=false
while getopts ":rhf:s:d:-:" opt
do
case "${opt}" in
-)
echo $OPTART
case "${OPTARG}" in
debug)
DEBUG=true
;;
regex)
REGEX=true
;;
seperator=*)
val=${OPTARG#*=}
SEP=$val
;;
*)
echo "Unknown option --${OPTARG}"
;;
esac;;
f) FILE_REGEX=$OPTARG ;;
r) REMOVE=true ;;
s) SRC=$OPTARG ;;
d) DST=$OPTARG ;;
h) usage; return 0 ;;
*) echo -e "\n option does not exist : $OPTARG\n";
usage; return 1 ;;
esac
done
SEARCH_CMD="ag `$REGEX || echo -Q` \"$SRC\" -l -G \"$FILE_REGEX\""
MATCHED_FILES=`eval "$SEARCH_CMD"`
echo "Replace in current files:$fg[green]\n$MATCHED_FILES$reset_color"
if $REMOVE; then
SED_CMD=\\${SEP}$SRC${SEP}d
else
SED_CMD=s${SEP}$SRC${SEP}$DST${SEP}g
fi
if [[ "$(uname)" == "Darwin" ]]; then
echo "xargs sed -i '' \"${SED_CMD}\""
echo $MATCHED_FILES | xargs sed -i '' "${SED_CMD}"
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
echo $MATCHED_FILES | xargs sed -i ${SED_CMD}
fi
}

View File

@ -5,18 +5,6 @@ function secure_source () {
source $1
fi
}
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
}
echo "n"
return 1
}
pp() { ps aux | grep "$1\|USER" | grep -v "grep" }
function getIP () {
echo $(grep $1 ~/.ssh/config -A 1 | tail -1 | tr -s ' ' | cut -d ' ' -f 3)
@ -30,50 +18,12 @@ function ssh_exec_by_file () {
ssh -t $1 "bash -s" -- < $2
}
function cp_container() {
sourceName=$1
targetName=$2
cp $sourceName $targetName -r
cd $targetName
mv ${sourceName}.scss ${targetName}.scss
mv ${sourceName}.js ${targetName}.js
cd ..
}
function set_proxy() {
export http_proxy=http://127.0.0.1:8668;export https_proxy=http://127.0.0.1:8668;
}
function set_vagrant_proxy() {
export http_proxy=http://127.0.0.1:9119;export https_proxy=http://127.0.0.1:9119;
}
function set_ss_proxy() {
export https_proxy=socks5://127.0.0.1:1080/
export http_proxy=socks5://127.0.0.1:1080/
}
function unset_proxy() {
unset all_proxy
unset http_proxy
unset https_proxy
}
function post {
curl -H "Content-Type: application/json" -X POST -d $1 $2
}
function git-set-remote {
git remote rm origin
git remote rm upstream
git remote add origin $1
git remote add upstream $1
}
function search_installed_packages {
dpkg --get-selections | grep $1
}
function delete_packages {
sudo apt remove `_search_installed_packages $1 | cut -f 1 | tr "\n" " "`
}
function gem_source_to_taobao {
gem source -r https://rubygems.org/
gem source -a https://ruby.taobao.org
}
function set_anonymous {
git filter-branch --env-filter '
if [ "$GIT_AUTHOR_NAME" = "ralletstellar" ]; then \
@ -95,95 +45,15 @@ function strip_color() {
function docker_rm_all() {
docker rm -f `docker ps --no-trunc -aq`
}
# Replace replaces non-regex pattern recursively
# Example: Replace 'ctx.Status(400)' "ctx.Status(http.StatusBadRequest)"
function Replace () {
CMD=$0
function usage ()
{
echo "Usage : $CMD [options] [--]
Options:
-f File regex pattern
-s Source pattern
-d Destination pattern
-r Remove line
--regex Match pattern with regex
--seperator= Seperator, # by default
-h Display this message"
}
FILE_REGEX='.*'
SRC=""
DST=""
SEP=";"
DEBUG=false
REMOVE=false
REGEX=false
while getopts ":rhf:s:d:-:" opt
do
case "${opt}" in
-)
echo $OPTART
case "${OPTARG}" in
debug)
DEBUG=true
;;
regex)
REGEX=true
;;
seperator=*)
val=${OPTARG#*=}
SEP=$val
;;
*)
echo "Unknown option --${OPTARG}"
;;
esac;;
f) FILE_REGEX=$OPTARG ;;
r) REMOVE=true ;;
s) SRC=$OPTARG ;;
d) DST=$OPTARG ;;
h) usage; return 0 ;;
*) echo -e "\n option does not exist : $OPTARG\n";
usage; return 1 ;;
esac
done
SEARCH_CMD="ag `$REGEX || echo -Q` \"$SRC\" -l -G \"$FILE_REGEX\""
MATCHED_FILES=`eval "$SEARCH_CMD"`
echo "Replace in current files:$fg[green]\n$MATCHED_FILES$reset_color"
if $REMOVE; then
SED_CMD=\\${SEP}$SRC${SEP}d
else
SED_CMD=s${SEP}$SRC${SEP}$DST${SEP}g
fi
if [[ "$(uname)" == "Darwin" ]]; then
echo "xargs sed -i '' \"${SED_CMD}\""
echo $MATCHED_FILES | xargs sed -i '' "${SED_CMD}"
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
echo $MATCHED_FILES | xargs sed -i ${SED_CMD}
fi
}
function git-change-module-remote() {
git config --file=.gitmodules submodule.$1.url $2
git config --file=.gitmodules submodule.$1.branch $3
git submodule sync
git submodule update --init --recursive --remote
}
function replace() {
ag -l -G $1 | xargs sed -ri.bak -e "s/$2/$3/g"
}
function markdown-preview() {
cat $1 | instant-markdown-d > /dev/null 2>&1
}
function swap() {
if [ $# -ne 2 ]; then
echo "Usage: swap file1 file2"
else
local TMPFILE=$(mktemp)
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
fi
}
function cmd_exists() {
$* &> /dev/null
if [[ $? == 0 ]]; then

15
zsh/proxy.zsh Normal file
View File

@ -0,0 +1,15 @@
set_proxy() {
export http_proxy=http://127.0.0.1:8668;export https_proxy=http://127.0.0.1:8668;
}
set_vagrant_proxy() {
export http_proxy=http://127.0.0.1:9119;export https_proxy=http://127.0.0.1:9119;
}
set_ss_proxy() {
export https_proxy=socks5://127.0.0.1:1080/
export http_proxy=socks5://127.0.0.1:1080/
}
unset_proxy() {
unset all_proxy
unset http_proxy
unset https_proxy
}

View File

@ -5,3 +5,16 @@ random-hex() {
random-string() {
cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1
}
contains() {
local n=$#
local value=$@[n]
for ((i=1;i < $#;i++)) {
if [[ $@[i] == ${value} ]]; then
echo "yes"
return 0
fi
}
echo "no"
return 1
}