Class: Lightning::Config
- Inherits:
-
Hash
- Object
- Hash
- Lightning::Config
- Defined in:
- lib/lightning/config.rb
Overview
Handles config file used to generate bolts and functions. The config file is in YAML so it’s easy to manually edit. Nevertheless, it’s recommended to modify the config through lightning commands since their API is more stable than the config file’s API.
Config File Format
Top level keys:
-
:source_file: Location of shell file generated by Builder. Defaults to ~/.lightning/functions.sh
-
:ignore_paths: Array of paths to globally ignore when generating possible completions
-
:complete_regex: true or false (default is true). When true, regular expressions can be used while completing. See Completion for more details.
-
:shell: Specifies shell Builder uses to generate completions. Defaults to bash.
-
:bolts: Array of bolts with each bolt being a hash with the following keys:
-
name(required): Unique name
-
alias: Abbreviated name which can be used to reference bolt in most lightning commands. This is used over name when generating function names.
-
global: true or false (default is false). When set, uses bolt to generate functions with each command in :shell_commands.
-
globs(required): Array of globs which defines group of paths bolt handles. A glob can any number of shell variables i.e. $HOME which are translated at runtime. If globs is not present, lightning will generate them using a Lightning::Generator method with the same name as the bolt, if it exists.
-
functions: Array of lightning functions. A function can either be a string (shell command with default options) or a hash with the following keys:
-
name: Name of the lightning function
-
shell_command(required): Shell command with default options which this function wraps
-
aliases: A hash of custom aliases and full paths only for this function
-
post_path: String to add immediately after a resolved path
-
-
-
:shell_commands: Hash of global shell commands which are combined with all global bolts to generate functions
Constant Summary collapse
- DEFAULT =
{:complete_regex=>true, :bolts=>{}, :shell_commands=>{'cd'=>'cd', 'echo'=>'echo'} }
Class Attribute Summary collapse
-
.config_file ⇒ String
~/.lightningrc.
Instance Attribute Summary collapse
-
#source_file ⇒ String
Shell file generated by Builder.
Class Method Summary collapse
-
.bolt(globs) ⇒ Hash
Creates a bolt hash given globs.
Instance Method Summary collapse
-
#bolts ⇒ Hash
Maps bolt names to their config hashes.
-
#function_name(shell_command, bolt) ⇒ String
Uses aliases for either if they exist.
-
#global_commands ⇒ Array
Global shell commands used to generate Functions for all Bolts.
-
#initialize(hash = read_config_file) ⇒ Config
constructor
A new instance of Config.
-
#only_command(shell_command) ⇒ String
Extracts shell command from a shell_command string.
-
#save ⇒ Object
Saves config to Config.config_file.
-
#shell_commands ⇒ Hash
Maps shell command names to their aliases.
-
#unalias_bolt(bolt) ⇒ String
Converts bolt alias to bolt’s name.
-
#unaliased_command(cmd) ⇒ String
Converts shell command alias to shell command.
Constructor Details
Class Attribute Details
.config_file ⇒ String
Returns ~/.lightningrc.
37 38 39 |
# File 'lib/lightning/config.rb', line 37 def config_file @config_file end |
Instance Attribute Details
#source_file ⇒ String
Returns Shell file generated by Builder. Defaults to ~/.lightning/functions.sh.
56 57 58 |
# File 'lib/lightning/config.rb', line 56 def source_file @source_file end |
Class Method Details
Instance Method Details
#bolts ⇒ Hash
Returns Maps bolt names to their config hashes.
71 72 73 |
# File 'lib/lightning/config.rb', line 71 def bolts self[:bolts] end |
#function_name(shell_command, bolt) ⇒ String
Uses aliases for either if they exist.
92 93 94 95 |
# File 'lib/lightning/config.rb', line 92 def function_name(shell_command, bolt) cmd = only_command shell_command "#{shell_commands[cmd] || cmd}-#{bolt}" end |
#global_commands ⇒ Array
Returns Global shell commands used to generate Functions for all Bolts.
61 62 63 |
# File 'lib/lightning/config.rb', line 61 def global_commands shell_commands.keys end |
#only_command(shell_command) ⇒ String
Returns Extracts shell command from a shell_command string.
86 87 88 |
# File 'lib/lightning/config.rb', line 86 def only_command(shell_command) shell_command[/\w+/] end |
#save ⇒ Object
Saves config to Config.config_file
98 99 100 |
# File 'lib/lightning/config.rb', line 98 def save File.open(self.class.config_file, "w") {|f| f.puts Hash.new.replace(self).to_yaml } end |
#shell_commands ⇒ Hash
Returns Maps shell command names to their aliases.
66 67 68 |
# File 'lib/lightning/config.rb', line 66 def shell_commands self[:shell_commands] end |
#unalias_bolt(bolt) ⇒ String
Returns Converts bolt alias to bolt’s name.
81 82 83 |
# File 'lib/lightning/config.rb', line 81 def unalias_bolt(bolt) bolts[bolt] ? bolt : (Array(bolts.find {|k,v| v['alias'] == bolt })[0] || bolt) end |
#unaliased_command(cmd) ⇒ String
Returns Converts shell command alias to shell command.
76 77 78 |
# File 'lib/lightning/config.rb', line 76 def unaliased_command(cmd) shell_commands.invert[cmd] || cmd end |