Class: GDKBox::CLI
- Inherits:
-
Thor
- Object
- Thor
- GDKBox::CLI
- Defined in:
- lib/gdkbox/cli.rb
Overview
The gdkbox command-line interface.
Class Method Summary collapse
Instance Method Summary collapse
- #add_skill(box_name, *skill_tokens) ⇒ Object
- #code(name) ⇒ Object
- #completion(shell) ⇒ Object
- #dispatch(name) ⇒ Object
- #harnesses ⇒ Object
- #install_agent(name) ⇒ Object
- #install_skill(name = nil) ⇒ Object
- #ls ⇒ Object
- #rm(name) ⇒ Object
- #set_host ⇒ Object
- #set_key(name) ⇒ Object
- #skills ⇒ Object
- #ssh(name) ⇒ Object
- #start(name) ⇒ Object
- #status(name) ⇒ Object
- #stop(name) ⇒ Object
- #up(name) ⇒ Object
- #update_image ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
10 11 12 |
# File 'lib/gdkbox/cli.rb', line 10 def self.exit_on_failure? true end |
Instance Method Details
#add_skill(box_name, *skill_tokens) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/gdkbox/cli.rb', line 363 def add_skill(box_name, *skill_tokens) box = load_box!(box_name) skill_tokens = prompt_for_skills if skill_tokens.empty? raise Error, "No skills selected." if skill_tokens.empty? skill_tokens.each do |token| skill = box.install_skill(token, force: [:force]) say "Installed skill '#{skill.name}' into box '#{box_name}'.", :green end say "\nDispatched agents in '#{box_name}' can now use these skills.", :cyan end |
#code(name) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/gdkbox/cli.rb', line 183 def code(name) box = load_box!(name) rewrite_ssh_config vscode = VSCode.new unless vscode.available? say "The `code` CLI was not found on PATH.", :yellow say "Open VS Code manually and connect to host: #{box.ssh_host_alias}" say "Or run: #{vscode.open_command(box).join(' ')}" return end say "Opening #{box.name} in VS Code (#{box.ssh_host_alias})...", :green vscode.open(box) end |
#completion(shell) ⇒ Object
385 386 387 |
# File 'lib/gdkbox/cli.rb', line 385 def completion(shell) puts Completion.new(self.class).script(shell) end |
#dispatch(name) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/gdkbox/cli.rb', line 159 def dispatch(name) box = load_box!(name) task = [:task] task = File.read([:task_file]) if [:task_file] raise Error, "Provide a task with --task or --task-file." if task.nil? || task.strip.empty? result = box.run_agent( task: task, json: [:json], yolo: [:yolo], timeout: [:timeout] ) $stdout.print(result.stdout) $stderr.print(result.stderr) unless result.stderr.to_s.empty? exit(result.status) end |
#harnesses ⇒ Object
207 208 209 210 211 212 |
# File 'lib/gdkbox/cli.rb', line 207 def harnesses Harness.all.each do |h| say format("%-10s ", h.id), :green, false say "#{h.summary} (key: $#{h.key_env})" end end |
#install_agent(name) ⇒ Object
198 199 200 201 202 203 |
# File 'lib/gdkbox/cli.rb', line 198 def install_agent(name) box = load_box!(name) say "Installing #{box.harness.summary} in '#{name}'...", :green box.install_agent! say "Done. SSH in and run `#{box.harness.bin}` to start an agent.", :green end |
#install_skill(name = nil) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/gdkbox/cli.rb', line 320 def install_skill(name = nil) available = Skills.available raise Error, "This repo ships no skills." if available.empty? if name && !available.include?(name) raise Error, "No skill named '#{name}'. Available: #{available.join(', ')}." end dest = [:project] ? Skills.project_dest : Skills::GLOBAL_DEST skills = Skills.new(dest: dest) (name ? [name] : available).each do |skill_name| target = skills.install(skill_name, force: [:force]) say "Installed skill '#{skill_name}' -> #{target}", :green end say "\nStart a new Claude Code session to pick up the skill.", :cyan end |
#ls ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gdkbox/cli.rb', line 106 def ls boxes = Box.all(config: config).sort_by(&:name) docker = Docker.new if [:json] summaries = boxes.map { |box| box.summary(state: docker.available? ? nil : "unknown") } puts JSON.generate(summaries) return end if boxes.empty? say "No GDK boxes yet. Create one with `gdkbox up <name>`." return end boxes.each do |box| status = docker.available? ? box.state : "unknown" say format("%-20s %-10s ssh:%-6s web:%-6s %s", box.name, status, box.ssh_port, box.web_port, box.web_url) end end |
#rm(name) ⇒ Object
296 297 298 299 300 301 302 303 304 |
# File 'lib/gdkbox/cli.rb', line 296 def rm(name) box = load_box!(name) unless [:force] return unless yes?("Remove box '#{name}' and its container? [y/N]") end box.destroy! rewrite_ssh_config say "Removed '#{name}'.", :green end |
#set_host ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/gdkbox/cli.rb', line 253 def set_host hosts = HostsFile.new if [:remove] case hosts.remove when :absent say "No gdkbox-managed '#{HostsFile::HOSTNAME}' entry in #{hosts.path}. Nothing to do." when :removed say "Removed the '#{HostsFile::HOSTNAME}' entry from #{hosts.path}.", :green when :manual say "Could not write #{hosts.path}. Remove the line tagged " \ "'#{HostsFile::MARKER}' manually (e.g. with `sudoedit #{hosts.path}`).", :yellow end else case hosts.add when :present say "'#{HostsFile::HOSTNAME}' already resolves via #{hosts.path}. Nothing to do." when :added say "Added '#{HostsFile::ENTRY}' to #{hosts.path}.", :green when :manual say "Could not write #{hosts.path}. Add the entry manually:", :yellow say " echo '#{HostsFile::ENTRY}' | sudo tee -a #{hosts.path}", :yellow end end end |
#set_key(name) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/gdkbox/cli.rb', line 225 def set_key(name) box = load_box!(name) api_key = resolve_api_key(box.harness) unless api_key raise Error, "No API key. Pass --api-key, set $#{box.harness.key_env}, " \ "or add `#{box.harness.config_key}:` to ~/.gdkbox/config.yml." end say "Seeding #{box.harness.key_env} into '#{name}'...", :green box.set_api_key!(api_key) say "Done. Unattended `gdkbox dispatch #{name}` is ready.", :green end |
#skills ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/gdkbox/cli.rb', line 340 def skills found = Skills.discover if found.empty? say "No skills found in this repo, ~/.claude/skills, or ./.claude/skills." return end found.each do |skill| say format("%-28s ", skill.name), :green, false say "[#{skill.origin}]", :cyan say " #{skill.description}" if skill.description && !skill.description.empty? end end |
#ssh(name) ⇒ Object
177 178 179 180 |
# File 'lib/gdkbox/cli.rb', line 177 def ssh(name) box = load_box!(name) exec(*box.ssh_command) end |
#start(name) ⇒ Object
280 281 282 283 284 285 |
# File 'lib/gdkbox/cli.rb', line 280 def start(name) box = load_box!(name) say "Starting '#{name}'...", :green box.start! print_connection_details(box) end |
#status(name) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/gdkbox/cli.rb', line 130 def status(name) box = load_box!(name) docker = Docker.new if [:json] puts JSON.generate(box.summary(state: docker.available? ? nil : "unknown")) return end say "Box: #{box.name}" say "Container: #{box.container_name}" say "State: #{docker.available? ? box.state : 'unknown'}" print_connection_details(box) end |
#stop(name) ⇒ Object
288 289 290 291 292 |
# File 'lib/gdkbox/cli.rb', line 288 def stop(name) box = load_box!(name) say "Stopping '#{name}'...", :green box.stop! end |
#up(name) ⇒ Object
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 |
# File 'lib/gdkbox/cli.rb', line 40 def up(name) ensure_docker! box = build_box(name) raise Error, "Box '#{name}' already exists. Use `gdkbox rm #{name}` first." if box.exists? harness = Harness[[:harness] || config.default_harness] api_key = resolve_api_key(harness) say "Spinning up GDK box '#{name}' (#{harness.summary}; first run pulls a large image)...", :green unless [:json] box.create!( image: [:image], ssh_port: [:ssh_port], web_port: [:web_port], harness: harness.id, install_agent: [:agent], api_key: api_key ) rewrite_ssh_config seed_skills(box) if [:json] puts JSON.generate(box.summary) return end setup_host say "\nBox '#{name}' is up.", :green print_connection_details(box) unless api_key say "\n No #{harness.key_env} seeded. Before unattended dispatch, run:", :yellow say " gdkbox set-key #{name} (uses $#{harness.key_env})", :yellow end end |
#update_image ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gdkbox/cli.rb', line 85 def update_image ensure_docker! image = [:image] || config.default_image docker = Docker.new say "Pulling #{image} ...", :green = ProgressBar.new docker.pull(image) do |progress| label = "#{progress.completed}/#{progress.total} layers" label += " ยท #{progress.human_speed}" if progress.human_speed .update(progress.fraction, label) end .finish say "Updated. New boxes will use this image; recreate existing boxes to adopt it.", :green end |