feat: 代码标准化和文件重命名
## 新增功能 - 创建统一的公共函数库 (common/logging.sh, common/error_handler.sh) - 添加功能演示脚本 (common/demo_usage.sh) - 完善的使用文档 (common/README.md) ## 代码重构 - 重构所有脚本使用统一的公共库 - 为所有函数添加完整的文档注释 - 统一代码格式(4空格缩进、严格模式) - 标准化错误处理和日志输出 ## 文件重命名 - gcp/create_ai_project.sh → gcp/create_ai_projects.sh (单复数统一) - gcp/delete_all_project.sh → gcp/delete_all_projects.sh (单复数统一) - linux/install_ohmyzsh.sh → linux/install_oh_my_zsh.sh (专有名词规范) - linux/create_raid0_with_ext4.sh → linux/create_raid0_array.sh (简化命名) - common/example.sh → common/demo_usage.sh (更具描述性) ## 技术改进 - 使用 readonly 声明常量 - 启用 set -euo pipefail 严格模式 - 统一的 ANSI 颜色日志输出 - 完善的命令重试机制 - 栈追踪支持
This commit is contained in:
179
common/demo_usage.sh
Executable file
179
common/demo_usage.sh
Executable file
@@ -0,0 +1,179 @@
|
||||
#!/bin/bash
|
||||
# ============================================================================
|
||||
# 文件名: demo_usage.sh
|
||||
# 描述: 公共函数库功能演示和使用示例
|
||||
# 作者: Cloud Tools Project
|
||||
# 版本: 1.0.0
|
||||
# ============================================================================
|
||||
|
||||
set -euo pipefail # 启用严格模式
|
||||
|
||||
# 获取脚本目录
|
||||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# 加载公共库
|
||||
source "${SCRIPT_DIR}/logging.sh"
|
||||
source "${SCRIPT_DIR}/error_handler.sh"
|
||||
|
||||
#
|
||||
# 示例 1: 基本日志使用
|
||||
#
|
||||
example_basic_logging() {
|
||||
log_info "============ 示例 1: 基本日志使用 ============"
|
||||
|
||||
log_debug "这是调试信息(默认不显示)"
|
||||
log_info "这是普通信息"
|
||||
log_warning "这是警告信息"
|
||||
log_error "这是错误信息(但不会退出)"
|
||||
log_success "这是成功信息"
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
#
|
||||
# 示例 2: 日志级别控制
|
||||
#
|
||||
example_log_levels() {
|
||||
log_info "============ 示例 2: 日志级别控制 ============"
|
||||
|
||||
log_info "当前日志级别: INFO"
|
||||
log_debug "这条调试信息不会显示"
|
||||
|
||||
log_info "切换到 DEBUG 级别..."
|
||||
log_set_level "DEBUG"
|
||||
log_debug "现在调试信息可以显示了!"
|
||||
|
||||
log_info "恢复到 INFO 级别..."
|
||||
log_set_level "INFO"
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
#
|
||||
# 示例 3: 错误检查
|
||||
#
|
||||
example_error_checking() {
|
||||
log_info "============ 示例 3: 错误检查 ============"
|
||||
|
||||
# 临时禁用自动退出
|
||||
disable_exit_on_error
|
||||
|
||||
# 检查命令
|
||||
if check_command "bash"; then
|
||||
log_success "bash 命令存在"
|
||||
fi
|
||||
|
||||
if ! check_command "nonexistent_command"; then
|
||||
log_warning "nonexistent_command 不存在(这是预期的)"
|
||||
fi
|
||||
|
||||
# 检查文件
|
||||
if check_file "${SCRIPT_DIR}/logging.sh"; then
|
||||
log_success "logging.sh 文件存在"
|
||||
fi
|
||||
|
||||
# 检查变量非空
|
||||
local test_var="hello"
|
||||
if check_not_empty "$test_var" "test_var"; then
|
||||
log_success "test_var 变量非空"
|
||||
fi
|
||||
|
||||
# 重新启用自动退出
|
||||
enable_exit_on_error
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
#
|
||||
# 示例 4: 命令执行和重试
|
||||
#
|
||||
example_command_execution() {
|
||||
log_info "============ 示例 4: 命令执行 ============"
|
||||
|
||||
# 执行简单命令
|
||||
run_command "列出文件失败" ls -la "${SCRIPT_DIR}"
|
||||
|
||||
log_info "执行成功的命令"
|
||||
|
||||
# 临时禁用自动退出来演示重试
|
||||
disable_exit_on_error
|
||||
|
||||
log_info "尝试执行一个会失败的命令(演示重试)"
|
||||
retry_command 3 1 "命令失败" false
|
||||
|
||||
# 重新启用
|
||||
enable_exit_on_error
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
#
|
||||
# 示例 5: 日志文件输出
|
||||
#
|
||||
example_log_file() {
|
||||
log_info "============ 示例 5: 日志文件输出 ============"
|
||||
|
||||
local temp_log="/tmp/common_lib_example_$$.log"
|
||||
|
||||
log_info "设置日志输出到文件: $temp_log"
|
||||
log_set_file "$temp_log"
|
||||
|
||||
log_info "这条消息会同时输出到控制台和文件"
|
||||
log_success "日志文件功能正常"
|
||||
|
||||
log_info "日志文件内容:"
|
||||
cat "$temp_log"
|
||||
|
||||
# 清理
|
||||
rm -f "$temp_log"
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
#
|
||||
# 示例 6: 时间戳控制
|
||||
#
|
||||
example_timestamp() {
|
||||
log_info "============ 示例 6: 时间戳控制 ============"
|
||||
|
||||
log_info "默认启用时间戳"
|
||||
|
||||
log_info "禁用时间戳..."
|
||||
log_disable_timestamp
|
||||
log_info "这条消息没有时间戳"
|
||||
|
||||
log_info "启用时间戳..."
|
||||
log_enable_timestamp
|
||||
log_info "时间戳已恢复"
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
#
|
||||
# 主函数
|
||||
#
|
||||
main() {
|
||||
# 配置日志
|
||||
log_set_level "INFO"
|
||||
log_enable_timestamp
|
||||
|
||||
log_success "==================================================="
|
||||
log_success " 公共函数库使用示例"
|
||||
log_success "==================================================="
|
||||
echo ""
|
||||
|
||||
# 执行所有示例
|
||||
example_basic_logging
|
||||
example_log_levels
|
||||
example_error_checking
|
||||
example_command_execution
|
||||
example_log_file
|
||||
example_timestamp
|
||||
|
||||
log_success "==================================================="
|
||||
log_success " 所有示例执行完成!"
|
||||
log_success "==================================================="
|
||||
}
|
||||
|
||||
# 执行主函数
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user