Class: Makit::Secrets::LocalSecrets

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/secrets/local_secrets.rb

Instance Method Summary collapse

Instance Method Details

#add(key, value) ⇒ Object



9
10
11
12
13
# File 'lib/makit/secrets/local_secrets.rb', line 9

def add(key, value)
  secrets_hash = get_secrets_hash
  secrets_hash[key] = value
  save_secrets_hash(secrets_hash)
end

#get(key) ⇒ Object



26
27
28
29
# File 'lib/makit/secrets/local_secrets.rb', line 26

def get(key)
  secrets_hash = get_secrets_hash
  secrets_hash[key]
end

#get_secrets_filenameObject



37
38
39
# File 'lib/makit/secrets/local_secrets.rb', line 37

def get_secrets_filename
  "#{Makit::Directories::ROOT}/secrets.json"
end

#get_secrets_hashObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/makit/secrets/local_secrets.rb', line 41

def get_secrets_hash
  secrets_file = get_secrets_filename
  return {} unless File.exist?(secrets_file)

  text = File.read(secrets_file).strip
  return {} if text.empty?

  JSON.parse(text)
rescue JSON::ParserError
  {}
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/makit/secrets/local_secrets.rb', line 21

def has_key?(key)
  secrets_hash = get_secrets_hash
  secrets_hash.key?(key)
end

#infoObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/makit/secrets/local_secrets.rb', line 59

def info
  secrets_hash = get_secrets_hash
  if secrets_hash.empty?
    puts "  No secrets found"
  else
    puts "  Available secrets (#{secrets_hash.keys.count}):"
    secrets_hash.keys.sort.each do |key|
      puts "    - #{key}"
    end
  end
end

#remove(key) ⇒ Object



15
16
17
18
19
# File 'lib/makit/secrets/local_secrets.rb', line 15

def remove(key)
  secrets_hash = get_secrets_hash
  secrets_hash.delete(key)
  save_secrets_hash(secrets_hash)
end

#save_secrets_hash(hash) ⇒ Object



53
54
55
56
57
# File 'lib/makit/secrets/local_secrets.rb', line 53

def save_secrets_hash(hash)
  secrets_file = get_secrets_filename
  # pretty print the hash
  File.open(secrets_file, "w") { |f| f.puts JSON.pretty_generate(hash) }
end

#set(key, value) ⇒ Object



31
32
33
34
35
# File 'lib/makit/secrets/local_secrets.rb', line 31

def set(key, value)
  secrets_hash = get_secrets_hash
  secrets_hash[key] = value
  save_secrets_hash(secrets_hash)
end