Class: EmergeCLI::Commands::Upload::Snapshots

Inherits:
GlobalOptions
  • Object
show all
Defined in:
lib/commands/upload/snapshots/snapshots.rb

Instance Method Summary collapse

Methods inherited from GlobalOptions

#before

Constructor Details

#initialize(network: nil, git_info_provider: nil) ⇒ Snapshots

Returns a new instance of Snapshots.



38
39
40
41
42
# File 'lib/commands/upload/snapshots/snapshots.rb', line 38

def initialize(network: nil, git_info_provider: nil)
  @network = network
  @git_info_provider = git_info_provider
  @profiler = EmergeCLI::Profiler.new
end

Instance Method Details

#call(image_paths:, **options) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/commands/upload/snapshots/snapshots.rb', line 44

def call(image_paths:, **options)
  @options = options
  @profiler = EmergeCLI::Profiler.new(enabled: options[:profile])
  before(options)

  start_time = Time.now
  run_id = nil
  success = false

  begin
    api_token = @options[:api_token] || ENV.fetch('EMERGE_API_TOKEN', nil)
    raise 'API token is required and cannot be blank' if api_token.nil? || api_token.strip.empty?

    @network ||= EmergeCLI::Network.new(api_token:)
    @git_info_provider ||= GitInfoProvider.new

    Sync do
      validate_options(image_paths)

      client = create_client(image_paths)

      image_files = @profiler.measure('find_image_files') { find_image_files(client) }

      @profiler.measure('check_duplicate_files') { check_duplicate_files(image_files, client) }

      run_id = @profiler.measure('create_run') { create_run }

      upload_images(run_id, options[:concurrency], image_files, client)

      @profiler.measure('finish_run') { finish_run(run_id) }
    end

    Logger.info 'Upload completed successfully!'
    Logger.info "Time taken: #{(Time.now - start_time).round(2)} seconds"
    @profiler.report
    Logger.info "✅ View your snapshots at https://emergetools.com/snapshot/#{run_id}"
    success = true
  rescue StandardError => e
    Logger.error "CLI Error: #{e.message}"
    Sync { report_error(run_id, e.message, 'crash') } if run_id
    raise e # Re-raise the error to dry-cli
  ensure
    @network&.close
  end

  success
end