class Sekrit::Runner

Public Class Methods

new(name: String, options: Hash, driver: lambda) click to toggle source
# File lib/sekrit/runner.rb, line 5
def initialize(name: String, options: Hash, driver: lambda)
  @name = name
  @options = options
  @driver = driver
end

Public Instance Methods

run() click to toggle source
# File lib/sekrit/runner.rb, line 11
def run
  delete_sekrit_dir_if_exist?

  begin
    print_command_config(name: @name)

    raise Thor::Error, Rainbow("Cannot find Sekritfile at #{@options[:config]}").red unless File.exist?(@options[:config])

    @config = Sekrit::Config.new(path: @options[:config])
    bundle_id = @options[:bundle_id] || @config.bundles.first.id

    print_sekrit_config(bundle_id: bundle_id)

    @passphrase = @options[:passphrase] || ENV[@config.passphrase]
    raise Thor::Error, Rainbow("passphrase cannot be nil").red if @passphrase.nil?
    raise Thor::Error, Rainbow("passphrase cannot be empty").red if @passphrase.empty?

    git_name = @config.repo.split('/').last.chomp('.git')
    git = Git.clone(@config.repo, git_name, path: sekrit_dir, log: log)
    begin
      git.checkout(@options[:git_ref])
      log.info Rainbow("Checking out #{@options[:git_ref]}").blue
    rescue
      git.branch(@options[:git_ref]).checkout
      log.info Rainbow("Creating new branch #{@options[:git_ref]}").blue
    end

    directory = "#{working_directory}/#{git_name}"
    driver = @driver.call(bundle_id, @config, @passphrase)
    driver.copy_bundled_files(dir: directory)
    driver.copy_shared_files(dir: directory)

    if driver.class == Push
      log.info Rainbow("git adding...").green
      git.add
      log.info Rainbow("git committing...").green
      git.commit "[Sekrit] Updating files for #{bundle_id}"
      log.info Rainbow("git pushing...").green
      git.push(git.remote('origin'), git.branch(@options[:git_ref]))
      log.info Rainbow("git completed!").green
    end

  rescue => error
    log.warn Rainbow("git repo at `#{sekrit_dir}/#{git_name}` was not deleted").yellow
    raise Thor::Error, Rainbow(error.full_message).red
  end

  delete_sekrit_dir_if_exist?
end

Private Instance Methods

delete_sekrit_dir_if_exist?() click to toggle source
# File lib/sekrit/runner.rb, line 111
def delete_sekrit_dir_if_exist?
  FileUtils.remove_dir(working_directory) if Dir.exist?(working_directory)
end
print_command_config(name: String) click to toggle source
print_sekrit_config(bundle_id: String) click to toggle source
sekrit_dir() click to toggle source
# File lib/sekrit/runner.rb, line 103
def sekrit_dir
  '.sekrit'
end
working_directory() click to toggle source
# File lib/sekrit/runner.rb, line 107
def working_directory
  "#{@options[:working_directory]}/#{sekrit_dir}"
end