-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindfileinclone.sh
executable file
·103 lines (83 loc) · 2.52 KB
/
findfileinclone.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
#. $TOOLS/common.sh
#enter_alt_screen
#trap exit_restore_screen INT
project_name=${PROJECT_NAME}
clone_name=${CLONE_NAME}
types_to_search_count=0
files_to_search_count=0
search_flags=""
function process_key_value_argument() {
key=$(echo $1 | cut -d '=' -f 1)
value=$(echo $1 | cut -d '=' -f 2)
if [[ $key = "type" ]]
then
types_to_search[$types_to_search_count]=$value
types_to_search_count=$(($types_to_search_count+1))
elif [[ $key = "clone-name" ]]
then
clone_name=$value
elif [[ $key = "project-name" ]]
then
project_name=$value
fi
}
function process_arguments() {
for arg in $@
do
if [[ $arg =~ ^[^=]+=[^=]+$ ]]
then
process_key_value_argument $arg
elif [[ $arg = "--exact-match" ]]
then
search_flags="$search_flags -w -o"
elif [[ $arg = "--greedy" ]]
then
greedy_find=1
elif [[ $arg =~ ^-.+$ ]]
then
search_flags="$search_flags $arg"
else
files_to_search[$files_to_search_count]=$arg
files_to_search_count=$(($files_to_search_count+1))
fi
done
[[ -z $project_name ]] && echo "Neither ENV{PROJECT_NAME} nor argument project-name=? is provided" && exit 1
[[ -z $clone_name ]] && echo "Neither ENV{CLONE_NAME} nor argument clone-name=? is provided" && exit 1
}
function find_files() {
tag_file=${HOME}/project_tags/${project_name}/${clone_name}/tags
[[ ! -f ${tag_file} ]] && echo "No tag file present. Generate tags first!" && exit 1
for i in "${files_to_search[@]}"
do
search_exp=$search_exp"$i|"
done
search_exp=${search_exp%?}
for i in "${types_to_search[@]}"
do
t_search_exp=$t_search_exp"$i|"
done
t_search_exp=${t_search_exp%?}
filtered_files=($(egrep -i $search_exp ${tag_file} | awk '{print $2}' | sort | uniq))
for line in "${filtered_files[@]}"
do
file=$(echo $line | awk -F'/' '{print $NF}')
file_type=$(echo $file | awk -F'.' '{print $NF}')
file_name=$(echo $file | awk -F'.' '{print $1}')
# Incase greedy find, search entire path
[[ -n $greedy_find ]] && file_name=$line
if [[ $types_to_search_count -ne 0 ]]
then
echo $file_type | egrep -wo $t_search_exp >&/dev/null
[[ $? -ne 0 ]] && continue
fi
echo $file_name | egrep $search_flags $search_exp >&/dev/null
[[ $? -eq 0 ]] && echo $line
done
}
#################################
## MAIN
###
process_arguments $@
find_files
#read_escape_cmd && exit_restore_screen