Class: Dotgpg::Environment

Inherits:
Dotenv::Environment
  • Object
show all
Defined in:
lib/dotgpg/environment.rb,
lib/dotgpg/environment/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Instance Method Summary collapse

Instance Method Details

#readObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dotgpg/environment.rb', line 7

def read
  # use dotgpg here
  dir = Dotgpg::Dir.closest(@filename)

  fail "not in a dotgpg directory" unless dir

  # if the file's not there assume we're creating a new one and return an
  # empty string
  return '' unless File.exists? @filename

  # make a new stringio object to pass in
  s = StringIO.new
  dir.decrypt @filename, s
  # have to rewind, otherwise read doesn't work
  s.rewind
  s.read
end

#writeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dotgpg/environment.rb', line 25

def write
  # use dotgpg here
  dir = Dotgpg::Dir.closest(@filename)

  fail "not in a dotgpg directory" unless dir
  # make a new stringio object to pass in
  s = StringIO.new

  sort.each do |k,v|
    # if our value has newlines or #'s it needs to be double-quoted. in
    # addition newlines need to be \n and not actual multi-line strings,
    # see https://github.com/bkeepers/dotenv#usage
    v = v.inspect if v.to_s.match(/\n|#/)
    s.write "#{k}=#{v}\n"
  end

  s.rewind
  dir.encrypt @filename, s
end