Module: LocalVault::Config

Defined in:
lib/localvault/config.rb

Overview

Global configuration — paths, default vault, API credentials.

Reads/writes ~/.localvault/config.yml (mode 0600). All directories are created with mode 0700. Override the root path with LOCALVAULT_HOME env var.

Examples:

Config.root_path        # => "~/.localvault"
Config.default_vault    # => "default"
Config.token = "tok-..."
Config.inventlist_handle = "nauman"

Constant Summary collapse

CONFIG_FILE =
"config.yml"

Class Method Summary collapse

Class Method Details

.api_urlString

Read the InventList API base URL.

Returns:



128
129
130
# File 'lib/localvault/config.rb', line 128

def self.api_url
  load.fetch("api_url", "https://inventlist.com")
end

.api_url=(url) ⇒ void

This method returns an undefined value.

Set the InventList API base URL.

Parameters:

  • url (String)

    the API URL to store



136
137
138
139
140
# File 'lib/localvault/config.rb', line 136

def self.api_url=(url)
  data = load
  data["api_url"] = url
  save(data)
end

.config_pathString

Path to the global config file.

Returns:

  • (String)

    absolute path to config.yml



29
30
31
# File 'lib/localvault/config.rb', line 29

def self.config_path
  File.join(root_path, CONFIG_FILE)
end

.default_vaultString

Name of the default vault.

Returns:

  • (String)

    vault name, defaults to “default”



68
69
70
# File 'lib/localvault/config.rb', line 68

def self.default_vault
  load.fetch("default_vault", "default")
end

.default_vault=(name) ⇒ void

This method returns an undefined value.

Set the default vault name.

Parameters:

  • name (String)

    the vault name to use as default



76
77
78
79
80
# File 'lib/localvault/config.rb', line 76

def self.default_vault=(name)
  data = load
  data["default_vault"] = name
  save(data)
end

.ensure_directories!void

This method returns an undefined value.

Create the root, vaults, and keys directories if they don’t exist (mode 0700).



85
86
87
88
89
# File 'lib/localvault/config.rb', line 85

def self.ensure_directories!
  FileUtils.mkdir_p(root_path, mode: 0o700)
  FileUtils.mkdir_p(vaults_path, mode: 0o700)
  FileUtils.mkdir_p(keys_path, mode: 0o700)
end

.inventlist_handleString?

Read the InventList user handle.

Returns:

  • (String, nil)

    the stored handle, or nil



111
112
113
# File 'lib/localvault/config.rb', line 111

def self.inventlist_handle
  load["inventlist_handle"]
end

.inventlist_handle=(h) ⇒ void

This method returns an undefined value.

Set the InventList user handle.

Parameters:

  • h (String)

    the handle to store



119
120
121
122
123
# File 'lib/localvault/config.rb', line 119

def self.inventlist_handle=(h)
  data = load
  data["inventlist_handle"] = h
  save(data)
end

.keys_pathString

Path to the directory containing identity keys.

Returns:

  • (String)

    absolute path to keys/ directory



43
44
45
# File 'lib/localvault/config.rb', line 43

def self.keys_path
  File.join(root_path, "keys")
end

.loadHash

Load the config file as a hash.

Returns:

  • (Hash)

    parsed config, or empty hash if file is missing



50
51
52
53
# File 'lib/localvault/config.rb', line 50

def self.load
  return {} unless File.exist?(config_path)
  YAML.safe_load_file(config_path, permitted_classes: [Symbol]) || {}
end

.root_pathString

Root directory for all LocalVault data. Honors LOCALVAULT_HOME env var.

Returns:

  • (String)

    absolute path, defaults to ~/.localvault



22
23
24
# File 'lib/localvault/config.rb', line 22

def self.root_path
  ENV.fetch("LOCALVAULT_HOME") { File.join(Dir.home, ".localvault") }
end

.save(data) ⇒ void

This method returns an undefined value.

Write config data to disk (mode 0600).

Parameters:

  • data (Hash)

    the config hash to persist



59
60
61
62
63
# File 'lib/localvault/config.rb', line 59

def self.save(data)
  FileUtils.mkdir_p(root_path, mode: 0o700)
  File.write(config_path, YAML.dump(data))
  File.chmod(0o600, config_path)
end

.tokenString?

Read the API authentication token.

Returns:

  • (String, nil)

    the stored token, or nil



94
95
96
# File 'lib/localvault/config.rb', line 94

def self.token
  load["token"]
end

.token=(t) ⇒ void

This method returns an undefined value.

Set the API authentication token.

Parameters:

  • t (String)

    the token to store



102
103
104
105
106
# File 'lib/localvault/config.rb', line 102

def self.token=(t)
  data = load
  data["token"] = t
  save(data)
end

.vaults_pathString

Path to the directory containing all vaults.

Returns:

  • (String)

    absolute path to vaults/ directory



36
37
38
# File 'lib/localvault/config.rb', line 36

def self.vaults_path
  File.join(root_path, "vaults")
end