Class: ActiveRecordGraphExtractor::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/activerecord_graph_extractor/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/activerecord_graph_extractor/cli.rb', line 14

def self.exit_on_failure?
  true
end

Instance Method Details

#analyze(file_path) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/activerecord_graph_extractor/cli.rb', line 119

def analyze(file_path)
  setup_colors
  
  unless File.exist?(file_path)
    error_exit("File not found: #{file_path}")
  end

  serializer = JSONSerializer.new
  
  data = nil
  with_spinner("Loading file") do |spinner|
    data = serializer.deserialize_from_file(file_path)
    spinner.success
  end

  print_analysis(data, file_path)
end

#dry_run(model_class_name, id) ⇒ Object



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
# File 'lib/activerecord_graph_extractor/cli.rb', line 242

def dry_run(model_class_name, id)
  setup_colors
  
  @pastel.bright_blue("🔍 Performing dry run analysis...")
  puts
  puts "   Model: #{@pastel.cyan(model_class_name)}"
  puts "   ID: #{@pastel.cyan(id)}"
  puts "   Max Depth: #{@pastel.cyan(options[:max_depth] || 'default')}"
  puts

  begin
    model_class = model_class_name.constantize
    record = model_class.find(id)
    
    extraction_options = build_extraction_options_from_options(options)
    
    extractor = Extractor.new
    analysis = nil
    
    with_spinner("Analyzing object graph") do |spinner|
      analysis = extractor.dry_run(record, extraction_options)
      spinner.success
    end
    
    if options[:output]
      File.write(options[:output], JSON.pretty_generate(analysis))
      puts @pastel.green("📄 Analysis report saved to: #{options[:output]}")
      puts
    end
    
    print_dry_run_analysis(analysis)
    
  rescue NameError => e
    error_exit("Model not found: #{model_class_name}. Make sure the model class exists and is loaded.")
  rescue ActiveRecord::RecordNotFound => e
    error_exit("Record not found: #{model_class_name} with ID #{id}")
  rescue => e
    error_exit("Analysis failed: #{e.message}")
  end
end

#extract(model_class_name, id) ⇒ Object



35
36
37
38
39
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
73
# File 'lib/activerecord_graph_extractor/cli.rb', line 35

def extract(model_class_name, id)
  setup_colors
  
  begin
    model_class = model_class_name.constantize
    object = model_class.find(id)
  rescue NameError
    error_exit("Model #{model_class_name} not found")
  rescue ActiveRecord::RecordNotFound
    error_exit("#{model_class_name} with ID #{id} not found")
  end

  config = build_config_from_options(options)
  
  if options[:progress]
    config[:on_progress] = method(:handle_progress_update)
    setup_progress_bars
  end

  @pastel.bright_blue("🔍 Analyzing relationships...")
  puts

  extractor = Extractor.new(root_object: object, config: config)
  
  if options[:show_graph]
    show_dependency_graph(extractor)
  end

  result = nil
  with_spinner("Extracting records") do |spinner|
    result = extractor.extract_to_file(options[:output])
    spinner.success("(#{result.duration_human})")
  end

  print_extraction_summary(result)
  
rescue => e
  error_exit("Extraction failed: #{e.message}")
end

#extract_to_s3(model_class_name, id) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/activerecord_graph_extractor/cli.rb', line 148

def extract_to_s3(model_class_name, id)
  setup_colors
  
  begin
    model_class = model_class_name.constantize
    object = model_class.find(id)
  rescue NameError
    error_exit("Model #{model_class_name} not found")
  rescue ActiveRecord::RecordNotFound
    error_exit("#{model_class_name} with ID #{id} not found")
  end

  @pastel.bright_blue("🔍 Extracting and uploading to S3...")
  puts

  extractor = Extractor.new
  extraction_options = build_extraction_options_from_options(options)
  
  result = nil
  with_spinner("Extracting and uploading") do |spinner|
    result = extractor.extract_and_upload_to_s3(
      object,
      bucket_name: options[:bucket],
      s3_key: options[:key],
      region: options[:region],
      options: extraction_options
    )
    spinner.success
  end

  print_s3_extraction_summary(result)
  
rescue => e
  error_exit("S3 extraction failed: #{e.message}")
end

#import(file_path) ⇒ Object



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
# File 'lib/activerecord_graph_extractor/cli.rb', line 82

def import(file_path)
  setup_colors
  
  unless File.exist?(file_path)
    error_exit("File not found: #{file_path}")
  end

  config = build_config_from_options(options)
  
  if options[:progress]
    config[:on_progress] = method(:handle_import_progress_update)
    setup_import_progress_bars
  end

  importer = Importer.new(config: config)
  
  result = nil
  if options[:dry_run]
    @pastel.yellow("🔍 Performing dry run...")
    puts
  else
    @pastel.bright_blue("📦 Importing records...")
    puts
  end

  with_spinner("Processing import") do |spinner|
    result = importer.import_from_file(file_path)
    spinner.success("(#{result.duration_human})")
  end

  print_import_summary(result)
  
rescue => e
  error_exit("Import failed: #{e.message}")
end

#s3_download(s3_key) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/activerecord_graph_extractor/cli.rb', line 218

def s3_download(s3_key)
  setup_colors
  
  @pastel.bright_blue("⬇️  Downloading from S3...")
  puts

  s3_client = S3Client.new(bucket_name: options[:bucket], region: options[:region])
  
  result = nil
  with_spinner("Downloading file") do |spinner|
    result = s3_client.download_file(s3_key, options[:output])
    spinner.success
  end

  print_s3_download_summary(result)
  
rescue => e
  error_exit("S3 download failed: #{e.message}")
end

#s3_listObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/activerecord_graph_extractor/cli.rb', line 190

def s3_list
  setup_colors
  
  @pastel.bright_blue("📋 Listing S3 files...")
  puts

  s3_client = S3Client.new(bucket_name: options[:bucket], region: options[:region])
  
  files = nil
  with_spinner("Fetching file list") do |spinner|
    files = s3_client.list_files(
      prefix: options[:prefix],
      max_keys: options[:max_keys]
    )
    spinner.success
  end

  print_s3_file_list(files)
  
rescue => e
  error_exit("S3 list failed: #{e.message}")
end

#versionObject



19
20
21
# File 'lib/activerecord_graph_extractor/cli.rb', line 19

def version
  puts "ActiveRecord Graph Extractor v#{ActiveRecordGraphExtractor::VERSION}"
end