Class: Aidp::CLI::DevcontainerCommands

Inherits:
Object
  • Object
show all
Includes:
MessageDisplay
Defined in:
lib/aidp/cli/devcontainer_commands.rb

Overview

Commands for managing devcontainer configuration

Constant Summary collapse

COMPONENT =
"devcontainer_commands"

Constants included from MessageDisplay

MessageDisplay::COLOR_MAP

Instance Method Summary collapse

Methods included from MessageDisplay

#display_message, included, #message_display_prompt

Constructor Details

#initialize(project_dir: Dir.pwd, prompt: TTY::Prompt.new) ⇒ DevcontainerCommands

Returns a new instance of DevcontainerCommands.



18
19
20
21
22
# File 'lib/aidp/cli/devcontainer_commands.rb', line 18

def initialize(project_dir: Dir.pwd, prompt: TTY::Prompt.new)
  @project_dir = project_dir
  @prompt = prompt
  Aidp.log_debug(COMPONENT, "initialized", project_dir: project_dir)
end

Instance Method Details

#apply(options = {}) ⇒ Object

Apply devcontainer configuration from aidp.yml



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
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
# File 'lib/aidp/cli/devcontainer_commands.rb', line 55

def apply(options = {})
  dry_run = options[:dry_run] || false
  force = options[:force] || false
  create_backup = options[:backup] != false  # Default true
  Aidp.log_debug(COMPONENT, "apply.start",
    dry_run: dry_run,
    force: force,
    create_backup: create_backup)

  parser = Aidp::Setup::Devcontainer::Parser.new(@project_dir)
  existing_config = parser.devcontainer_exists? ? parser.parse : nil
  devcontainer_path = parser.detect || default_devcontainer_path

  # Load configuration
  proposed_config = load_proposed_config(options)

  unless proposed_config
    Aidp.log_debug(COMPONENT, "apply.no_configuration")
    display_message("❌ No configuration found in aidp.yml", type: :error)
    display_message("Run 'aidp config --interactive' first", type: :muted)
    return false
  end

  # Merge with existing if present
  generator = Aidp::Setup::Devcontainer::Generator.new(@project_dir)
  final_config = if existing_config
    generator.merge_with_existing(proposed_config, existing_config)
  else
    proposed_config
  end

  # Show preview
  if dry_run
    display_message("🔍 Dry Run - Changes Preview", type: :highlight)
    display_diff(existing_config || {}, final_config, devcontainer_path)
    display_message("\nNo changes made (dry run)", type: :muted)
    Aidp.log_debug(COMPONENT, "apply.dry_run_preview",
      has_existing: !existing_config.nil?,
      devcontainer_path: devcontainer_path)
    return true
  end

  # Confirm unless forced
  unless force
    display_diff(existing_config || {}, final_config, devcontainer_path)
    display_message("")

    unless @prompt.yes?("Apply these changes?")
      display_message("Cancelled", type: :warning)
      return false
    end
  end

  # Create backup if existing file
  if create_backup && File.exist?(devcontainer_path)
    backup_manager = Aidp::Setup::Devcontainer::BackupManager.new(@project_dir)
    backup_path = backup_manager.create_backup(devcontainer_path, {
      reason: "cli_apply",
      timestamp: Time.now.utc.iso8601
    })
    display_message("✅ Backup created: #{File.basename(backup_path)}", type: :success)
    Aidp.log_debug(COMPONENT, "apply.backup_created", backup_path: backup_path)
  end

  # Write devcontainer.json
  write_devcontainer(devcontainer_path, final_config)

  display_message("✅ Devcontainer configuration applied", type: :success)
  display_message("   #{devcontainer_path}", type: :muted)
  Aidp.log_debug(COMPONENT, "apply.completed",
    devcontainer_path: devcontainer_path,
    forward_ports: final_config["forwardPorts"]&.length)
  true
end

#diff(options = {}) ⇒ Object

Show diff between current and proposed devcontainer configuration



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
# File 'lib/aidp/cli/devcontainer_commands.rb', line 25

def diff(options = {})
  Aidp.log_debug(COMPONENT, "diff.start", options: options)
  parser = Aidp::Setup::Devcontainer::Parser.new(@project_dir)

  unless parser.devcontainer_exists?
    Aidp.log_debug(COMPONENT, "diff.no_devcontainer")
    display_message("No existing devcontainer.json found", type: :warning)
    display_message("Run 'aidp config --interactive' to create one", type: :muted)
    return false
  end

  devcontainer_path = parser.detect
  current_config = parser.parse

  # Load proposed config from aidp.yml or generate from config
  proposed_config = load_proposed_config(options)

  unless proposed_config
    Aidp.log_debug(COMPONENT, "diff.no_proposed_config")
    display_message("No proposed configuration found", type: :warning)
    display_message("Update your aidp.yml or use --generate", type: :muted)
    return false
  end

  display_diff(current_config, proposed_config, devcontainer_path)
  Aidp.log_debug(COMPONENT, "diff.complete", devcontainer_path: devcontainer_path)
  true
end

#list_backupsObject

List available backups



131
132
133
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
# File 'lib/aidp/cli/devcontainer_commands.rb', line 131

def list_backups
  Aidp.log_debug(COMPONENT, "list_backups.start")
  backup_manager = Aidp::Setup::Devcontainer::BackupManager.new(@project_dir)
  backups = backup_manager.list_backups

  if backups.empty?
    display_message("No backups found", type: :muted)
    Aidp.log_debug(COMPONENT, "list_backups.none_found")
    return true
  end

  display_message("📦 Available Backups", type: :highlight)
  display_message("")

  backups.each_with_index do |backup, index|
    display_message("#{index + 1}. #{backup[:filename]}", type: :info)
    display_message("   Created: #{backup[:created_at].strftime("%Y-%m-%d %H:%M:%S")}", type: :muted)
    display_message("   Size: #{format_size(backup[:size])}", type: :muted)
    if backup[:metadata]
      display_message("   Reason: #{backup[:metadata]["reason"]}", type: :muted)
    end
    display_message("")
  end

  total_size = backup_manager.total_backup_size
  display_message("Total: #{backups.size} backups (#{format_size(total_size)})", type: :muted)
  Aidp.log_debug(COMPONENT, "list_backups.complete",
    count: backups.size,
    total_size: total_size)
  true
end

#restore(backup_index_or_path, options = {}) ⇒ Object

Restore from a backup



164
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
214
215
216
217
# File 'lib/aidp/cli/devcontainer_commands.rb', line 164

def restore(backup_index_or_path, options = {})
  Aidp.log_debug(COMPONENT, "restore.start",
    selector: backup_index_or_path,
    options: options)
  backup_manager = Aidp::Setup::Devcontainer::BackupManager.new(@project_dir)

  backup_path = if backup_index_or_path.to_i.positive?
    # Index-based selection
    backups = backup_manager.list_backups
    index = backup_index_or_path.to_i - 1

    unless backups[index]
      Aidp.log_debug(COMPONENT, "restore.invalid_index",
        selector: backup_index_or_path)
      display_message("❌ Invalid backup index: #{backup_index_or_path}", type: :error)
      return false
    end

    backups[index][:path]
  else
    # Direct path
    backup_index_or_path
  end
  Aidp.log_debug(COMPONENT, "restore.resolved_backup", backup_path: backup_path)

  unless File.exist?(backup_path)
    Aidp.log_debug(COMPONENT, "restore.missing_backup", backup_path: backup_path)
    display_message("❌ Backup not found: #{backup_path}", type: :error)
    return false
  end

  parser = Aidp::Setup::Devcontainer::Parser.new(@project_dir)
  target_path = parser.detect || default_devcontainer_path

  # Show what will be restored
  JSON.parse(File.read(backup_path))
  display_message("📦 Restoring Backup", type: :highlight)
  display_message("   From: #{File.basename(backup_path)}", type: :muted)
  display_message("   To: #{target_path}", type: :muted)
  display_message("")

  unless options[:force] || @prompt.yes?("Restore this backup?")
    Aidp.log_debug(COMPONENT, "restore.cancelled_by_user", target_path: target_path)
    display_message("Cancelled", type: :warning)
    return false
  end

  # Restore
  backup_manager.restore_backup(backup_path, target_path, create_backup: !options[:no_backup])
  Aidp.log_debug(COMPONENT, "restore.completed", target_path: target_path)

  display_message("✅ Backup restored successfully", type: :success)
  true
end