From 435b558260d14c40f83f726832553bf39fb3f5a3 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 20 Jan 2023 14:38:27 +0100 Subject: [PATCH] Extract pipe pattern --- lib/mrsk/commands/app.rb | 9 +++++---- lib/mrsk/commands/base.rb | 11 ++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 0cc5bb25..5b7c0f36 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -24,7 +24,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base end def stop - [ "docker ps -q #{service_filter.join(" ")} | xargs docker stop" ] + pipe current_container_id, "xargs docker stop" end def info @@ -32,9 +32,10 @@ class Mrsk::Commands::App < Mrsk::Commands::Base end def logs(lines: 1000, grep: nil) - [ "docker ps -q #{service_filter.join(" ")} | xargs docker logs -n #{lines} -t" ].tap do |command| - command.first << " | grep #{grep}" if grep - end + pipe \ + current_container_id, + "xargs docker logs -n #{lines} -t", + ("grep #{grep}" if grep) end def exec(*command, interactive: false) diff --git a/lib/mrsk/commands/base.rb b/lib/mrsk/commands/base.rb index ef5527f9..87f917da 100644 --- a/lib/mrsk/commands/base.rb +++ b/lib/mrsk/commands/base.rb @@ -11,10 +11,15 @@ module Mrsk::Commands end private - def combine(*commands) + def combine(*commands, by: "&&") commands - .collect { |command| command + [ "&&" ] }.flatten # Join commands with && - .tap { |commands| commands.pop } # Remove trailing && + .compact + .collect { |command| Array(command) + [ by ] }.flatten # Join commands + .tap { |commands| commands.pop } # Remove trailing combiner + end + + def pipe(*commands) + combine *commands, by: "|" end def docker(*args)