Sha256: 20140fcce4710826eecdc64a6d91508848f3273936c2b4a96488899e631f05f1

Contents?: true

Size: 899 Bytes

Versions: 3

Compression:

Stored size: 899 Bytes

Contents

# frozen_string_literal: true

module SaveFile
  def save_file(file_path, content) # rubocop:disable Metrics/MethodLength
    return true if @config.dry_run

    saved = false
    begin
      ensure_path(file_path)

      File.open(full_file_path(file_path), 'w') do |file|
        file.write(content)
        file.close
        saved = true
      end
    rescue => e
      print "Failed to save #{file_path}, error: #{e.inspect}\n"
    end
    saved
  end

  def current_time_for_subfolder
    Time.now.to_s.parameterize.underscore
  end

  def ensure_path(file_path)
    path = folder_path(file_path)
      
    unless File.directory?(path)
      FileUtils.mkdir_p(path)
    end  
  end

  def full_file_path(file_path)
    "#{@config.files_location}/#{file_path}"
  end

  def folder_path(file_path)
    result = full_file_path(file_path).split('/')
    result.pop
    result.join('/')
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
travis-backup-for-v3-0.1.1 lib/backup/save_file.rb
travis-backup-for-v3-0.1.0 lib/backup/save_file.rb
travis-backup-0.3.0 lib/backup/save_file.rb