diff --git a/Gemfile b/Gemfile index 23e2090..de3ea5c 100644 --- a/Gemfile +++ b/Gemfile @@ -18,3 +18,5 @@ gem "byebug" gem "dry-validation" gem "mocktail" + +gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index f49f61c..7833dd3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,7 @@ GEM benchmark (0.2.1) bigdecimal (3.1.4) byebug (11.1.3) + coderay (1.1.3) concurrent-ruby (1.2.2) connection_pool (2.4.1) diff-lcs (1.5.0) @@ -86,6 +87,7 @@ GEM kramdown (~> 2.0) language_server-protocol (3.17.0.3) lint_roller (1.1.0) + method_source (1.0.0) minitest (5.20.0) mocktail (2.0.0) sorbet-eraser (~> 0.3.1) @@ -104,6 +106,9 @@ GEM parser (3.2.2.4) ast (~> 2.4.1) racc + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) racc (1.7.1) rainbow (3.1.1) rake (13.0.6) @@ -195,6 +200,7 @@ DEPENDENCIES cove! dry-validation mocktail + pry rake (~> 13.0) rspec (~> 3.0) solargraph diff --git a/README.md b/README.md index f5099d8..9bee762 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,22 @@ # Cove -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cove`. To experiment with that code, run `bin/console` for an interactive prompt. +## Console -TODO: Delete this and the text above, and describe your gem +Start: + +``` +bin/console +``` + +Execute a step on a host + +``` +role = roles["nginx/web"] +result = with_connection("host1") { |c| Cove::Steps::GetExistingContainerDetails.call(c, role) } +result.select(&:running?).map(&:name) +``` + +`roles`, `services`, and `hosts` are forwarded to `Cove.registry` ## Installation diff --git a/bin/console b/bin/console index 6c501da..ee252c9 100755 --- a/bin/console +++ b/bin/console @@ -2,17 +2,15 @@ # frozen_string_literal: true require "bundler/setup" -require "cove" -# You can add fixtures and/or initialization code here to make experimenting -# with your gem easier. You can also use a different console, if you like. +Thread.report_on_exception = false -# (If you use this, don't forget to add pry to your Gemfile!) -# require "pry" -# Pry.start +ENV["COVE_CONSOLE_SESSION"] = "true" -Cove.init -include Cove +require "pry" +require "cove" +require "cove/console_helpers" +include Cove::ConsoleHelpers -require "irb" -IRB.start(__FILE__) +Cove.init +Pry.start diff --git a/bin/cove b/bin/cove index 73f4d54..03834db 100755 --- a/bin/cove +++ b/bin/cove @@ -1,10 +1,10 @@ #!/usr/bin/env ruby # frozen_string_literal: true +require "bundler/setup" require "cove" Thread.report_on_exception = false - Cove.init Cove::CLI::Main.start(ARGV) diff --git a/lib/cove.rb b/lib/cove.rb index 4dbf3af..828f8e9 100644 --- a/lib/cove.rb +++ b/lib/cove.rb @@ -18,8 +18,13 @@ "cli" => "CLI", "docker_cli" => "DockerCLI" ) +if ENV["COVE_CONSOLE_SESSION"] == "true" + loader.enable_reloading +end loader.setup +$zeitwerk_loader = loader + module Cove class Error < StandardError; end @@ -40,6 +45,10 @@ def self.init(config: root) Initialization.new(config, registry).perform end + def self.farts + "smell" + end + def self.root ENV.fetch("COVE_CONFIG_DIR", "./") end diff --git a/lib/cove/console_helpers.rb b/lib/cove/console_helpers.rb new file mode 100644 index 0000000..22c783a --- /dev/null +++ b/lib/cove/console_helpers.rb @@ -0,0 +1,51 @@ +module Cove + module ConsoleHelpers + class ConnectionWrapper + include SSHKit::DSL + + def initialize(host, block) + @host = host + @block = block + end + + def call + block = @block + result = nil + + on(@host) do |connection| + result = block.call(self) + end + + result + end + end + + def with_connection(host, &block) + ConnectionWrapper.new(ssh_host(host), block).call + end + + def reload! + $zeitwerk_loader.reload + end + + def ssh_host(name) + hosts[name].sshkit_host + end + + def hosts + registry.hosts + end + + def services + registry.services + end + + def roles + registry.roles + end + + def registry + Cove.registry + end + end +end