Class: Lightning::Config

Inherits:
Hash
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = read_config_file) ⇒ Config

Returns a new instance of Config.



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

def initialize(hash=read_config_file)
  hash = DEFAULT.merge hash
  super
  replace(hash)
end

Class Attribute Details

.config_fileString

Returns ~/.lightningrc.

Returns:

  • (String)

    ~/.lightningrc



37
38
39
# File 'lib/lightning/config.rb', line 37

def config_file
  @config_file
end

Instance Attribute Details

#source_fileString

Returns Shell file generated by Builder. Defaults to ~/.lightning/functions.sh.

Returns:

  • (String)

    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

.bolt(globs) ⇒ Hash

Returns Creates a bolt hash given globs.

Returns:

  • (Hash)

    Creates a bolt hash given globs



42
43
44
# File 'lib/lightning/config.rb', line 42

def bolt(globs)
  {'globs'=>globs.map {|e| e.sub(/^~/, Lightning.home) }}
end

Instance Method Details

#boltsHash

Returns Maps bolt names to their config hashes.

Returns:

  • (Hash)

    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.

Returns:

  • (String)

    Creates a command name from its shell command and bolt in the form “#command-#bolt”.



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_commandsArray

Returns Global shell commands used to generate Functions for all Bolts.

Returns:

  • (Array)

    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.

Returns:

  • (String)

    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

#saveObject

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_commandsHash

Returns Maps shell command names to their aliases.

Returns:

  • (Hash)

    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.

Returns:

  • (String)

    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.

Returns:

  • (String)

    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