Class: EasyCreds::CredentialsIO
- Inherits:
-
Object
- Object
- EasyCreds::CredentialsIO
- Defined in:
- lib/easy_creds/credentials_io.rb
Constant Summary collapse
- GITIGNORE_RULE =
'/config/credentials/*.key'- LOCAL_GITIGNORE_RULE =
'/config/credentials/*_local.yml.enc'
Instance Method Summary collapse
- #add_gitignore_rule! ⇒ Object
- #add_local_gitignore_rule! ⇒ Object
- #enc_exists?(env, local: false) ⇒ Boolean
- #ensure_key!(env, local: false) ⇒ Object
- #example_template ⇒ Object
- #gitignore_has_local_rule? ⇒ Boolean
- #gitignore_has_rule? ⇒ Boolean
-
#initialize(root, git_repo: true) ⇒ CredentialsIO
constructor
A new instance of CredentialsIO.
- #key_exists?(env, local: false) ⇒ Boolean
- #key_value(env, local: false) ⇒ Object
- #read(env, local: false) ⇒ Object
- #read_raw(env, local: false) ⇒ Object
- #write(env, hash, local: false) ⇒ Object
- #write_key(env, value, local: false) ⇒ Object
- #write_raw(env, yaml_str, local: false) ⇒ Object
Constructor Details
#initialize(root, git_repo: true) ⇒ CredentialsIO
Returns a new instance of CredentialsIO.
13 14 15 16 |
# File 'lib/easy_creds/credentials_io.rb', line 13 def initialize(root, git_repo: true) @root = Pathname.new(root) @git_repo = git_repo end |
Instance Method Details
#add_gitignore_rule! ⇒ Object
75 76 77 78 79 |
# File 'lib/easy_creds/credentials_io.rb', line 75 def add_gitignore_rule! return unless @git_repo gitignore_path.open('a') { |f| f.puts("\n#{GITIGNORE_RULE}") } end |
#add_local_gitignore_rule! ⇒ Object
87 88 89 90 91 92 |
# File 'lib/easy_creds/credentials_io.rb', line 87 def add_local_gitignore_rule! return unless @git_repo return if gitignore_has_local_rule? gitignore_path.open('a') { |f| f.puts('', LOCAL_GITIGNORE_RULE) } end |
#enc_exists?(env, local: false) ⇒ Boolean
65 66 67 |
# File 'lib/easy_creds/credentials_io.rb', line 65 def enc_exists?(env, local: false) enc_path(env, local: local).exist? end |
#ensure_key!(env, local: false) ⇒ Object
48 49 50 51 52 |
# File 'lib/easy_creds/credentials_io.rb', line 48 def ensure_key!(env, local: false) return if key_exists?(env, local: local) write_key(env, SecureRandom.hex(16), local: local) end |
#example_template ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/easy_creds/credentials_io.rb', line 94 def example_template path = @root.join('config/credentials/example.yml') return {} unless path.exist? YAML.safe_load(path.read, symbolize_names: true) || {} rescue Psych::SyntaxError {} end |
#gitignore_has_local_rule? ⇒ Boolean
81 82 83 84 85 |
# File 'lib/easy_creds/credentials_io.rb', line 81 def gitignore_has_local_rule? return false unless @git_repo gitignore_path.exist? && gitignore_path.read.include?(LOCAL_GITIGNORE_RULE) end |
#gitignore_has_rule? ⇒ Boolean
69 70 71 72 73 |
# File 'lib/easy_creds/credentials_io.rb', line 69 def gitignore_has_rule? return false unless @git_repo gitignore_path.exist? && gitignore_path.read.include?(GITIGNORE_RULE) end |
#key_exists?(env, local: false) ⇒ Boolean
31 32 33 |
# File 'lib/easy_creds/credentials_io.rb', line 31 def key_exists?(env, local: false) key_path(env, local: local).exist? end |
#key_value(env, local: false) ⇒ Object
35 36 37 38 39 |
# File 'lib/easy_creds/credentials_io.rb', line 35 def key_value(env, local: false) key_path(env, local: local).read.strip rescue Errno::ENOENT nil end |
#read(env, local: false) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/easy_creds/credentials_io.rb', line 18 def read(env, local: false) content = configuration(env, local: local).read return {} if content.blank? YAML.safe_load(content, symbolize_names: true, permitted_classes: [Symbol, Date, Time]) || {} rescue ActiveSupport::EncryptedFile::MissingKeyError {} end |
#read_raw(env, local: false) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/easy_creds/credentials_io.rb', line 54 def read_raw(env, local: false) content = configuration(env, local: local).read content.presence || "--- {}\n" rescue ActiveSupport::EncryptedFile::MissingKeyError "--- {}\n" end |
#write(env, hash, local: false) ⇒ Object
27 28 29 |
# File 'lib/easy_creds/credentials_io.rb', line 27 def write(env, hash, local: false) configuration(env, local: local).write(hash.deep_stringify_keys.to_yaml) end |
#write_key(env, value, local: false) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/easy_creds/credentials_io.rb', line 41 def write_key(env, value, local: false) path = key_path(env, local: local) path.dirname.mkpath path.write(value) path.chmod(0o600) end |
#write_raw(env, yaml_str, local: false) ⇒ Object
61 62 63 |
# File 'lib/easy_creds/credentials_io.rb', line 61 def write_raw(env, yaml_str, local: false) configuration(env, local: local).write(yaml_str) end |