Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 5, 2025

  • Understand the codebase structure and existing bash-based terraform execution
  • Add terraform-exec dependency (v0.24.0)
  • Create a new terraform executor module using terraform-exec (mod/terraform.go)
  • Refactor mod/tf.go to use terraform-exec instead of bash commands
  • Test the build
  • Run code review and address feedback
  • Run security checks (CodeQL) - No issues found
  • Code optimizations:
    • Added constants for magic numbers (TerraformTimeout, ExitCodeFailure, MaxRetries, InitRetries)
    • Created notifyError() helper to reduce error notification duplication
    • Implemented retryOperation() helper for consistent retry logic
    • Simplified fallback functions by removing redundant error handling
    • Reduced code duplication across Init, Apply, Destroy, and Status functions
    • Fixed code review issues: consistent retry constants and error handling
    • Final cleanup: removed extra whitespace for consistency
  • Complete bash/deploy.sh elimination:
    • Removed all bash fallback functions (no more deploy.sh dependencies)
    • Added helper functions applyTerraformInDir and destroyTerraformInDir for multi-directory operations
    • Refactored C2Apply, C2Change, C2Destroy to use terraform-exec exclusively
    • Simplified all proxy, ASM, dnslog, and base64 functions to use terraform-exec
    • Replaced bash file operations with Go's os.ReadFile (added readFileContent helper)
    • 0 deploy.sh references remaining
    • 0 bash command calls remaining

Summary

Successfully refactored terraform execution to 100% Go-native implementation using terraform-exec library:

  • Complete bash elimination - No deploy.sh or bash command dependencies
  • Pure Go implementation - All terraform operations use terraform-exec, all file operations use Go standard library
  • Better error handling and type safety
  • Improved cross-platform compatibility
  • Cleaner, more maintainable code (~200 lines reduction)
  • Consistent retry behavior across operations
  • All builds, vet checks, and security scans passing

Key Changes

  • Removed all bash script dependencies and fallback functions
  • Implemented multi-step terraform operations (C2 scenarios) using terraform-exec
  • Replaced external commands with native Go file I/O operations
  • Unified all apply/destroy operations across different scenarios
Original prompt

This section details on the original issue you should resolve

<issue_title>使用terraform-exec进行重构</issue_title>
<issue_description>使用terraform-exec进行重构
bash的方式太不优雅了,兼容性不强
https://developer.hashicorp.com/terraform/plugin/sdkv2</issue_description>

<agent_instructions>改造代码使用terraform-exec方式进行重构</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Dec 5, 2025
Copilot AI and others added 3 commits December 5, 2025 08:05
Co-authored-by: keac <16091665+keac@users.noreply.github.com>
Co-authored-by: keac <16091665+keac@users.noreply.github.com>
Co-authored-by: keac <16091665+keac@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor code to use terraform-exec for improved compatibility Refactor terraform execution to use terraform-exec library Dec 5, 2025
Copilot AI requested a review from keac December 5, 2025 08:15
@keac
Copy link
Member

keac commented Dec 30, 2025

@copilot 继续优化代码

Copilot AI and others added 3 commits December 30, 2025 06:35
Co-authored-by: keac <16091665+keac@users.noreply.github.com>
Co-authored-by: keac <16091665+keac@users.noreply.github.com>
Co-authored-by: keac <16091665+keac@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 30, 2025

@copilot 继续优化代码

已完成代码优化。主要改进包括:

  1. 添加常量 - 定义了 TerraformTimeout、ExitCodeFailure、MaxRetries、InitRetries 等常量,消除魔术数字
  2. 减少重复代码 - 创建了 notifyError()retryOperation() 辅助函数,统一错误处理和重试逻辑
  3. 一致的重试机制 - 所有操作使用统一的常量配置重试次数
  4. 改进错误处理 - 在 tfApplyFallback 中正确处理销毁操作的错误
  5. 代码清理 - 移除多余空白,提高代码一致性

优化后代码更简洁、可维护性更强,保持了所有功能的完整性。

提交记录:

@keac
Copy link
Member

keac commented Dec 30, 2025

@copilot 进一步优化,替换deploy.sh的作用,全部采用terraform-exec实现管理

Copilot AI and others added 2 commits December 30, 2025 06:51
Co-authored-by: keac <16091665+keac@users.noreply.github.com>
…tion

Co-authored-by: keac <16091665+keac@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 30, 2025

@copilot 进一步优化,替换deploy.sh的作用,全部采用terraform-exec实现管理

已完成全面重构,完全使用 terraform-exec 实现管理,彻底移除了 deploy.sh 依赖。

主要改进:

  1. 移除所有 bash 脚本调用 - 删除了所有 deploy.sh 相关代码和 fallback 函数
  2. terraform 操作 - 所有 Init/Apply/Destroy/Status 操作纯 Go 实现
  3. 文件操作 - 使用 os.ReadFile 替代 bash cat 命令
  4. 多步骤操作 - C2Apply/Change/Destroy 使用新增的 applyTerraformInDirdestroyTerraformInDir helper 函数
  5. 简化代码 - 减少约 200 行代码,所有代理、ASM、dnslog 等函数统一调用 TfApply/TfDestroy

代码统计:

  • 0 个 deploy.sh 引用
  • 0 个 utils.Command 调用
  • 100% terraform-exec 实现

提交记录:

  • 58b86ec - 移除 bash 文件读取操作
  • 7571cfe - 移除所有 deploy.sh 依赖

Copilot AI changed the title Refactor terraform execution to use terraform-exec library Refactor terraform execution to use terraform-exec library - complete bash elimination Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

使用terraform-exec进行重构

2 participants