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
    • 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

DEFAULT =
{:complete_regex=>true, :bolts=>{}, :shell_commands=>{'cd'=>'cd', 'echo'=>'echo'} }

Class Attribute Summary

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Constructor Details

- (Config) initialize(hash = read_config_file)

A new instance of Config



46
47
48
49
50
# File 'lib/lightning/config.rb', line 46

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

Class Attribute Details

+ (String) config_file

~/.lightningrc

Returns:

  • (String) — ~/.lightningrc


34
35
36
# File 'lib/lightning/config.rb', line 34

def config_file
  @config_file ||= File.join(Lightning.home,".lightningrc")
end

Instance Attribute Details

- (String) source_file

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

Returns:

  • (String) — Shell file generated by Builder. Defaults to ~/.lightning/functions.sh


53
54
55
# File 'lib/lightning/config.rb', line 53

def source_file
  @source_file ||= self[:source_file] || File.join(Lightning.dir, 'functions.sh')
end

Class Method Details

+ (Hash) bolt(globs)

Creates a bolt hash given globs

Returns:

  • (Hash) — Creates a bolt hash given globs


39
40
41
# File 'lib/lightning/config.rb', line 39

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

Instance Method Details

- (Hash) bolts

Maps bolt names to their config hashes

Returns:

  • (Hash) — Maps bolt names to their config hashes


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

def bolts
  self[:bolts]
end

- (String) function_name(shell_command, bolt)

Creates a command name from its shell command and bolt in the form “#{command}-#{bolt}”. Uses aliases for either if they exist.

Returns:

  • (String) — Creates a command name from its shell command and bolt in the form “#{command}-#{bolt}”. Uses aliases for either if they exist.


89
90
91
92
# File 'lib/lightning/config.rb', line 89

def function_name(shell_command, bolt)
  cmd = only_command shell_command
  "#{shell_commands[cmd] || cmd}-#{bolt}"
end

- (Array) global_commands

Global shell commands used to generate Functions for all Bolts.

Returns:

  • (Array) — Global shell commands used to generate Functions for all Bolts.


58
59
60
# File 'lib/lightning/config.rb', line 58

def global_commands
  shell_commands.keys
end

- (String) only_command(shell_command)

Extracts shell command from a shell_command string

Returns:

  • (String) — Extracts shell command from a shell_command string


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

def only_command(shell_command)
  shell_command[/\w+/]
end

- (Object) save

Saves config to Config.config_file



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

def save
  File.open(self.class.config_file, "w") {|f| f.puts Hash.new.replace(self).to_yaml }
end

- (Hash) shell_commands

Maps shell command names to their aliases

Returns:

  • (Hash) — Maps shell command names to their aliases


63
64
65
# File 'lib/lightning/config.rb', line 63

def shell_commands
  self[:shell_commands]
end

- (String) unalias_bolt(bolt)

Converts bolt alias to bolt’s name

Returns:

  • (String) — Converts bolt alias to bolt’s name


78
79
80
# File 'lib/lightning/config.rb', line 78

def unalias_bolt(bolt)
  bolts[bolt] ? bolt : (Array(bolts.find {|k,v| v['alias'] == bolt })[0] || bolt)
end

- (String) unaliased_command(cmd)

Converts shell command alias to shell command

Returns:

  • (String) — Converts shell command alias to shell command


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

def unaliased_command(cmd)
  shell_commands.invert[cmd] || cmd
end