-
Notifications
You must be signed in to change notification settings - Fork 1
/
tidy.sh
executable file
·69 lines (64 loc) · 1.28 KB
/
tidy.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
#!/usr/bin/env bash
###########################################################
#Author:mengdj@outlook.com
#Created Time:2020.12.04 11:56
#Description:execute go mod tidy in current directory
#Version:0.0.5
#File:tidy.sh
###########################################################
CURRENT_DIR=$(pwd)
SEARCH_DIR=$CURRENT_DIR
SEARCH_TOTAL=0
EXECUTE_CMD="$GOROOT/bin/go mod tidy"
EXECUTE_TIMESTAMP=`date +%s`
EXECUTE_FIFO="$$.fifo"
EXECUTE_MAX_PROCESS=4
function GoTidy() {
for file in $(ls $1); do
local target="$1/$file"
if [ -d $target ]; then
cd $target
#case
if [ -f "go.mod" ];then
read -u 6
{
`$EXECUTE_CMD`
#revert data to pipe
echo >&6
} &
if [ $? -ne 0 ]; then
break
fi
echo "process $target"
let "SEARCH_TOTAL+=1"
fi
GoTidy $target
fi
done
}
#test
if [ $# -ne 0 ]; then
if [ -d $1 ]; then
cd $1
SEARCH_DIR=$(pwd)
else
echo "$1 is not exist directory."
exit
fi
fi
#start
mkfifo $EXECUTE_FIFO
#alias file description
exec 6<> $EXECUTE_FIFO
rm -rf $EXECUTE_FIFO
for i in `seq $EXECUTE_MAX_PROCESS`;do
echo >&6
done
GoTidy $SEARCH_DIR
wait
#close fifo
exec 6<&-
let "EXECUTE_TIMESTAMP=`date +%s`-EXECUTE_TIMESTAMP"
echo "processed($SEARCH_TOTAL),loss $EXECUTE_TIMESTAMP seconds."
#back directory
cd $CURRENT_DIR