Skip to content

Commit 0835e08

Browse files
comchangsclaude
andcommitted
refactor: #42 split internal/deploy/deploy.go into 11 files
deploy.go (1481 lines) → 11 responsibility-based files: - deploy.go (230) — core: Deployer, Init, Upload, Install - lifecycle.go (170) — Start, StartRolling, Stop, Restart - status.go (210) — Status, StatusJSON, ListDeployments - rollback.go (70) — Rollback - logs.go (260) — multi-server/multi-module log streaming - manage.go (180) — Cleanup, Download, ThreadDump, UploadCert - provision.go (180) — Provision, provisionModule - notify.go (55) — Notify, runShell - audit.go (50) — WriteAuditLog, getGitInfo - config_expand.go (55) — expandConfigDir - health.go (80) — waitForHealthy, execHook Existing: lock.go, parallel.go, branch.go unchanged. Pure refactor — no behavior changes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 677aab2 commit 0835e08

11 files changed

Lines changed: 1340 additions & 1248 deletions

File tree

‎internal/deploy/audit.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package deploy
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"strings"
9+
"time"
10+
)
11+
12+
// WriteAuditLog writes a deployment event to a local audit log file
13+
func (d *Deployer) WriteAuditLog(envName, moduleName, action, detail string) {
14+
auditDir := ".tow"
15+
auditFile := filepath.Join(auditDir, "audit.log")
16+
17+
os.MkdirAll(auditDir, 0755)
18+
19+
currentUser := os.Getenv("USER")
20+
if currentUser == "" {
21+
currentUser = "unknown"
22+
}
23+
24+
entry := fmt.Sprintf("%s | user=%s | env=%s | module=%s | action=%s | %s\n",
25+
time.Now().Format("2006-01-02T15:04:05Z"), currentUser, envName, moduleName, action, detail)
26+
27+
f, err := os.OpenFile(auditFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
28+
if err != nil {
29+
return
30+
}
31+
defer f.Close()
32+
f.WriteString(entry)
33+
}
34+
35+
// getGitInfo returns current commit hash, branch, and last commit message
36+
func getGitInfo() (commit, branch, message string) {
37+
if out, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output(); err == nil {
38+
commit = strings.TrimSpace(string(out))
39+
}
40+
if out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output(); err == nil {
41+
branch = strings.TrimSpace(string(out))
42+
}
43+
if out, err := exec.Command("git", "log", "-1", "--format=%s").Output(); err == nil {
44+
message = strings.TrimSpace(string(out))
45+
}
46+
return
47+
}

‎internal/deploy/config_expand.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package deploy
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
// expandConfigDir copies a config directory to a temp location,
10+
// expanding ${VAR} environment variables in all text files.
11+
// This allows secrets to live in env vars instead of config files committed to git.
12+
func expandConfigDir(srcDir string) (string, error) {
13+
tmpDir, err := os.MkdirTemp("", "tow-config-*")
14+
if err != nil {
15+
return "", err
16+
}
17+
18+
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
19+
if err != nil {
20+
return err
21+
}
22+
23+
relPath, _ := filepath.Rel(srcDir, path)
24+
destPath := filepath.Join(tmpDir, relPath)
25+
26+
if info.IsDir() {
27+
return os.MkdirAll(destPath, info.Mode())
28+
}
29+
30+
// Read file
31+
data, err := os.ReadFile(path)
32+
if err != nil {
33+
return err
34+
}
35+
36+
content := string(data)
37+
38+
// Only expand if file contains ${...} patterns (skip binaries)
39+
if strings.Contains(content, "${") {
40+
content = os.ExpandEnv(content)
41+
}
42+
43+
return os.WriteFile(destPath, []byte(content), info.Mode())
44+
})
45+
46+
if err != nil {
47+
os.RemoveAll(tmpDir)
48+
return "", err
49+
}
50+
51+
return tmpDir, nil
52+
}

0 commit comments

Comments
 (0)