Module: Sanctum::Command::DiffHelper

Included in:
Base
Defined in:
lib/sanctum/command/diff_helper.rb

Instance Method Summary collapse

Instance Method Details

#compare_secrets(vault_secrets, local_secrets, name, direction = "both") ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sanctum/command/diff_helper.rb', line 20

def compare_secrets(vault_secrets, local_secrets, name, direction="both")
  if vault_secrets == local_secrets
    warn yellow("Target #{name}: contains no differences")
  else
    case direction
    when "pull"
      puts yellow("Target #{name}: differences pulling from vault")
      hash_diff(local_secrets, vault_secrets)
    when "push"
      puts yellow("Target #{name}: differences pushing to vault")
      hash_diff(vault_secrets, local_secrets)
    when "both"
      puts yellow("Target #{name}: differences pulling from vault")
      hash_diff(local_secrets, vault_secrets)

      puts yellow("Target #{name}: differences pushing to vault")
      hash_diff(vault_secrets, local_secrets)
    end
  end
end

#confirmed_with_user?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sanctum/command/diff_helper.rb', line 41

def confirmed_with_user?
  puts yellow("\nWould you like to continue?: ")
  question = STDIN.gets.chomp.upcase

  if ["Y", "YES"].include? question
    puts yellow("Overwriting differences")
    true
  else
    warn yellow("Skipping....\n")
    false
  end
end

#hash_diff(first_hash, second_hash) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sanctum/command/diff_helper.rb', line 7

def hash_diff(first_hash, second_hash)
  differences = HashDiff.best_diff(first_hash, second_hash, delimiter: " => ", array_path: true)

  differences.each do |diff|
    if diff[0] == "+"
      puts green("#{diff[0].to_s + diff[1].join(" => ").to_s} => #{diff[2]}")
    else
      puts red("#{diff[0].to_s + diff[1].join(" => ").to_s} => #{diff[2]}")
    end
  end
  differences
end

#only_changes(array, hash) ⇒ Object

Array is a unique list of paths built from the differences of hash1 and hash2.

See diff_paths variable in push or pull command

Hash will be all local, or vault secrets. We then build a new hash that contains only the k,v needed to sync (to or from vault)



58
59
60
61
62
63
64
65
66
# File 'lib/sanctum/command/diff_helper.rb', line 58

def only_changes(array, hash)
  tmp_hash = Hash.new
  array.each do |a|
    hash.each do |k, v|
      tmp_hash[k] = v if a == k
    end
  end
  tmp_hash
end