Class: Sekrit::Pull

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

Instance Method Summary collapse

Constructor Details

#initialize(bundle_id: String, config: Config, passphrase: String) ⇒ Pull

Returns a new instance of Pull.

Raises:

  • (Thor::Error)


7
8
9
10
11
12
13
# File 'lib/sekrit/pull.rb', line 7

def initialize(bundle_id: String, config: Config, passphrase: String)
  @bundle_id = bundle_id
  @config = config
  @decoder = Decoder.new(password: passphrase)

  raise Thor::Error, Rainbow("Bundle id cannot be nil").red if @bundle_id.nil?
end

Instance Method Details

#bundleObject



15
16
17
# File 'lib/sekrit/pull.rb', line 15

def bundle
  @config.bundles.select { |b| b.id == @bundle_id }.first
end

#copy_bundled_files(dir: String) ⇒ Object

Raises:

  • (Thor::Error)


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
# File 'lib/sekrit/pull.rb', line 19

def copy_bundled_files(dir: String)
  raise Thor::Error, Rainbow("Cannot find bundle with id '#{@bundle_id}' in Sekritfile").red if bundle.nil?

  config_bundled_files = @config.bundled_files.nil? ? [] : @config.bundled_files.files
  config_bundled_files += bundle.files
  config_bundled_files.each do |f|
    src = "#{dir}/#{bundle.id}/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(src)
      log.debug Rainbow("Preparing to copy '#{src}' to '#{file_path}'").purple
      FileUtils.cp(src, f)
      log.debug Rainbow("Copied '#{src}' to '#{file_path}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{src}'").yellow)
    end
  end

  config_encrypted_files = @config.bundled_files.nil? ? [] : @config.bundled_files.encrypted
  config_encrypted_files += bundle.encrypted
  config_encrypted_files.each do |f|
    src = "#{dir}/#{bundle.id}/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(src)
      log.debug Rainbow("Preparing to decrypt and copy '#{src}' to '#{file_path}'").purple
      FileUtils.cp(src, f)
      log.debug Rainbow("Copied '#{src}' to '#{file_path}'").purple

      log.debug Rainbow("Preparing to decrypt '#{file_path}'").purple
      File.write(file_path, @decoder.decode(string: File.read(file_path)))
      log.debug Rainbow("Decrypted '#{file_path}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{src}'").yellow)
    end
  end
end

#copy_shared_files(dir: String) ⇒ Object



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
# File 'lib/sekrit/pull.rb', line 55

def copy_shared_files(dir: String)
  config_shared_files = @config.shared_files.nil? ? [] : @config.shared_files.files
  config_shared_files += bundle.files
  config_shared_files.each do |f|
    src = "#{dir}/shared/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(src)
      log.debug Rainbow("Preparing to copy '#{src}' to '#{file_path}'").purple
      FileUtils.cp(src, f)
      log.debug Rainbow("Copied '#{src}' to '#{file_path}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{src}'").yellow)
    end
  end

  config_encrypted_files = @config.shared_files.nil? ? [] : @config.shared_files.encrypted
  config_encrypted_files += bundle.encrypted
  config_encrypted_files.each do |f|
    src = "#{dir}/shared/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(src)
      log.debug Rainbow("Preparing to decrypt and copy '#{src}' to '#{file_path}'").purple
      FileUtils.cp(src, f)
      log.debug Rainbow("Copied '#{src}' to '#{file_path}'").purple

      log.debug Rainbow("Preparing to decrypt '#{file_path}'").purple
      File.write(file_path, @decoder.decode(string: File.read(file_path)))
      log.debug Rainbow("Decrypted '#{file_path}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{src}'").yellow)
    end
  end
end