projects.go
93 lines1
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package internal
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"congo.gg/dev/models"
)
// CreateProject runs `congo init` to scaffold a new project in the repos directory.
// Flags are passed directly to congo init (e.g., "--no-frontend", "--no-assistant").
func CreateProject(name string, flags []string) error {
if name == "" {
return fmt.Errorf("project name is required")
}
existing, _ := models.Repositories.First("WHERE Name = ?", name)
if existing != nil {
return fmt.Errorf("a repository named '%s' already exists", name)
}
reposPath := hostReposDir()
targetDir := filepath.Join(reposPath, name)
if _, err := os.Stat(targetDir); err == nil {
return fmt.Errorf("directory %s already exists", name)
}
os.MkdirAll(reposPath, 0755)
// Run congo init in this container (has the congo binary)
args := append([]string{"init", name}, flags...)
cmd := exec.Command("congo", args...)
cmd.Dir = reposPath
output, err := cmd.CombinedOutput()
if err != nil {
msg := strings.TrimSpace(string(output))
if msg != "" {
return fmt.Errorf("congo init failed: %s", msg)
}
return fmt.Errorf("congo init failed: %w", err)
}
// Fix ownership so code-server (uid 1000) can access the files
if err := exec.Command("chown", "-R", "1000:1000", targetDir).Run(); err != nil {
return fmt.Errorf("chown failed: %w", err)
}
// Initialize git repo via code-server so SyncFromFilesystem sees it correctly
coderPath := filepath.Join("/home/coder/repos", name)
if _, err := CoderExec(fmt.Sprintf("git init %s", coderPath)); err != nil {
return fmt.Errorf("git init failed: %w", err)
}
CoderExec("git config --global user.email 'dev@congo.gg'")
CoderExec("git config --global user.name 'Congo Dev'")
if _, err := CoderExec(fmt.Sprintf("git -C %s add .", coderPath)); err != nil {
return fmt.Errorf("git add failed: %w", err)
}
if _, err := CoderExec(fmt.Sprintf("git -C %s commit -m 'Initial scaffold'", coderPath)); err != nil {
return fmt.Errorf("git commit failed: %w", err)
}
branch := "main"
if out, err := CoderExec(fmt.Sprintf("git -C %s rev-parse --abbrev-ref HEAD", coderPath)); err == nil {
branch = strings.TrimSpace(out)
}
repo := &models.Repository{
Name: name,
Path: coderPath,
Branch: branch,
Status: "created",
}
repo.ID = RepoSlug(name)
if _, err := models.Repositories.Insert(repo); err != nil {
return fmt.Errorf("failed to save repository: %w", err)
}
return nil
}
// hostReposDir returns the repos directory path on the host filesystem.
// In production, DATA_DIR is set (e.g., /mnt/data) and repos are at DATA_DIR/repos.
// In development, falls back to /home/coder/repos (direct access).
func hostReposDir() string {
if dir := os.Getenv("DATA_DIR"); dir != "" {
return filepath.Join(dir, "repos")
}
return "/home/coder/repos"
}