Skip to content

Commit

Permalink
Fix automation script path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzm0809 committed Dec 9, 2024
1 parent 5a777bd commit 30e2bc5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 31 deletions.
64 changes: 33 additions & 31 deletions script/bin/auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# debug mode
#set -x


export RED='\033[31m'
export GREEN='\033[32m'
export YELLOW='\033[33m'
Expand Down Expand Up @@ -93,20 +94,14 @@ assertIsInputVersion() {
fi
}

# 从 application.yml 中读取端口号
read_yaml() {
local key=$1
local file=$2
local value=$(awk -F': ' '/^'"$key"'=: /{print $2}' $file)
echo "$value"
}

# 尝试从 application.yml 中读取端口号
if [ -f "${APP_HOME}/config/application.yml" ]; then
APP_PORT=$(read_yaml "server.port" "${APP_HOME}/config/application.yml")
result=$("${APP_HOME}"/bin/parse_yml.sh "${APP_HOME}/config/application.yml" "server.port")
APP_PORT=$result
fi

# 如果仍然没有找到,则使用默认端口
echo -e "${GREEN}From ${APP_HOME}/config/application.yml server.port: ${APP_PORT}${RESET}"

if [ -z "$APP_PORT" ]; then
echo -e "${RED}Could not find server.port in configuration files, using default port 8888 ${RESET}"
APP_PORT=8888
Expand All @@ -117,28 +112,35 @@ check_health() {
curl --silent --max-time 2 --output /dev/null --write-out "%{http_code}" "http://localhost:$APP_PORT/actuator/health"
}


function wait_start_process() {
echo "Starting application..."
for i in {1..100}; do
# 检查应用是否已经启动完成
if [ "$(check_health)" == "200" ]; then
echo -ne "\r[==================================================] 100%\n"
echo "Application started successfully."
break
else
# 打印进度条
echo -ne "\r[=$(printf '=%.0s' $(seq 1 $((i-1))))>$(printf ' %.0s' $(seq 1 $((100-i))))] ${i}%"
sleep 0.5 # 调整等待时间
fi

# 防止无限循环,设置最大尝试次数
if [ $i -eq 100 ]; then
echo -ne "\r[==================================================] 100%\n"
echo "Application startup timed out."
exit 1
fi
done
echo "Starting application..."
local max_attempts=100
local attempt=0
local delay=0.25
local health_status=""
local success_status_codes=("200" "201" "202" "203" "204") # 可以根据实际情况调整认为成功的状态码范围

while [ $attempt -lt $max_attempts ]; do
attempt=$((attempt + 1))
health_status=$(check_health)
for code in "${success_status_codes[@]}"; do
if [ "$health_status" == "$code" ]; then
echo -ne "\r[==================================================] 100%\n"
echo -e "${GREEN}Application started completed.${RESET}"
return 0
fi
done
local progress=$((attempt * 100 / max_attempts))
local bar_length=50
local filled_length=$((progress * bar_length / 100))
local empty_length=$((bar_length - filled_length))
local bar=$(printf '=%.0s' $(seq 1 $filled_length))$(printf ' %.0s' $(seq 1 $empty_length))
echo -ne "\r[${bar}] ${progress}%"
sleep $delay
done
echo -ne "\r[==================================================] 100%\n"
echo "Application startup timed out."
return 1
}


Expand Down
65 changes: 65 additions & 0 deletions script/bin/parse_yml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

# 检查参数个数是否正确
if [ $# -ne 2 ]; then
echo "请按正确格式输入参数,格式为: $0 yaml_file_path key"
exit 1
fi

#yaml_file_path="/Users/zhumingye/idea_works/dinky/dinky-admin/src/main/resources/application.yml"
#key="spring.port"

yaml_file_path="$1"
key="$2"
# 将键按照.分割为数组(用于处理层级结构的键)
IFS='.' read -ra key_parts <<< "$key"

# 用于存储最终找到的值
value=""

# 先处理文件,去除注释行(假设注释以#开头)和空白行
temp_file=$(mktemp)
grep -Ev '^(#|$)' "$yaml_file_path" > "$temp_file"

# 开始按层级查找值
current_data=$(cat "$temp_file")
for part in "${key_parts[@]}"; do
found=false
while IFS= read -r line; do

if [[ $line =~ ^$part: ]]; then
# 如果是最后一个键,提取值
if [ "$part" == "${key_parts[${#key_parts[@]}-1]}" ]; then
value=$(echo "$line" | sed 's/.*: //')
found=true
break
else
# 如果不是最后一个键,获取下一层级的数据范围
start_line_num=$(grep -n "$line" "$temp_file" | cut -d: -f1)
end_line_num=$(awk -v start="$start_line_num" '$0 ~ /^[a-zA-Z]/ && NR > start {print NR - 1; exit}' "$temp_file")
if [ -z "$end_line_num" ]; then
end_line_num=$(wc -l < "$temp_file")
fi

current_data=$(sed -n "$((start_line_num + 1)),$((end_line_num))p" "$temp_file")
current_data=$(echo "$current_data" | sed 's/^[[:space:]]*//')
found=true
break
fi
fi
done <<< "$current_data"
if [ "$found" = false ]; then
value=""
break
fi
done

# 删除临时文件
rm -f "$temp_file"

# 将找到的值输出到标准输出,方便外部获取
if [ -n "$value" ]; then
echo "$value"
else
echo ""
fi

0 comments on commit 30e2bc5

Please sign in to comment.