|
| 1 | +// ExitBox - Multi-Agent Container Sandbox |
| 2 | +// Copyright (C) 2026 Cloud Exit B.V. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "os/exec" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/cloud-exit/exitbox/internal/agent" |
| 26 | + "github.com/cloud-exit/exitbox/internal/config" |
| 27 | + "github.com/cloud-exit/exitbox/internal/profile" |
| 28 | + "github.com/cloud-exit/exitbox/internal/ui" |
| 29 | + "github.com/spf13/cobra" |
| 30 | +) |
| 31 | + |
| 32 | +func newConfigCmd() *cobra.Command { |
| 33 | + cmd := &cobra.Command{ |
| 34 | + Use: "config", |
| 35 | + Short: "Manage agent configuration", |
| 36 | + Long: "Import or edit agent configuration files for a workspace.", |
| 37 | + } |
| 38 | + |
| 39 | + cmd.AddCommand(newConfigImportCmd()) |
| 40 | + cmd.AddCommand(newConfigEditCmd()) |
| 41 | + return cmd |
| 42 | +} |
| 43 | + |
| 44 | +func newConfigImportCmd() *cobra.Command { |
| 45 | + var workspace string |
| 46 | + var configFile string |
| 47 | + |
| 48 | + cmd := &cobra.Command{ |
| 49 | + Use: "import <agent|all>", |
| 50 | + Short: "Import agent config from host", |
| 51 | + Long: `Import agent configuration and credentials from the host into a workspace. |
| 52 | +
|
| 53 | +By default, imports into the active workspace. Use --workspace to target |
| 54 | +a specific workspace. Use --config to import a specific config file instead |
| 55 | +of the entire host config directory. |
| 56 | +
|
| 57 | +Examples: |
| 58 | + exitbox config import claude Import Claude config into active workspace |
| 59 | + exitbox config import all Import all agent configs into active workspace |
| 60 | + exitbox config import claude -w work Import Claude config into 'work' workspace |
| 61 | + exitbox config import all --workspace personal Import all configs into 'personal' workspace |
| 62 | + exitbox config import codex -c config.toml Import a config file into Codex workspace |
| 63 | + exitbox config import opencode -c opencode.json Import OpenCode config file |
| 64 | + exitbox config import codex -c config.toml -w work Import into specific workspace`, |
| 65 | + Args: cobra.ExactArgs(1), |
| 66 | + Run: func(cmd *cobra.Command, args []string) { |
| 67 | + target := args[0] |
| 68 | + |
| 69 | + // Validate --config flag constraints. |
| 70 | + if configFile != "" { |
| 71 | + if target == "all" { |
| 72 | + ui.Errorf("Cannot use --config with 'all'; specify a single agent") |
| 73 | + } |
| 74 | + if _, err := os.Stat(configFile); err != nil { |
| 75 | + ui.Errorf("Config file not found: %s", configFile) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + var agents []string |
| 80 | + if target == "all" { |
| 81 | + agents = agent.AgentNames |
| 82 | + } else { |
| 83 | + if !agent.IsValidAgent(target) { |
| 84 | + ui.Errorf("Unknown agent: %s", target) |
| 85 | + } |
| 86 | + agents = []string{target} |
| 87 | + } |
| 88 | + |
| 89 | + // Resolve target workspace. |
| 90 | + cfg := config.LoadOrDefault() |
| 91 | + workspaceName := resolveConfigWorkspace(cfg, workspace) |
| 92 | + |
| 93 | + importedAny := false |
| 94 | + for _, name := range agents { |
| 95 | + a := agent.Get(name) |
| 96 | + if a == nil { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + dst := profile.WorkspaceAgentDir(workspaceName, name) |
| 101 | + if err := os.MkdirAll(dst, 0755); err != nil { |
| 102 | + ui.Warnf("Failed to create workspace dir for %s: %v", name, err) |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + if configFile != "" { |
| 107 | + // Import a specific file. |
| 108 | + if err := a.ImportFile(configFile, dst); err != nil { |
| 109 | + ui.Warnf("Failed to import file into %s: %v", name, err) |
| 110 | + continue |
| 111 | + } |
| 112 | + ui.Successf("Imported %s → %s workspace '%s'", |
| 113 | + configFile, name, workspaceName) |
| 114 | + importedAny = true |
| 115 | + } else { |
| 116 | + // Import entire host config directory. |
| 117 | + src, err := a.DetectHostConfig() |
| 118 | + if err != nil { |
| 119 | + ui.Warnf("No host config found for %s", name) |
| 120 | + continue |
| 121 | + } |
| 122 | + if err := a.ImportConfig(src, dst); err != nil { |
| 123 | + ui.Warnf("Failed to import %s config: %v", name, err) |
| 124 | + continue |
| 125 | + } |
| 126 | + ui.Successf("Imported %s config from %s → workspace '%s'", |
| 127 | + name, src, workspaceName) |
| 128 | + importedAny = true |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if !importedAny { |
| 133 | + if configFile != "" { |
| 134 | + ui.Warn("Config file was not imported.") |
| 135 | + } else { |
| 136 | + ui.Warn("No configs were imported.") |
| 137 | + } |
| 138 | + } |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + cmd.Flags().StringVarP(&workspace, "workspace", "w", "", "Target workspace (default: active workspace)") |
| 143 | + cmd.Flags().StringVarP(&configFile, "config", "c", "", "Import a specific config file (e.g. config.toml, opencode.json)") |
| 144 | + return cmd |
| 145 | +} |
| 146 | + |
| 147 | +func newConfigEditCmd() *cobra.Command { |
| 148 | + var workspace string |
| 149 | + |
| 150 | + cmd := &cobra.Command{ |
| 151 | + Use: "edit <agent>", |
| 152 | + Short: "Open agent config file in $EDITOR", |
| 153 | + Long: `Opens the agent's primary config file in your $EDITOR (or vi). |
| 154 | +
|
| 155 | +Creates the file if it doesn't exist. |
| 156 | +
|
| 157 | +Examples: |
| 158 | + exitbox config edit claude Edit Claude settings.json |
| 159 | + exitbox config edit codex Edit Codex config.toml |
| 160 | + exitbox config edit opencode -w work Edit OpenCode config in 'work' workspace`, |
| 161 | + Args: cobra.ExactArgs(1), |
| 162 | + Run: func(cmd *cobra.Command, args []string) { |
| 163 | + name := args[0] |
| 164 | + if !agent.IsValidAgent(name) { |
| 165 | + ui.Errorf("Unknown agent: %s", name) |
| 166 | + } |
| 167 | + |
| 168 | + a := agent.Get(name) |
| 169 | + if a == nil { |
| 170 | + ui.Errorf("Agent not found: %s", name) |
| 171 | + } |
| 172 | + |
| 173 | + cfg := config.LoadOrDefault() |
| 174 | + workspaceName := resolveConfigWorkspace(cfg, workspace) |
| 175 | + wsDir := profile.WorkspaceAgentDir(workspaceName, name) |
| 176 | + p := a.ConfigFilePath(wsDir) |
| 177 | + |
| 178 | + // Create parent dirs + empty file if it doesn't exist. |
| 179 | + if _, err := os.Stat(p); os.IsNotExist(err) { |
| 180 | + if mkErr := os.MkdirAll(filepath.Dir(p), 0755); mkErr != nil { |
| 181 | + ui.Errorf("Failed to create directory: %v", mkErr) |
| 182 | + } |
| 183 | + if wErr := os.WriteFile(p, []byte{}, 0644); wErr != nil { |
| 184 | + ui.Errorf("Failed to create config file: %v", wErr) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + editor := os.Getenv("EDITOR") |
| 189 | + if editor == "" { |
| 190 | + editor = "vi" |
| 191 | + } |
| 192 | + c := exec.Command(editor, p) |
| 193 | + c.Stdin = os.Stdin |
| 194 | + c.Stdout = os.Stdout |
| 195 | + c.Stderr = os.Stderr |
| 196 | + if err := c.Run(); err != nil { |
| 197 | + ui.Errorf("Editor exited with error: %v", err) |
| 198 | + } |
| 199 | + }, |
| 200 | + } |
| 201 | + |
| 202 | + cmd.Flags().StringVarP(&workspace, "workspace", "w", "", "Workspace name (default: active workspace)") |
| 203 | + return cmd |
| 204 | +} |
| 205 | + |
| 206 | +// resolveConfigWorkspace determines which workspace to target. |
| 207 | +func resolveConfigWorkspace(cfg *config.Config, override string) string { |
| 208 | + if override != "" { |
| 209 | + w := profile.FindWorkspace(cfg, override) |
| 210 | + if w == nil { |
| 211 | + available := profile.WorkspaceNames(cfg) |
| 212 | + if len(available) > 0 { |
| 213 | + ui.Errorf("Unknown workspace '%s'. Available: %s", override, strings.Join(available, ", ")) |
| 214 | + } else { |
| 215 | + ui.Errorf("Unknown workspace '%s'. No workspaces configured. Run 'exitbox setup' first.", override) |
| 216 | + } |
| 217 | + } |
| 218 | + return w.Name |
| 219 | + } |
| 220 | + |
| 221 | + // Use the active/default workspace. |
| 222 | + projectDir, _ := os.Getwd() |
| 223 | + active, _ := profile.ResolveActiveWorkspace(cfg, projectDir, "") |
| 224 | + if active != nil { |
| 225 | + return active.Workspace.Name |
| 226 | + } |
| 227 | + |
| 228 | + // Fallback if no workspace exists at all. |
| 229 | + if len(cfg.Workspaces.Items) > 0 { |
| 230 | + return cfg.Workspaces.Items[0].Name |
| 231 | + } |
| 232 | + return "default" |
| 233 | +} |
| 234 | + |
| 235 | +func init() { |
| 236 | + rootCmd.AddCommand(newConfigCmd()) |
| 237 | +} |
0 commit comments