Class: EtnaApp::Metis::PullArchive

Inherits:
Etna::Command show all
Includes:
WithEtnaClients
Defined in:
lib/commands.rb

Instance Attribute Summary

Attributes inherited from Etna::Command

#parent

Instance Method Summary collapse

Methods included from WithEtnaClients

#environment, #exit, exit, #janus_client, #magma_client, #metis_client, #polyphemus_client, #token

Methods inherited from Etna::Command

#completions, #fill_in_missing_params, #find_command, #initialize, parent_scope, #setup

Methods included from Etna::CommandOrExecutor

#command_name, #completions_for, #desc, #flag_argspec, #flag_as_parameter, included, #parse_flags, #program_name, #usage

Constructor Details

This class inherits a constructor from Etna::Command

Instance Method Details

#execute(project_name:, bucket_name:, path:) ⇒ Object



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
# File 'lib/commands.rb', line 173

def execute(project_name:, bucket_name:, path:)
  @project_name = project_name
  @bucket_name = bucket_name
  basename = ::File.basename(path)
  dir = ::File.dirname(path)

  files = metis_client.list_folder(Etna::Clients::Metis::ListFolderRequest.new(
    project_name: project_name,
    bucket_name: bucket_name,
    folder_path: dir
  )).files.all

  files.sort_by(&:file_name).reverse

  archived = files.select { |f| f.file_name == basename }.first
  if archived.nil?
    archived = files.first
  end

  tmp = Tempfile.new('download', Dir.pwd)
  begin
    puts "Downloading to #{basename} from #{archived.file_name}"
    metis_client.download_file(archived) do |chunk|
      tmp.write(chunk)
    end

    # Atomic operation -- ensure we only place the file in position when it successfully loads
    ::File.rename(tmp.path, ::File.join(::Dir.pwd, basename))
  rescue
    tmp.close!
    raise
  end
end