Class: CLIChef::Cookbook
- Inherits:
-
Object
- Object
- CLIChef::Cookbook
- Includes:
- BBLib::Effortless
- Defined in:
- lib/cli_chef/components/cookbook.rb
Class Method Summary collapse
- .ingredient(name) ⇒ Object
- .method_missing(method, *args, &block) ⇒ Object
- .prototype ⇒ Object
- .respond_to_missing?(method, include_private = false) ⇒ Boolean
Instance Method Summary collapse
-
#execute(string, opts = {}, &block) ⇒ Object
Executes a string as a command to this CLI wrapper in a job (threaded).
-
#execute!(string, opts = {}, &block) ⇒ Object
Synchonous version of execute.
-
#menu ⇒ Object
Produces a dynamic help menu for this wrapper.
-
#prepare(**args) ⇒ Object
Returns the full command line that would be run based on the given arguments.
- #prepare_args(**args) ⇒ Object
-
#ready? ⇒ Boolean
Returns true if the path is either set to a valid file or can be found in the environment.
-
#run(**args, &block) ⇒ Object
Runs a command within a Job (seperate thread) For when a command should be run asynchronously.
-
#run!(**args, &block) ⇒ Object
Blocking version of run that is not executed within a thread.
Class Method Details
.ingredient(name) ⇒ Object
87 88 89 |
# File 'lib/cli_chef/components/cookbook.rb', line 87 def self.ingredient(name) ingredients.find { |i| i.match?(name) } end |
.method_missing(method, *args, &block) ⇒ Object
79 80 81 |
# File 'lib/cli_chef/components/cookbook.rb', line 79 def self.method_missing(method, *args, &block) prototype.respond_to?(method) ? prototype.send(method, *args, &block) : super end |
.prototype ⇒ Object
75 76 77 |
# File 'lib/cli_chef/components/cookbook.rb', line 75 def self.prototype @prototype ||= self.new end |
.respond_to_missing?(method, include_private = false) ⇒ Boolean
83 84 85 |
# File 'lib/cli_chef/components/cookbook.rb', line 83 def self.respond_to_missing?(method, include_private = false) prototype.respond_to?(method) || super end |
Instance Method Details
#execute(string, opts = {}, &block) ⇒ Object
Executes a string as a command to this CLI wrapper in a job (threaded)
25 26 27 28 29 30 31 32 33 |
# File 'lib/cli_chef/components/cookbook.rb', line 25 def execute(string, opts = {}, &block) raise RuntimeError, "A valid path is currently not set for #{self.class}. Please set a valid path to the executable first." unless path return execute!(string, opts.except(:synchronous), &block) if opts[:synchronous] string = "#{clean_path} #{string}" BBLib.logger.debug("About to run cmd: #{string}") (opts.delete(:job_class) || default_job_class).new(opts.merge(command: string, parent: self)).tap do |job| job.run(&block) end end |
#execute!(string, opts = {}, &block) ⇒ Object
Synchonous version of execute
36 37 38 39 40 41 |
# File 'lib/cli_chef/components/cookbook.rb', line 36 def execute!(string, opts = {}, &block) while (job ||= execute(string, opts, &block)).running? # Nothing end job.result end |
#menu ⇒ Object
Produces a dynamic help menu for this wrapper. Useful mostly for console or command line based interactions.
71 72 73 |
# File 'lib/cli_chef/components/cookbook.rb', line 71 def end |
#prepare(**args) ⇒ Object
Returns the full command line that would be run based on the given arguments
57 58 59 |
# File 'lib/cli_chef/components/cookbook.rb', line 57 def prepare(**args) "#{clean_path} #{prepare_args(args)}" end |
#prepare_args(**args) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/cli_chef/components/cookbook.rb', line 61 def prepare_args(**args) args.map do |name, arg| ingredient = self.ingredient(name) raise ArgumentError, "Unknown parameter #{name} for #{self.class}." unless ingredient ingredient.to_s(arg) end.join(' ') end |
#ready? ⇒ Boolean
Returns true if the path is either set to a valid file or can be found in the environment
20 21 22 |
# File 'lib/cli_chef/components/cookbook.rb', line 20 def ready? path && (File.exist?(path) || BBLib::OS.which(path)) end |
#run(**args, &block) ⇒ Object
Runs a command within a Job (seperate thread) For when a command should be run asynchronously
45 46 47 48 |
# File 'lib/cli_chef/components/cookbook.rb', line 45 def run(**args, &block) return run!(args.except(:synchronous), &block) if args[:synchronous] execute(prepare_args(args), &block) end |
#run!(**args, &block) ⇒ Object
Blocking version of run that is not executed within a thread. For when a command should be run synchronously
52 53 54 |
# File 'lib/cli_chef/components/cookbook.rb', line 52 def run!(**args, &block) execute!(prepare_args(args), &block) end |