|
| 1 | +package compose |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | +) |
| 8 | + |
| 9 | +type Manager struct { |
| 10 | + basePath string |
| 11 | +} |
| 12 | + |
| 13 | +type ProjectConfig struct { |
| 14 | + Name string `json:"name"` |
| 15 | + ComposeFile string `json:"compose_file,omitempty"` // Optional, defaults to docker-compose.yml |
| 16 | + Content string `json:"content"` // Docker compose YAML content |
| 17 | + EnvVars map[string]string `json:"env_vars,omitempty"` // Environment variables for .env file |
| 18 | + Override bool `json:"override,omitempty"` // Whether to override existing files |
| 19 | +} |
| 20 | + |
| 21 | +func NewManager(basePath string) *Manager { |
| 22 | + return &Manager{ |
| 23 | + basePath: basePath, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// EnsureBaseDirectory creates the base compose directory if it doesn't exist |
| 28 | +func (m *Manager) EnsureBaseDirectory() error { |
| 29 | + if err := os.MkdirAll(m.basePath, 0755); err != nil { |
| 30 | + return fmt.Errorf("failed to create base directory %s: %w", m.basePath, err) |
| 31 | + } |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +// CreateProject creates a new compose project directory with files |
| 36 | +func (m *Manager) CreateProject(config ProjectConfig) error { |
| 37 | + if config.Name == "" { |
| 38 | + return fmt.Errorf("project name is required") |
| 39 | + } |
| 40 | + |
| 41 | + if config.Content == "" { |
| 42 | + return fmt.Errorf("compose content is required") |
| 43 | + } |
| 44 | + |
| 45 | + // Set default compose file name |
| 46 | + if config.ComposeFile == "" { |
| 47 | + config.ComposeFile = "docker-compose.yml" |
| 48 | + } |
| 49 | + |
| 50 | + projectPath := filepath.Join(m.basePath, config.Name) |
| 51 | + |
| 52 | + // Create project directory |
| 53 | + if err := os.MkdirAll(projectPath, 0755); err != nil { |
| 54 | + return fmt.Errorf("failed to create project directory %s: %w", projectPath, err) |
| 55 | + } |
| 56 | + |
| 57 | + // Create compose file |
| 58 | + composeFilePath := filepath.Join(projectPath, config.ComposeFile) |
| 59 | + if err := m.writeFileIfNotExists(composeFilePath, config.Content, config.Override); err != nil { |
| 60 | + return fmt.Errorf("failed to create compose file: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Create .env file if env vars provided |
| 64 | + if len(config.EnvVars) > 0 { |
| 65 | + envFilePath := filepath.Join(projectPath, ".env") |
| 66 | + envContent := m.generateEnvContent(config.EnvVars) |
| 67 | + if err := m.writeFileIfNotExists(envFilePath, envContent, config.Override); err != nil { |
| 68 | + return fmt.Errorf("failed to create .env file: %w", err) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// UpdateProject updates an existing project's files |
| 76 | +func (m *Manager) UpdateProject(config ProjectConfig) error { |
| 77 | + config.Override = true // Force override for updates |
| 78 | + return m.CreateProject(config) |
| 79 | +} |
| 80 | + |
| 81 | +// DeleteProject removes a project directory |
| 82 | +func (m *Manager) DeleteProject(projectName string) error { |
| 83 | + if projectName == "" { |
| 84 | + return fmt.Errorf("project name is required") |
| 85 | + } |
| 86 | + |
| 87 | + projectPath := filepath.Join(m.basePath, projectName) |
| 88 | + |
| 89 | + // Check if project exists |
| 90 | + if _, err := os.Stat(projectPath); os.IsNotExist(err) { |
| 91 | + return fmt.Errorf("project %s does not exist", projectName) |
| 92 | + } |
| 93 | + |
| 94 | + // Remove project directory |
| 95 | + if err := os.RemoveAll(projectPath); err != nil { |
| 96 | + return fmt.Errorf("failed to delete project %s: %w", projectName, err) |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// ListProjects returns a list of existing project names |
| 103 | +func (m *Manager) ListProjects() ([]string, error) { |
| 104 | + entries, err := os.ReadDir(m.basePath) |
| 105 | + if err != nil { |
| 106 | + if os.IsNotExist(err) { |
| 107 | + return []string{}, nil |
| 108 | + } |
| 109 | + return nil, fmt.Errorf("failed to read base directory: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + var projects []string |
| 113 | + for _, entry := range entries { |
| 114 | + if entry.IsDir() { |
| 115 | + projects = append(projects, entry.Name()) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return projects, nil |
| 120 | +} |
| 121 | + |
| 122 | +// ProjectExists checks if a project directory exists |
| 123 | +func (m *Manager) ProjectExists(projectName string) bool { |
| 124 | + projectPath := filepath.Join(m.basePath, projectName) |
| 125 | + _, err := os.Stat(projectPath) |
| 126 | + return !os.IsNotExist(err) |
| 127 | +} |
| 128 | + |
| 129 | +// GetProjectPath returns the full path to a project directory |
| 130 | +func (m *Manager) GetProjectPath(projectName string) string { |
| 131 | + return filepath.Join(m.basePath, projectName) |
| 132 | +} |
| 133 | + |
| 134 | +// GetComposePath returns the full path to a project's compose file |
| 135 | +func (m *Manager) GetComposePath(projectName, composeFile string) string { |
| 136 | + if composeFile == "" { |
| 137 | + composeFile = "docker-compose.yml" |
| 138 | + } |
| 139 | + return filepath.Join(m.basePath, projectName, composeFile) |
| 140 | +} |
| 141 | + |
| 142 | +// writeFileIfNotExists writes content to a file, optionally overriding existing files |
| 143 | +func (m *Manager) writeFileIfNotExists(filePath, content string, override bool) error { |
| 144 | + // Check if file exists |
| 145 | + if _, err := os.Stat(filePath); err == nil && !override { |
| 146 | + return fmt.Errorf("file %s already exists and override is false", filePath) |
| 147 | + } |
| 148 | + |
| 149 | + // Write file |
| 150 | + if err := os.WriteFile(filePath, []byte(content), 0644); err != nil { |
| 151 | + return fmt.Errorf("failed to write file %s: %w", filePath, err) |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +// generateEnvContent creates .env file content from environment variables |
| 158 | +func (m *Manager) generateEnvContent(envVars map[string]string) string { |
| 159 | + content := "# Environment variables for Docker Compose\n" |
| 160 | + content += "# Generated by Arcane Agent\n\n" |
| 161 | + |
| 162 | + for key, value := range envVars { |
| 163 | + content += fmt.Sprintf("%s=%s\n", key, value) |
| 164 | + } |
| 165 | + |
| 166 | + return content |
| 167 | +} |
0 commit comments