Skip to content

Commit

Permalink
Make the console more useful
Browse files Browse the repository at this point in the history
* Use Pry instead of IRB
* Provide helpers to access the registry
* Add a helper to execute steps on a host
  • Loading branch information
jfahrer committed Nov 29, 2023
1 parent c6c22ff commit 79e5f3d
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ gem "byebug"
gem "dry-validation"

gem "mocktail"

gem "pry"
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -195,6 +200,7 @@ DEPENDENCIES
cove!
dry-validation
mocktail
pry
rake (~> 13.0)
rspec (~> 3.0)
solargraph
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 8 additions & 10 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion bin/cove
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions lib/cove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
51 changes: 51 additions & 0 deletions lib/cove/console_helpers.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 79e5f3d

Please sign in to comment.