Module: AccessHelper
- Included in:
- VpsCli::Access
- Defined in:
- lib/vps_cli/helpers/access_helper.rb
Overview
Helper methods to be used within Access to help reduce the file size This is meant for reading files encrypted with sops
Instance Method Summary collapse
-
#decrypt(yaml_file:, path:) ⇒ String
uses an access file via SOPS SOPS is an encryption tool It will decrypt the file, please use a .yaml file.
-
#dig_for_path(*path) ⇒ String
Returns a path string to be able to traverse a yaml file.
-
#export_tty ⇒ Object
I noticed needing to export $(tty) while troubleshooting issues with gpg keys.
-
#heroku_api_string(yaml_file:) ⇒ String
retrieves the values of .netrc for heroku and creates a writable string.
-
#heroku_git_string(yaml_file:) ⇒ String
retries the git values for heroku in your .yaml file.
-
#make_string(base:, keys:) ⇒ String
Creates a string to be used to write to .netrc.
-
#my_inject_with_count(array) {|accum, element| ... } ⇒ String
my version of Enumerable#inject intended to return a string provides a count to know what # object youre on.
-
#netrc_error(netrc_file:, error:) ⇒ Object
Adds the error to VpsCli#errors array.
-
#write_to_netrc(netrc_file: nil, string:) ⇒ Object
Writes the value of string to netrc.
Instance Method Details
#decrypt(yaml_file:, path:) ⇒ String
uses an access file via SOPS SOPS is an encryption tool It will decrypt the file, please use a .yaml file
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 105 def decrypt(yaml_file:, path:) # puts all keys into a ["key"] within the array sops_cmd = "sops -d --extract '#{path}' #{yaml_file}" # this allows you to enter your passphrase export_tty # this will return in the string form the value you were looking for stdout, _stderr, _status = Open3.capture3(sops_cmd) stdout end |
#dig_for_path(*path) ⇒ String
Returns a path string to be able to traverse a yaml file
120 121 122 123 124 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 120 def dig_for_path(*path) path.flatten.inject('') do |final_path, node| final_path + "[#{node.to_s.to_json}]" end end |
#export_tty ⇒ Object
I noticed needing to export $(tty) while troubleshooting issues with gpg keys. It is here just in case its not in your zshrc / bashrc file
130 131 132 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 130 def export_tty Rake.sh('GPG_TTY=$(tty) && export GPG_TTY') end |
#heroku_api_string(yaml_file:) ⇒ String
retrieves the values of .netrc for heroku and creates a writable string
13 14 15 16 17 18 19 20 21 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 13 def heroku_api_string(yaml_file:) # initial tree traversal in the .yaml file heroku_api = %i[heroku api] heroku_api_keys = %i[machine login password] make_string(base: heroku_api, keys: heroku_api_keys) do |path| decrypt(yaml_file: yaml_file, path: path) end end |
#heroku_git_string(yaml_file:) ⇒ String
retries the git values for heroku in your .yaml file
25 26 27 28 29 30 31 32 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 25 def heroku_git_string(yaml_file:) heroku_git = %i[heroku git] heroku_git_keys = %i[machine login password] make_string(base: heroku_git, keys: heroku_git_keys) do |path| decrypt(yaml_file: yaml_file, path: path) end end |
#make_string(base:, keys:) ⇒ String
Creates a string to be used to write to .netrc
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 57 def make_string(base:, keys:) # iterates through the keys to provide a path to each array # essentialy is the equivalent of Hash.dig(:heroku, :api, :key) my_inject_with_count(keys) do |string, key, count| path = dig_for_path(base, key) value = yield(path) value << "\n " if count < keys.length - 1 string + value end end |
#my_inject_with_count(array) {|accum, element| ... } ⇒ String
my version of Enumerable#inject intended to return a string provides a count to know what # object youre on
43 44 45 46 47 48 49 50 51 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 43 def my_inject_with_count(array) value = nil count = 0 array.inject('') do |accum, element| value = yield(accum, element, count) count += 1 value # if not here, returns the value of count end end |
#netrc_error(netrc_file:, error:) ⇒ Object
Adds the error to VpsCli#errors array
90 91 92 93 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 90 def netrc_error(netrc_file:, error:) error_msg = "Unable to write to your #{netrc_file}." VpsCli.errors << error.exception(error_msg) end |
#write_to_netrc(netrc_file: nil, string:) ⇒ Object
Writes the value of string to netrc
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/vps_cli/helpers/access_helper.rb', line 74 def write_to_netrc(netrc_file: nil, string:) netrc_file ||= File.join(Dir.home, '.netrc') Rake.mkdir_p(File.dirname(netrc_file)) Rake.touch(netrc_file) unless File.exist?(netrc_file) begin File.write(netrc_file, string) rescue Errno::EACCES => e netrc_error(netrc_file: netrc_file, error: e) end end |