-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
72 lines (62 loc) · 1.82 KB
/
init.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
#!/usr/bin/env bash
# shellcheck disable=SC2046,SC2206
set -o errexit
export powered_by="https://github.com/jeyrce/gin-app"
export flag=".init_ok"
export build_files=(
Makefile
go.mod
)
# step: 从输入读取module配置
tmpl="$(echo $powered_by | awk -F// '{print $NF}')"
module=""
if [ -f "$flag" ]; then
echo "项目已经执行过初始化: "
cat "$flag"
exit 0
else
rm -rf .git
read -rp "请输入项目module地址($tmpl): " module
fi
if [[ ! "$module" =~ ^[a-zA-Z0-9_][-a-zA-Z0-9_]{1,62}\.[a-zA-Z0-9_][-a-zA-Z0-9_]{1,62}\/.+\/.+$ ]]; then
echo "不合法的module!"
exit 1
fi
# step: app.go 重命名
app_name="$(echo "${module//-/_}" | awk -F/ '{print $NF}' |tr '[A-Z]' '[a-z]')"
mv app.go "$app_name".go
# step: 各go文件导入路径变更 module
go_files=$(find $(pwd) -type f -name "*.go")
files=(${build_files[*]} ${go_files[*]})
for file in ${files[*]}; do
if [ -f "$file" ]; then
if [ "$(uname)" == "Linux" ]; then
sed -i "s#${tmpl}#${module}#g" "$file"
elif [ "$(uname)" == "Darwin" ]; then
sed -i "" "s#${tmpl}#${module}#g" "$file"
fi
else
echo "$file 文件不存在跳过"
fi
done
# step: 设置授权
author=$(echo "${module}" | awk -F/ '{print $2}')
year=$(date "+%Y")
if [ "$(uname)" == "Linux" ]; then
sed -i "s#2022~#${year}~#g" LICENSE
sed -i "s#Jeyrce.Lu#${author}#g" LICENSE
elif [ "$(uname)" == "Darwin" ]; then
sed -i "" "s#2022~#${year}~#g" LICENSE
sed -i "" "s#Jeyrce.Lu#${author}#g" LICENSE
fi
# step: 初始化依赖
go mod download
go install github.com/swaggo/swag/cmd/swag@latest
# step: 置空README.md
echo "$app_name" > README.md
echo "---" >> README.md
# step: 设置执行过初始化的标识
echo "Init by $powered_by" > "$flag"
date >> "$flag"
echo "项目初始化完成"
exit 0