Class: PromptObjects::CLI::EnvCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/cli/env_command.rb

Overview

env command: manage environments (list, create, info, export, import, archive, restore, clone, delete, default).

Instance Method Summary collapse

Constructor Details

#initialize(manager: nil) ⇒ EnvCommand

Returns a new instance of EnvCommand.



8
9
10
# File 'lib/prompt_objects/cli/env_command.rb', line 8

def initialize(manager: nil)
  @manager = manager || Env::Manager.new
end

Instance Method Details

#archive(args) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/prompt_objects/cli/env_command.rb', line 289

def archive(args)
  name = args.shift
  unless name
    puts "Usage: #{prog} env archive <name>"
    exit 1
  end

  path = @manager.archive(name)
  puts "Archived environment '#{name}' to:"
  puts "  #{path}"
end

#clone(args) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/prompt_objects/cli/env_command.rb', line 326

def clone(args)
  source = args.shift
  target = args.shift

  unless source && target
    puts "Usage: #{prog} env clone <source> <target>"
    exit 1
  end

  path = @manager.clone(source, target)
  puts "Cloned '#{source}' to '#{target}'"
  puts "Location: #{path}"
end

#create(args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/prompt_objects/cli/env_command.rb', line 91

def create(args)
  name = args.shift
  unless name
    puts "Usage: #{prog} env create <name> [--template <template>]"
    exit 1
  end

  template = nil
  args.each_with_index do |arg, i|
    if arg == "--template" || arg == "-t"
      template = args[i + 1]
    end
  end

  @manager.setup!

  path = @manager.create(name: name, template: template)
  puts "Created environment: #{name}"
  puts "Location: #{path}"

  # Print dependency info if template declares gems
  manifest = Env::Manifest.load_from_dir(path)
  if manifest.dependencies&.dig("gems")&.any?
    vendor_dir = File.join(path, "vendor")
    if Dir.exist?(vendor_dir) && !Dir.empty?(vendor_dir)
      puts "\nBundled dependencies (vendored):"
      manifest.dependencies["gems"].each do |gem_info|
        puts "  #{gem_info['name']}#{gem_info['purpose']}"
      end
    else
      puts "\nRequired dependencies (not vendored — install manually):"
      manifest.dependencies["gems"].each do |gem_info|
        puts "  gem install #{gem_info['name']}  # #{gem_info['purpose']}"
      end
    end
  end

  if @manager.list.size == 1
    @manager.set_default_environment(name)
    puts "Set as default environment."
  end
end

#delete(args) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/prompt_objects/cli/env_command.rb', line 340

def delete(args)
  permanent = args.delete("--permanent")
  names = args

  if names.empty?
    puts "Usage: #{prog} env delete <name> [<name>...] --permanent"
    puts
    puts "Permanently deletes environments (active or archived) and all their data."
    puts "For a recoverable flow, use `env archive <name>` instead."
    return
  end

  unless permanent
    puts "This permanently deletes #{names.size} environment#{names.size == 1 ? '' : 's'} and cannot be undone:"
    names.each { |n| puts "  - #{n}" }
    puts
    puts "Re-run with --permanent to confirm."
    return
  end

  names.each do |name|
    if @manager.environment_exists?(name)
      @manager.destroy(name)
      puts "Deleted: #{name}"
    elsif @manager.list_archived.include?(name)
      @manager.delete_archived(name)
      puts "Deleted (archived): #{name}"
    else
      puts "Not found: #{name}"
    end
  rescue => e
    puts "Failed to delete #{name}: #{e.message}"
  end
end

#export(args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/prompt_objects/cli/env_command.rb', line 165

def export(args)
  @manager.setup!

  # Parse arguments
  name = nil
  output = nil

  i = 0
  while i < args.length
    case args[i]
    when "-o", "--output"
      output = args[i + 1]
      i += 1
    else
      name ||= args[i]
    end
    i += 1
  end

  # Default to current directory name if no output specified
  name ||= @manager.default_environment
  unless name
    puts "Usage: #{prog} env export <name> [-o output.dump]"
    exit 1
  end

  unless @manager.environment_exists?(name)
    puts "Environment '#{name}' not found."
    exit 1
  end

  output ||= "#{name}.dump"
  env_path = @manager.environment_path(name)

  exporter = Env::Exporter.new(env_path)
  result = exporter.export(output)

  if result[:success]
    puts "Exported '#{name}' to: #{result[:path]}"
    puts
    puts "Stats:"
    puts "  Commits: #{result[:stats][:commits]}"
    puts "  Objects: #{result[:stats][:objects]}"
    puts "  Primitives: #{result[:stats][:primitives]}"
  else
    puts "Export failed: #{result[:error]}"
    exit 1
  end
end

#helpObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/prompt_objects/cli/env_command.rb', line 393

def help
  puts <<~HELP
    Environment management commands:

      #{prog} env list              List all environments (one line each)
        -v, --verbose                     Show full details per environment
      #{prog} env create <name>    Create new environment
        --template, -t <template>         Use one of the templates listed below
      #{prog} env info <name>      Show environment details
      #{prog} env export <name>    Export environment as .dump bundle
        -o, --output <file>               Output file path
      #{prog} env import <file>    Import environment from .dump bundle
        --as <name>                       Import with different name
        --trust                           Trust custom primitives
      #{prog} env archive <name>   Archive (soft delete) environment
      #{prog} env restore <name>   Restore archived environment
        --as <new_name>                   Restore with different name
      #{prog} env clone <src> <dest>  Clone environment
      #{prog} env default <name>   Set default environment
      #{prog} env delete <name>... --permanent  Permanently delete (active or archived; accepts multiple)

    Available templates:
      basic      - No capabilities, learns as needed (great for demos!)
      minimal    - Basic assistant with file reading
      developer  - Code review, debugging, testing specialists
      writer     - Editor, researcher for content creation
      empty      - Bootstrap assistant only
      arc-agi-1  - ARC-AGI-1 challenge solving with grid primitives
      arc-agi-3  - Interactive ARC-AGI-3 environment with remote API
      chiptune   - Chiptune music composition via Sonic Pi (genre-specific)
      music      - Open-ended music composition via Sonic Pi (any genre)
      music-pro  - Expressive live-pattern composition via Sonic Pi
  HELP
end

#import(args) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/prompt_objects/cli/env_command.rb', line 215

def import(args)
  @manager.setup!

  # Parse arguments
  bundle_path = nil
  import_as = nil
  trust = false

  i = 0
  while i < args.length
    case args[i]
    when "--as"
      import_as = args[i + 1]
      i += 1
    when "--trust"
      trust = true
    else
      bundle_path ||= args[i]
    end
    i += 1
  end

  unless bundle_path
    puts "Usage: #{prog} env import <bundle.dump> [--as <name>] [--trust]"
    puts
    puts "Options:"
    puts "  --as <name>   Import with a different name"
    puts "  --trust       Trust custom primitives (skip sandbox warnings)"
    exit 1
  end

  importer = Env::Importer.new(bundle_path)

  # First, inspect and show what's in the bundle
  info = importer.inspect_bundle
  unless info.valid
    puts "Invalid bundle: #{info.error}"
    exit 1
  end

  puts "Bundle contents:"
  puts "  Name: #{info.name}"
  puts "  Description: #{info.description}" if info.description
  puts "  Objects: #{info.objects.join(', ')}" if info.objects.any?
  puts "  Primitives: #{info.primitives.join(', ')}" if info.primitives.any?
  puts "  Commits: #{info.commits}"
  puts

  # Warn about primitives
  if info.primitives.any? && !trust
    puts "⚠️  WARNING: This bundle contains custom primitives."
    puts "Custom primitives can execute arbitrary Ruby code."
    puts "Review the code before running, or use --trust to skip this warning."
    puts
  end

  # Import
  import_name = import_as || info.name
  result = importer.import(manager: @manager, name: import_name, trust_primitives: trust)

  if result[:success]
    puts "Imported as '#{result[:name]}'"
    puts "Location: #{result[:path]}"

    if result[:warnings].any?
      puts
      result[:warnings].each { |w| puts w }
    end
  else
    puts "Import failed: #{result[:error]}"
    exit 1
  end
end

#info(args) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/prompt_objects/cli/env_command.rb', line 134

def info(args)
  name = args.shift || @manager.default_environment
  unless name
    puts "Usage: #{prog} env info <name>"
    exit 1
  end

  manifest = @manager.manifest_for(name)
  unless manifest
    puts "Environment '#{name}' not found."
    exit 1
  end

  path = @manager.environment_path(name)
  objects = Dir.glob(File.join(path, "objects", "*.md")).map { |f| File.basename(f, ".md") }
  primitives = Dir.glob(File.join(path, "primitives", "*.rb")).map { |f| File.basename(f, ".rb") }

  puts manifest.info
  puts
  puts "  Location: #{path}"
  puts "  Objects: #{objects.join(', ')}" if objects.any?
  puts "  Custom primitives: #{primitives.join(', ')}" if primitives.any?
  puts

  if Env::Git.repo?(path)
    commits = Env::Git.commit_count(path)
    dirty = Env::Git.dirty?(path)
    puts "  Git: #{commits} commits#{dirty ? ' (uncommitted changes)' : ''}"
  end
end

#list(args = []) ⇒ Object



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
# File 'lib/prompt_objects/cli/env_command.rb', line 46

def list(args = [])
  @manager.setup!
  envs = @manager.list_with_manifests

  if envs.empty?
    puts "No environments found."
    puts "Create one with: #{prog} env create <name>"
    return
  end

  default = @manager.default_environment

  if args.include?("-v") || args.include?("--verbose")
    puts "Environments (#{envs.size}):"
    puts
    envs.each do |manifest|
      default_marker = manifest.name == default ? " (default)" : ""
      last_opened = manifest.last_opened&.strftime("%Y-%m-%d") || "never"
      puts "  #{manifest.icon} #{manifest.name}#{default_marker}"
      puts "    #{manifest.description}" if manifest.description
      puts "    Last opened: #{last_opened}, Objects: #{manifest.stats['po_count']}"
      puts
    end
    return
  end

  # Concise: one line per environment, aligned for scanning.
  width = [envs.map { |m| m.name.length }.max, 32].min
  archived = @manager.list_archived.size

  header = "Environments (#{envs.size})"
  header += "  ·  #{archived} archived" if archived.positive?
  puts header
  puts
  envs.each do |m|
    name = m.name.ljust(width)
    last = (m.last_opened&.strftime("%Y-%m-%d") || "never").ljust(10)
    pos = (m.stats["po_count"] || 0).to_s.rjust(2)
    marker = m.name == default ? "  (default)" : ""
    puts "  #{name}  #{last}  #{pos} POs#{marker}"
  end
  puts
  puts "Details: env list -v   ·   Delete: env delete <name>... --permanent"
end

#restore(args) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/prompt_objects/cli/env_command.rb', line 301

def restore(args)
  archived_name = args.shift
  unless archived_name
    # Show available archived environments
    archived = @manager.list_archived
    if archived.empty?
      puts "No archived environments."
    else
      puts "Archived environments:"
      archived.each { |name| puts "  - #{name}" }
      puts
      puts "Usage: #{prog} env restore <archived_name> [--as <new_name>]"
    end
    return
  end

  restore_as = nil
  args.each_with_index do |arg, i|
    restore_as = args[i + 1] if arg == "--as"
  end

  path = @manager.restore(archived_name, restore_as: restore_as)
  puts "Restored to: #{path}"
end

#run(args) ⇒ Object

Run the env subcommand.

Parameters:

  • args (Array<String>)

    Arguments after 'env'



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
# File 'lib/prompt_objects/cli/env_command.rb', line 14

def run(args)
  subcommand = args.shift || "list"

  case subcommand
  when "list", "ls"
    list(args)
  when "create", "new"
    create(args)
  when "info"
    info(args)
  when "export"
    export(args)
  when "import"
    import(args)
  when "archive"
    archive(args)
  when "restore"
    restore(args)
  when "clone", "cp"
    clone(args)
  when "delete"
    delete(args)
  when "default"
    set_default(args)
  else
    puts "Unknown env command: #{subcommand}"
    puts
    help
    exit 1
  end
end

#set_default(args) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/prompt_objects/cli/env_command.rb', line 375

def set_default(args)
  name = args.shift
  unless name
    current = @manager.default_environment
    if current
      puts "Current default: #{current}"
    else
      puts "No default environment set."
    end
    puts
    puts "Usage: #{prog} env default <name>"
    return
  end

  @manager.set_default_environment(name)
  puts "Default environment set to: #{name}"
end