Module: Train
- Defined in:
- lib/train/errors.rb,
lib/train.rb,
lib/train/options.rb,
lib/train/plugins.rb,
lib/train/version.rb
Overview
- Author
-
Dominik Richter (<[email protected]>)
Defined Under Namespace
Modules: Extras, Options, Transports Classes: ClientError, Plugins, TransportError, UserError
Constant Summary collapse
- VERSION =
'0.9.4'
Class Method Summary collapse
-
.create(name, *args) ⇒ Transport
Create a new transport instance, with the plugin indicated by the given name.
-
.load_transport(name) ⇒ Train::Transport
Load the transport plugin indicated by name.
-
.options(name) ⇒ Hash
Retrieve the configuration options of a transport plugin.
-
.plugin(version = 1) ⇒ Transport
Create a new plugin by inheriting from the class returned by this method.
-
.target_config(config = nil) ⇒ Object
Resolve target configuration in URI-scheme into all respective fields and merge with existing configuration.
Class Method Details
.create(name, *args) ⇒ Transport
Create a new transport instance, with the plugin indicated by the given name.
17 18 19 20 |
# File 'lib/train.rb', line 17 def self.create(name, *args) cls = load_transport(name) cls.new(*args) unless cls.nil? end |
.load_transport(name) ⇒ Train::Transport
Load the transport plugin indicated by name. If the plugin is not yet found in the plugin registry, it will be attempted to load from ‘train/transports/plugin_name`.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/train.rb', line 37 def self.load_transport(name) res = Train::Plugins.registry[name.to_s] return res unless res.nil? # if the plugin wasnt loaded yet: require 'train/transports/' + name.to_s Train::Plugins.registry[name.to_s] rescue LoadError => _ raise Train::UserError, "Can't find train plugin #{name.inspect}. Please install it first." end |
.options(name) ⇒ Hash
Retrieve the configuration options of a transport plugin.
26 27 28 29 |
# File 'lib/train.rb', line 26 def self.(name) cls = load_transport(name) cls. unless cls.nil? end |
.plugin(version = 1) ⇒ Transport
Create a new plugin by inheriting from the class returned by this method. Create a versioned plugin by providing the transport layer plugin version to this method. It will then select the correct class to inherit from.
The plugin version determins what methods will be available to your plugin.
31 32 33 34 35 36 37 38 39 |
# File 'lib/train/plugins.rb', line 31 def self.plugin(version = 1) if version != 1 fail ClientError, 'Only understand train plugin version 1. You are trying to '\ "initialize a train plugin #{version}, which is not supported "\ 'in the current release of train.' end ::Train::Plugins::Transport end |
.target_config(config = nil) ⇒ Object
Resolve target configuration in URI-scheme into all respective fields and merge with existing configuration. e.g. ssh://bob@remote => backend: ssh, user: bob, host: remote
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/train.rb', line 52 def self.target_config(config = nil) # rubocop:disable Metrics/AbcSize conf = config.nil? ? {} : config.dup # symbolize keys conf = conf.each_with_object({}) do |(k, v), acc| acc[k.to_sym] = v acc end group_keys_and_keyfiles(conf) return conf if conf[:target].to_s.empty? uri = URI.parse(conf[:target].to_s) unless uri.host.nil? and uri.scheme.nil? conf[:backend] ||= uri.scheme conf[:host] ||= uri.host conf[:port] ||= uri.port conf[:user] ||= uri.user conf[:password] ||= uri.password conf[:path] ||= uri.path end # ensure path is nil, if its empty; e.g. required to reset defaults for winrm conf[:path] = nil if !conf[:path].nil? && conf[:path].to_s.empty? # return the updated config conf end |