Module: Adamantite::AdamantiteFileUtils

Included in:
AdamantiteApp, Base::Adamantite, Base::Editor::PasswordObjectEditor
Defined in:
lib/file_utils/adamantite_file_utils.rb

Instance Method Summary collapse

Instance Method Details

#delete_pw_file(title) ⇒ Object



71
72
73
# File 'lib/file_utils/adamantite_file_utils.rb', line 71

def delete_pw_file(title)
  File.delete(pw_file(title))
end

#get_license_keyObject



95
96
97
# File 'lib/file_utils/adamantite_file_utils.rb', line 95

def get_license_key
	File.open(pw_file('master_license_key'), 'rb', &:read)
end

#get_master_encrypted_vault_keyObject



91
92
93
# File 'lib/file_utils/adamantite_file_utils.rb', line 91

def get_master_encrypted_vault_key
  File.open(pw_file('master_encrypted_vault_key'), 'rb', &:read)
end

#get_master_password_hashObject



83
84
85
# File 'lib/file_utils/adamantite_file_utils.rb', line 83

def get_master_password_hash
  File.open(pw_file('master_password_hash'), 'rb', &:read)
end

#get_master_password_infoObject



79
80
81
# File 'lib/file_utils/adamantite_file_utils.rb', line 79

def get_master_password_info
  get_pw_file('master')
end

#get_master_password_saltObject



87
88
89
# File 'lib/file_utils/adamantite_file_utils.rb', line 87

def get_master_password_salt
  File.open(pw_file('master_password_salt'), 'rb', &:read)
end

#get_pw_file(title) ⇒ Object



75
76
77
# File 'lib/file_utils/adamantite_file_utils.rb', line 75

def get_pw_file(title)
  JSON.load_file(pw_file(title))
end

#get_stored_pwsObject



99
100
101
102
103
104
105
# File 'lib/file_utils/adamantite_file_utils.rb', line 99

def get_stored_pws
  excluded_filenames = [
    '.', '..', 'master_password_hash', 'master_password_salt', 'master_encrypted_vault_key',
    'master_license_key'
  ]
  Dir.entries(pwmanager_dir).filter { |f| !excluded_filenames.include?(f) }
end

#has_license_key?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/file_utils/adamantite_file_utils.rb', line 111

def has_license_key?
	pw_file_exists?('master_license_key')
end

#home_dirObject



7
8
9
# File 'lib/file_utils/adamantite_file_utils.rb', line 7

def home_dir
  ENV['HOME']
end

#make_password_dir(password_dir_title) ⇒ Object



27
28
29
# File 'lib/file_utils/adamantite_file_utils.rb', line 27

def make_password_dir(password_dir_title)
  Dir.mkdir(File.join(pwmanager_dir, password_dir_title))
end

#make_pwmanager_dirObject



23
24
25
# File 'lib/file_utils/adamantite_file_utils.rb', line 23

def make_pwmanager_dir
  Dir.mkdir(pwmanager_dir)
end

#master_password_exists?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/file_utils/adamantite_file_utils.rb', line 107

def master_password_exists?
  pw_file_exists?('master_encrypted_vault_key') && pw_file_exists?('master_password_salt')
end

#password_file(*args) ⇒ Object



35
36
37
# File 'lib/file_utils/adamantite_file_utils.rb', line 35

def password_file(*args)
  File.join(pwmanager_dir, *args)
end

#pw_file(title) ⇒ Object



31
32
33
# File 'lib/file_utils/adamantite_file_utils.rb', line 31

def pw_file(title)
  File.join(pwmanager_dir, title)
end

#pw_file_exists?(title) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/file_utils/adamantite_file_utils.rb', line 39

def pw_file_exists?(title)
  File.exist?(pw_file(title))
end

#pwmanager_dirObject



11
12
13
# File 'lib/file_utils/adamantite_file_utils.rb', line 11

def pwmanager_dir
  File.join(home_dir, '.adamantite')
end

#pwmanager_dir_exists?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/file_utils/adamantite_file_utils.rb', line 19

def pwmanager_dir_exists?
  Dir.exist?(pwmanager_dir)
end

#pwmanager_tmp_dirObject



15
16
17
# File 'lib/file_utils/adamantite_file_utils.rb', line 15

def pwmanager_tmp_dir
  File.join(home_dir, '.adamantite_tmp')
end

#read_file(file_name, binary) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/file_utils/adamantite_file_utils.rb', line 63

def read_file(file_name, binary)
  if binary
    File.open(file_name, 'rb', &:read)
  else
    File.open(file_name, 'r', &:read)
  end
end

#write_pw_to_file(title, **kwargs) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/file_utils/adamantite_file_utils.rb', line 43

def write_pw_to_file(title, **kwargs)
  make_pwmanager_dir unless pwmanager_dir_exists?

  File.open(pw_file(title), 'w') do |f|
    JSON.dump(kwargs, f)
  end
end

#write_to_file(file_name, file_contents, binary) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/file_utils/adamantite_file_utils.rb', line 51

def write_to_file(file_name, file_contents, binary)
  if binary
    File.open(file_name, 'wb') do |f|
      f.write(file_contents)
    end
  else
    File.open(file_name, 'w') do |f|
      f.write(file_contents)
    end
  end
end