Class: KBSecret::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/kbsecret/config.rb

Overview

Global and per-session configuration for kbsecret.

Constant Summary collapse

CONFIG_DIR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The configuration directory.

File.join(Keybase::Local.private_dir, "kbsecret/.config").freeze
CONFIG_FILE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The configuration file.

File.join(CONFIG_DIR, "config.yml").freeze
COMMAND_CONFIG_FILE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The command configuration file.

File.join(CONFIG_DIR, "commands.ini").freeze
CUSTOM_TYPES_DIR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The directory searched for custom record types.

File.join(CONFIG_DIR, "record").freeze
CONFIG_FACETS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Configuration facets used for method generation.

{
  session: {
    plural: :sessions,
    except: Exceptions::SessionUnknownError,
  },

  generator: {
    plural: :generators,
    except: Exceptions::GeneratorUnknownError,
  },
}.freeze
DEFAULT_SESSION =

The default session configuration.

{
  default: {
    users: [Keybase::Local.current_user],
    root: "default",
  },
}.freeze
DEFAULT_GENERATOR =

The default generator configuration.

{
  default: {
    format: "hex",
    length: 16,
  },
}.freeze
DEFAULT_CONFIG =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

configuration defaults

{
  mount: "/keybase",
  sessions: DEFAULT_SESSION.dup,
  generators: DEFAULT_GENERATOR.dup,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](key) ⇒ Object

Retrieve a configured value.



95
96
97
# File 'lib/kbsecret/config.rb', line 95

def self.[](key)
  @config[key]
end

.command(cmd) ⇒ Hash?

Fetch the configuration for a kbsecret command.

Examples:

# retrieves the config for `kbsecret-list`
Config.command("list") # => { "args" => "...",  }


105
106
107
# File 'lib/kbsecret/config.rb', line 105

def self.command(cmd)
  @command_config[cmd]
end

.command_args(cmd) ⇒ Array

Note:

Default arguments are split according to normal shell splitting rules.

Fetch the configured default arguments for a kbsecret command.

Examples:

Config.command_args("list") # => ["--show-all"]


115
116
117
# File 'lib/kbsecret/config.rb', line 115

def self.command_args(cmd)
  @command_config.dig(cmd, "args")&.shellsplit || []
end

.load!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reads the user's configuration files from disk, introducing default values as necessary.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kbsecret/config.rb', line 68

def self.load!
  user_config = if File.exist?(CONFIG_FILE)
                  YAML.load_file(CONFIG_FILE)
                else
                  DEFAULT_CONFIG
                end

  @command_config = if File.exist?(COMMAND_CONFIG_FILE)
                      INIH.load(COMMAND_CONFIG_FILE)
                    else
                      {}
                    end

  @config = DEFAULT_CONFIG.merge(user_config)
  @config[:sessions].merge!(DEFAULT_SESSION)
  @config[:generators].merge!(DEFAULT_GENERATOR)
end

.sync!void

This method returns an undefined value.

Writes the user's configuration to disk.



88
89
90
# File 'lib/kbsecret/config.rb', line 88

def self.sync!
  File.write(CONFIG_FILE, @config.to_yaml)
end

.unalias_command(acmd) ⇒ String

Attempt to resolve an alias into a kbsecret command.

Examples:

Config.unalias_command("l") # => "list"
Config.unalias_command("madeup") # => "madeup"


125
126
127
128
129
130
131
132
# File 'lib/kbsecret/config.rb', line 125

def self.unalias_command(acmd)
  @command_config.each do |cmd, conf|
    aliases = conf["aliases"]&.split || []
    return cmd if aliases.include?(acmd)
  end

  acmd
end

Instance Method Details

#configure_generator(label, hsh) ⇒ void

This method returns an undefined value.

Configure a secret generator.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#configure_session(label, hsh) ⇒ void

This method returns an undefined value.

Configure a session.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#deconfigure_generator(label) ⇒ void

This method returns an undefined value.

Deconfigure a generator.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#deconfigure_session(label) ⇒ void

Note:

This only removes the given session from the configuration, making it "invisible" to kbsecret. To actually remove all files associated with a session, see Session#unlink!.

This method returns an undefined value.

Deconfigure a session.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#generator(label) ⇒ Hash

Retrieve a generator's configuration.

Raises:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#generator?(label) ⇒ Boolean



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#generator_labelsArray<Symbol>



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#session(label) ⇒ Hash

Retrieve a session's configuration.

Raises:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#session?(label) ⇒ Boolean



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end

#session_labelsArray<Symbol>



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kbsecret/config.rb', line 175

CONFIG_FACETS.each do |facet, data|
  define_singleton_method facet do |label|
    hsh = @config[data[:plural]][label.to_sym]

    raise data[:except], label unless hsh

    hsh
  end

  define_singleton_method "#{facet}_labels" do
    @config[data[:plural]].keys
  end

  define_singleton_method "#{facet}?" do |label|
    @config[data[:plural]].keys.include? label.to_sym
  end

  define_singleton_method "configure_#{facet}" do |label, **hsh|
    @config[data[:plural]][label.to_sym] = hsh
    sync!
  end

  define_singleton_method "deconfigure_#{facet}" do |label|
    @config[data[:plural]].delete(label.to_sym)
    sync!
  end
end