diff --git a/.rubocop.yml b/.rubocop.yml index 7f1a30f..68938be 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -12,6 +12,12 @@ AllCops: - 'tmp/**/*' NewCops: enable +RSpec: + Language: + Expectations: + - expected_subprocess + - expected_exec + Style/FrozenStringLiteralComment: Enabled: true diff --git a/lib/dip/cli.rb b/lib/dip/cli.rb index 378b607..4646de1 100644 --- a/lib/dip/cli.rb +++ b/lib/dip/cli.rb @@ -69,8 +69,17 @@ def stop(*argv) end desc "down [OPTIONS]", "Run `docker-compose down` command" + method_option :help, aliases: "-h", type: :boolean, desc: "Display usage information" + method_option :all, aliases: "-A", type: :boolean, desc: "Shutdown all running docker-compose projects" def down(*argv) - compose("down", *argv) + if options[:help] + invoke :help, ["down"] + elsif options[:all] + require_relative "commands/down_all" + Dip::Commands::DownAll.new.execute + else + compose("down", *argv) + end end desc "run [OPTIONS] CMD [ARGS]", "Run configured command in a docker-compose service. `run` prefix may be omitted" diff --git a/lib/dip/commands/down_all.rb b/lib/dip/commands/down_all.rb new file mode 100644 index 0000000..8636e5c --- /dev/null +++ b/lib/dip/commands/down_all.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require_relative "../command" + +module Dip + module Commands + class DownAll < Dip::Command + def execute + exec_subprocess( + "docker rm --volumes $(docker stop $(docker ps --filter 'label=com.docker.compose.project' -q))" + ) + end + end + end +end diff --git a/spec/lib/dip/commands/down_all_spec.rb b/spec/lib/dip/commands/down_all_spec.rb new file mode 100644 index 0000000..8bed25e --- /dev/null +++ b/spec/lib/dip/commands/down_all_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "shellwords" +require "dip/cli" +require "dip/commands/down_all" + +describe Dip::Commands::DownAll do + let(:cli) { Dip::CLI } + + before { cli.start "down -A".shellsplit } + + it "runs a valid command" do + expected_subprocess( + "docker rm --volumes $(docker stop $(docker ps --filter 'label=com.docker.compose.project' -q))", + [] + ) + end +end