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.expand_path("~/.config/kbsecret").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.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

{
  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.

Parameters:

  • key (String)

    the configuration key to retrieve

Returns:

  • (Object)

    the corresponding configuration



73
74
75
# File 'lib/kbsecret/config.rb', line 73

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" => "...",  }

Parameters:

  • cmd (String)

    the short name of the command

Returns:

  • (Hash, nil)

    the command's configuration



83
84
85
# File 'lib/kbsecret/config.rb', line 83

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"]

Parameters:

  • cmd (String)

    the short name of the command

Returns:

  • (Array)

    the command's default arguments



93
94
95
# File 'lib/kbsecret/config.rb', line 93

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

.sync!void

This method returns an undefined value.

Writes the user's configuration to disk.



66
67
68
# File 'lib/kbsecret/config.rb', line 66

def self.sync!
  File.open(CONFIG_FILE, "w") { |io| io.write @config.to_yaml }
end

Instance Method Details

#configure_generator(label, hsh) ⇒ void

This method returns an undefined value.

Configure a secret generator.

Parameters:

  • label (String, Symbol)

    the generator label (profile name)

  • hsh (Hash)

    the generator configuration



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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.

Parameters:

  • label (String, Symbol)

    the session label

  • hsh (Hash)

    the session configuration



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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.

Parameters:

  • label (String, Symbol)

    the generator label (profile name)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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.

Parameters:

  • label (String, Symbol)

    the session label



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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.

Parameters:

  • label (String, Symbol)

    the generator's label

Returns:

  • (Hash)

    the generator configuration

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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

Returns whether or not the given generator is configured.

Parameters:

  • label (String, Symbol)

    the generator label

Returns:

  • (Boolean)

    whether or not the given generator is configured



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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>

Returns all configured session labels.

Returns:

  • (Array<Symbol>)

    all configured session labels



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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.

Parameters:

  • label (String, Symbol)

    the session's label

Returns:

  • (Hash)

    the session configuration

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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

Returns whether or not the given session is configured.

Parameters:

  • label (String, Symbol)

    the session label

Returns:

  • (Boolean)

    whether or not the given session is configured



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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>

Returns all configured session labels.

Returns:

  • (Array<Symbol>)

    all configured session labels



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kbsecret/config.rb', line 138

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