Class | SSHMenu::Config |
In: |
lib/sshmenu.rb
(Git)
|
Parent: | Object |
The SSHMenu::Config class implements the data model for the application. It is responsible for:
The ClassMapper is used to delegate chunks of functionality to different classes as follows:
DefaultTooltip | = | 'Open an SSH session in a new window' |
filename | [R] | pathname of user‘s config file ($HOME/.sshmenu) |
Called from SSHMenu::Factory#make_app. Calls load_classes and inject_defaults to set up any class mapping overrides defined in the config file.
# File lib/sshmenu.rb, line 1226 def initialize(args = {}) @globals = { 'tooltip' => DefaultTooltip } @menu_items = [ ] @home_dir = nil # suppress warning message @timestamp = nil @classes = { } if args[:filename] @filename = args[:filename] else @filename = home_dir + '.sshmenu' end load_classes inject_defaults end
Takes a host item, appends it to the main menu and saves the result
# File lib/sshmenu.rb, line 1503 def append_host(item) @menu_items.push(item) self.save end
Called to invoke the setup wizard
# File lib/sshmenu.rb, line 1292 def autoconfigure wizard = mapper.get_class('app.model.autoconf') a = wizard.new.autoconfigure(self) or return set_items_from_array(a) end
Iterator for walking the tree of menu items as a depth first traversal.
# File lib/sshmenu.rb, line 1510 def each_item(&action) # :yields: parent_items, item parents = [] iterate_items(@menu_items, parents, action) end
Gets the value of an attribute in the ‘globals’ config section
# File lib/sshmenu.rb, line 1388 def get(key, default = nil) return default unless @globals.has_key?(key) return @globals[key] end
Sets the state of the ‘hide button border’ option
# File lib/sshmenu.rb, line 1416 def hide_border=(val) set('hide_border', val ? 1 : 0) end
Returns true if the ‘hide button border’ option is enabled
# File lib/sshmenu.rb, line 1407 def hide_border? if opt = get('hide_border') return opt != 0 end return false end
Returns the user‘s home directory which will be determined either from the $HOME environment variable or from the user‘s entry in /etc/passwd.
# File lib/sshmenu.rb, line 1264 def home_dir return @home_dir unless @home_dir.nil? if ENV['HOME'] return @home_dir = Pathname.new(ENV['HOME']) end require 'etc' if name = Etc.getlogin info = Etc.getpwnam(name) return @home_dir = Pathname.new(info.dir) if info.dir end raise "$HOME is not defined" end
Returns a Host item for the supplied name, either by locating the first host definition with a title match, or by using the name as a hostname
# File lib/sshmenu.rb, line 1494 def host_by_name(name) each_item() do |parents, item| return item if item.host? and item.title == name end return host_from_text(name) end
Creates and returns a Host item from a simple hostname string
# File lib/sshmenu.rb, line 1482 def host_from_text(text) item_class = mapper.get_class('app.model.item') return item_class.new_from_hash({ 'type' => 'host', 'title' => text, 'sshparams' => text }) end
Called from the constructor to set up default class mappings for menu item classes and the setup wizard.
# File lib/sshmenu.rb, line 1252 def inject_defaults mapper.inject( 'app.model.item' => SSHMenu::Item, 'app.model.hostitem' => SSHMenu::HostItem, 'app.model.menuitem' => SSHMenu::MenuItem, 'app.model.autoconf' => SSHMenu::SetupWizard ) end
Reads the config file and creates an in-memory representation of the configuration. May be called multiple times during the life of the process (e.g.: if the file is modified). Any config read from the file will replace in-memory config data.
# File lib/sshmenu.rb, line 1329 def load if not_configured? save return end mtime = File.mtime(@filename) if !@timestamp.nil? return if mtime == @timestamp end config = YAML.load_file(@filename) || {} @globals = config['global'] || {} @classes = config['classes'] || {} a = config['items'] || config['item'] || [] set_items_from_array(a) @globals['tooltip'] ||= DefaultTooltip @timestamp = mtime end
Reads the ‘classes’ section from the config file. If a ‘require’ key is defined, the specified file is ‘required’. Any remaining keys are passed to the SSHMenu::ClassMapper. The remainder of the config file is ignored by this routine.
# File lib/sshmenu.rb, line 1303 def load_classes classes = nil begin config = YAML.load_file(@filename) classes = config['classes'] or return rescue return end if source_file = classes.delete('require') begin require source_file rescue Exception => detail raise "Error in 'require': #{detail}" end end classes.each do |k,v| cls = eval "class #{v}\nend\n#{v}" # turn string into a Class mapper.inject(k => cls) end end
Called to copy the config file to .sshmenu.bak before the original is overwritten.
# File lib/sshmenu.rb, line 1381 def make_backup_copy return unless File.exists?(@filename) File.syscopy(@filename, @filename.to_s + '.bak') end
Accessor for the SSHMenu::ClassMapper singleton object
# File lib/sshmenu.rb, line 1245 def mapper ClassMapper.instance end
Sets the state of the ‘Open all windows’ option
# File lib/sshmenu.rb, line 1446 def menus_open_all=(val) set('menus_open_all', val ? 1 : 0) end
Returns true if the ‘Open all windows’ option is enabled
# File lib/sshmenu.rb, line 1437 def menus_open_all? if opt = get('menus_open_all') return opt != 0 end return false end
Sets the state of the ‘tear-off menus’ option
# File lib/sshmenu.rb, line 1431 def menus_tearoff=(val) set('menus_tearoff', val ? 1 : 0) end
Returns true if the ‘tear-off menus’ option is enabled
# File lib/sshmenu.rb, line 1422 def menus_tearoff? if opt = get('menus_tearoff') return opt != 0 end return false end
Returns true if the .sshmenu config file has not been created yet.
# File lib/sshmenu.rb, line 1286 def not_configured? return !File.exists?(@filename) end
Serialises the menu items and global settings to hashes and writes them all back out to the config file in YAML format.
# File lib/sshmenu.rb, line 1364 def save make_backup_copy if back_up_config? fh = File.new(@filename, 'w') config = { 'global' => @globals, 'classes' => @classes, 'items' => @menu_items.map { |i| i.to_h } } fh.print YAML.dump(config) fh.close mtime = File.mtime(@filename) @timestamp = mtime end
Sets the value of an attribute in the ‘globals’ config section
# File lib/sshmenu.rb, line 1395 def set(key, value) @globals[key] = value end
Used to override the default config file (e.g.: call from SSHMenu::App#set_config_file during commandline option parsing).
# File lib/sshmenu.rb, line 1280 def set_config_file(file) @filename = file end
Helper routine to translate the array of menu items from the config file into SSHMenu::Item objects.
# File lib/sshmenu.rb, line 1356 def set_items_from_array(a) item_class = mapper.get_class('app.model.item') @menu_items = item_class.new_from_array(a) end
Sets the state of the ‘show text entry’ option
# File lib/sshmenu.rb, line 1461 def show_entry=(val) set('show_entry', val ? 1 : 0) end
Returns true if the ‘show text entry’ option is enabled
# File lib/sshmenu.rb, line 1452 def show_entry? if opt = get('show_entry') return opt != 0 end return false end