Class: Sekrit::Runner

Inherits:
Object show all
Defined in:
lib/sekrit/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(name: String, options: Hash, driver: lambda) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
9
# File 'lib/sekrit/runner.rb', line 5

def initialize(name: String, options: Hash, driver: lambda)
  @name = name
  @options = options
  @driver = driver
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# 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