Class: Sekrit::Push

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Push.

Raises:

  • (Thor::Error)


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

def initialize(bundle_id: String, config: Config, passphrase: String)
  @bundle_id = bundle_id
  @config = config
  @encoder = Encoder.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/push.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
54
55
# File 'lib/sekrit/push.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|
    dest = "#{dir}/#{bundle.id}/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(file_path)
      log.debug Rainbow("Preparing to copy '#{file_path}' to '#{dest}'").purple
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(f, dest)
      log.debug Rainbow("Copied '#{file_path}' to '#{dest}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{file_path}'").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|
    dest = "#{dir}/#{bundle.id}/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(file_path)
      log.debug Rainbow("Preparing to encrypt and copy '#{file_path}' to '#{dest}'").purple
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(f, dest)
      log.debug Rainbow("Copied '#{file_path}' to '#{dest}'").purple

      log.debug Rainbow("Preparing to encrypt '#{dest}'").purple
      File.write(dest, @encoder.encode(string: File.read(dest)))
      log.debug Rainbow("Encrypted '#{dest}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{file_path}'").yellow)
    end
  end
end

#copy_shared_files(dir: String) ⇒ Object



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
88
89
90
91
# File 'lib/sekrit/push.rb', line 57

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|
    dest = "#{dir}/shared/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(file_path)
      log.debug Rainbow("Preparing to copy '#{file_path}' to '#{dest}'").purple
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(f, dest)
      log.debug Rainbow("Copied '#{file_path}' to '#{dest}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{file_path}'").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|
    dest = "#{dir}/shared/#{f}"
    file_path = "#{Dir.pwd}/#{f}"
    if File.exist?(file_path)
      log.debug Rainbow("Preparing to encrypt and copy '#{file_path}' to '#{dest}'").purple
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(f, dest)
      log.debug Rainbow("Copied '#{file_path}' to '#{dest}'").purple

      log.debug Rainbow("Preparing to encrypt '#{dest}'").purple
      File.write(dest, @encoder.encode(string: File.read(dest)))
      log.debug Rainbow("Encrypted '#{dest}'").purple
    else
      log.warn(Rainbow("Could not find file at path '#{file_path}'").yellow)
    end
  end
end