forked from skwp/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
files.zsh
78 lines (77 loc) · 1.93 KB
/
files.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
}